Need to automate Document Merge in Word 2016

Michael Fulton
Michael Fulton used Ask the Experts™
on
I have a PowerShell script that collects data from Active Directory and puts it into a nicely formatted Word Document and appends the date at the end of the file name when it is saved so I can see the different files and when they were ran.
I then run the Document Merge in Word to save the file with the differences indicated between the last two times the script ran.
***I want to automate this process.***

I don't want a third party application to do this.  I want to be able to do this with either PowerShell or VBS or a VBS called out from with in a PowerShell Script.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
GrahamSkanRetired
Top Expert 2012

Commented:
This is VBA code for the basic merge function. It can be called from the command line or converted to VBS.
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim docMain As Word.Document
    Dim bNewInstance As Boolean
    Dim strMainDoc As String
    
    strMainDoc = "C:\MyFolder\MyMainMerge.docx"
    'Try to use already running instance of Word
    On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    
    If wdApp Is Nothing Then 'there wasn't one, so create a new one
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    
    Set docMain = wdApp.Documents.Open(strMainDoc)
    docMain.MailMerge.Execute
    docMain.Close wdDoNotSaveChanges
    
    'leave application open in case there is a result document to be saved
    'otherwise
    'only close Word application if instance created for this macro
    If bNewInstance Then
        wdApp.Quit
    End If

Open in new window

Retired
Top Expert 2012
Commented:
Here is the VBS version.
    Dim wdApp 'As Word.Application
    Dim wdDoc 'As Word.Document
    Dim docMain As Word.Document
    Dim bNewInstance 'As Boolean
    Dim strMainDoc 'As String
    
    strMainDoc = "C:\MyFolder\MyMainMerge.docx"
    'Try to use already running instance of Word
    On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    
    If wdApp Is Nothing Then 'there wasn't one, so create a new one
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    
    Set docMain = wdApp.Documents.Open(strMainDoc)
    docMain.MailMerge.Execute
    docMain.Close 'wdDoNotSaveChanges
    
    'leave application open in case there is a result document to be saved
    'otherwise
    'only close Word application if instance created for this macro
    If bNewInstance Then
        wdApp.Quit
    End If

Open in new window

Michael FultonMCITP: Enterprise/Virtual Administrator

Author

Commented:
I assume you got that from the macro recording?
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

GrahamSkanRetired
Top Expert 2012

Commented:
No, I wrote that from scratch, though it's adapted from some of my library code. If it were recorded, you would see the Selection object,  method or both. Does it matter?
Michael FultonMCITP: Enterprise/Virtual Administrator

Author

Commented:
No.  I was curious.  Looked similar to the code I was presented after recording the macro.
PberSolutions Architect

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: GrahamSkan (https:#a42434758)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial