[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More
Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
' Add a reference to the Word-library via VBE > Tools > References > Microsoft Word xx.x Object Library.
' Create a folder named C:\Temp or edit the filnames in the code.
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim bWeStartedWord As Boolean
Dim i As Integer
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wrdApp Is Nothing Then
Set wrdApp = CreateObject("Word.Application")
bWeStartedWord = True
wrdApp.Visible = True 'optional!
Set wrdDoc = wrdApp.Documents.Add ' create a new document
' or open an existing document:
'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.docx")
' example word operations:
'copy data from A1:A10 into the word doc:
For i = 1 To 10
.Content.InsertAfter Range("A" & i).Value
wrdApp.DisplayAlerts = wdAlertsNone
.SaveAs "C:\Temp\MyNewWordDoc.docx", FileFormat:=12 'wdFormatXMLDocument
.Close ' close the document
wrdApp.DisplayAlerts = wdAlertsAll
If bWeStartedWord Then wrdApp.Quit 'close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
Open in new window
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.