Skip to content

Instantly share code, notes, and snippets.

@troyscott
Created June 28, 2018 16:31
Show Gist options
  • Save troyscott/f33a7e50753b695fca01562d27d3f742 to your computer and use it in GitHub Desktop.
Save troyscott/f33a7e50753b695fca01562d27d3f742 to your computer and use it in GitHub Desktop.
Write data to word from another Office Program using the Microsoft Word Object Library. Checkout this link https://msdn.microsoft.com/en-us/vba/excel-vba/articles/exporting-a-table-to-a-word-document
Sub ModifyWordDoc()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdbmRange As Word.Range
Application.ScreenUpdating = False
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open("c:\Apps\mydoc.docx")
Set wdbmRange = wdDoc.Bookmarks("Start").Range
wdApp.Visible = True
Set wdbmRange = wdDoc.Bookmarks("ReportName").Range
With wdbmRange
.Select
.Text = "ACCR13 Report"
End With
' Query 1
Set wdbmRange = wdDoc.Bookmarks("Q1_Title").Range
With wdbmRange
.Select
.Style = Word.WdBuiltinStyle.wdStyleHeading1
.Text = "ACCR13"
End With
Set wdbmRange = wdDoc.Bookmarks("Q1_Table").Range
Set wdbmRange = wdDoc.Bookmarks("Q1").Range
With wdbmRange
.InsertAfter vbCrLf & vbCrLf & "select * from table"
.Style = Word.WdBuiltinStyle.wdStyleBodyText
End With
With wdDoc
.SaveAs2 "mydoc2.docx"
.Close
End With
'Quit Word.
wdApp.Quit
Application.ScreenUpdating = True
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment