Avatar of Michael Fulton
Michael Fulton
Flag for United States of America asked on

Need to automate Document Merge in Word 2016

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.
PowershellVB ScriptActive DirectoryMicrosoft Word

Avatar of undefined
Last Comment
Pber

8/22/2022 - Mon
GrahamSkan

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

ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Michael Fulton

ASKER
I assume you got that from the macro recording?
GrahamSkan

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Michael Fulton

ASKER
No.  I was curious.  Looked similar to the code I was presented after recording the macro.
Pber

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