BATCH Run a Word Macro

Deleted
Not applicable

I would like to run a word macro (for converting files to PDF without any markup comments) for many files in a given folder

I pieced this together from other powerShell scripts I had and it doesn't work

 

 

Param(

[Parameter(Mandatory=$True)]

[string]$FilePath

)

 

$Files = Get-ChildItem "$FilePath\*.docx"

 

$Word = New-Object -ComObject Word.Application

 

Foreach ($File in $Files) {

# open a Word document, filename from the directory

$Doc = $Word.Documents.Open($File.FullName)

 

#run Macro to Export to PDF without comments

$Doc.Run("NoCommentsPDF")

 

$Doc.Close()

}




 

(I got the macro from MVP Doug Robbins)
[here is the macro and a link to the thread on a diffrerent Microsoft Q&A where I received this

 

With ActiveWindow.View.RevisionsFilter

.Markup = wdRevisionsMarkupNone

.View = wdRevisionsViewFinal

End With

With ActiveDocument

.ExportAsFixedFormat Left(.FullName, InStr(.FullName, ".") - 1), wdExportFormatPDF, True, wdExportOptimizeForPrint

End With

 

 

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_win10-mso_365hp/default-saveasp...

1 Reply
$Doc.Run("NoCommentsPDF")

Replace with

$Word.Run and it will work fine.