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.
Michael FultonMCITP: Enterprise/Virtual AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"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.

GrahamSkanRetiredCommented:
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

0
GrahamSkanRetiredCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael FultonMCITP: Enterprise/Virtual AdministratorAuthor Commented:
I assume you got that from the macro recording?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

GrahamSkanRetiredCommented:
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?
0
Michael FultonMCITP: Enterprise/Virtual AdministratorAuthor Commented:
No.  I was curious.  Looked similar to the code I was presented after recording the macro.
0
PberSolutions ArchitectCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.