Exportar datos de access a word
Con el siguiente código se abre la aplicación Microsoft Word y se ‘escribe’ en ella. Para conocer los comandos que dan formato al documento, y la forma en que lo hacen, yo me he apoyado en una macro realizada desde el archivo Word.
Private Sub cmdWord_Click()
'Variables que utilizaremos
Dim AppWord As New Word.Application
Dim Documento As Word.Document
Dim Rango As Word.Range
Dim Parrafo As Paragraph
'Si queremos que se vea en la pantalla
AppWord.Visible = True
'Creamos un nuevo documento
Set Documento = AppWord.Documents.Add
'Ponemos un titulillo
Set Rango = Documento.Sections(1).Range
Rango.ParagraphFormat.Alignment = wdAlignParagraphCenter
Rango.Font.Size = 24
Rango.Font.Bold = True
Rango.Text = "Probando desde VB"
'Pones la siguiente linea
Set Parrafo = Documento.Paragraphs.Add
Set Rango = Parrafo.Range.Next
Rango.ParagraphFormat.Alignment = wdAlignParagraphLeft
Rango.Font.ColorIndex = wdRed
Rango.Font.Size = 16
Rango.InsertBefore ("El valor de Text1= ")
'Pones la siguiente linea
Set Parrafo = Documento.Paragraphs.Add
Set Rango = Parrafo.Range.Next
Rango.ParagraphFormat.Alignment = wdAlignParagraphLeft
Rango.Font.ColorIndex = wdRed
Rango.Font.Size = 16
Rango.InsertBefore ("El valor de Text2= ")
'Guardamos el documento
Documento.SaveAs "C:\prueba.doc"
'Cerramos la aplicacion
AppWord.Quit
'Descargamos los objetos
Set Documento = Nothing
Set Rango = Nothing
Set Parrafo = Nothing
Set AppWord = Nothing
End Sub
Fuente: www.todoexpertos.com
Y este es de mi cosecha…
Dim objWord As Word.Application
Dim objSelection As Word.Selection
Dim objDoc As Word.Document
Set objWord = CreateObject("Word.Application")
Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection
objSelection.Font.Name = "Arial"
objSelection.Font.Size = 9
objSelection.TypeText Text:="Texto"
With objWord.ActiveDocument
.SaveAs "c:\archivo.doc"
.Close
End With
objWord.Quit
Set objSelection = Nothing
Set objDoc = Nothing
Set objWord = Nothing
Y de la página de Microsoft, bastante completino…
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
Dim Pos As Double
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()
'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter()
'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
oPara3.Range.Font.Bold = False
oPara3.Format.SpaceAfter = 24
oPara3.Range.InsertParagraphAfter()
'Insert a 3 x 5 table, fill it with data, and make the first row bold and italic.
Dim r As Integer, c As Integer
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Rows.Item(1).Range.Font.Bold = True
oTable.Rows.Item(1).Range.Font.Italic = True
'Add some text after the table.
'oTable.Range.InsertParagraphAfter()
oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara4.Range.InsertParagraphBefore()
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagraphAfter()
'Insert a 5 x 2 table, fill it with data, and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Columns.Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
oTable.Columns.Item(2).Width = oWord.InchesToPoints(3)
'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
Pos = oWord.InchesToPoints(7)
oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
Do
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertAfter("We're now on page 2. Here's my chart:")
oRng.InsertParagraphAfter()
'Insert a chart and change the chart.
oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( ClassType:="MSGraph.Chart.8", FileName:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 4 'xlLine = 4
oChart.Application.Update()
oChart.Application.Quit()
'If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)
'Add text after the chart.
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.InsertParagraphAfter()
oRng.InsertAfter("THE END.")
'All done. Close this form.
Me.Close()
End Sub
Fuente: Micorsoft.com
Categorías:Microsoft Access
exportar, maccess, word