0

I'm trying to do a tricky script to export to pdf, some Word files that are corrected, but with "SIMPLE revision marks". So for now I use ExportAsFixedFormat() from Microsoft but the WdExportItem option is binary (0 or 7) : ALL revision marks or none. Does someone as an idea of an api that would help me in this goal ?

Below, my powershell script :

$path = 'C:\path'

$wd = New-Object -ComObject Word.Application
Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse |
    ForEach-Object {
        $doc = $wd.Documents.Open($_.Fullname)
        $pdf = $_.FullName -replace $_.Extension, '.pdf'
        $doc.ExportAsFixedFormat($pdf,17,$false,0,0,0,0,7,$false, $false,0,$false, $true)
        $doc.Close()
    }
$wd.Quit()
braX
  • 11,506
  • 5
  • 20
  • 33
BelowSea
  • 1
  • 1
  • 1
    Does this answer your question? [powershell word to pdf](https://stackoverflow.com/questions/46286292/powershell-word-to-pdf) – ArcSet Feb 21 '20 at 16:47
  • See my answer for complete working and tested program. The trick here is to programmatticaly set `Markup Insertions` to `None` and `Markup Deletions` to `Hidden` – VA systems engineer Feb 23 '20 at 02:27
  • ArcSet, no it doesn't, because my issue isn't about exporting to pdf but concerns the specification of revision marks. William answered my question. – BelowSea Feb 24 '20 at 10:59
  • @BelowSea: If I answered your question, please accept the answer: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – VA systems engineer Feb 25 '20 at 11:45

1 Answers1

1

I'm using Word 2019

Disclaimer: The settings that my program changes to meet the requirements seem to be sticky, i.e., although I don't save the doc after changing the markup settings, they persist across closing and re-opening the document. My program does not attempt to put the markup settings back to their original settings

The trick here is to programmatically set Markup Insertions to None and Markup Deletions to Hidden

These two lines of code accomplish this. See Code below for complete working and tested program

$wordApp.Options.InsertedTextMark = [Microsoft.Office.Interop.Word.WdInsertedTextMark]::wdInsertedTextMarkNone
$wordApp.Options.DeletedTextMark = [Microsoft.Office.Interop.Word.WdDeletedTextMark]::wdDeletedTextMarkHidden

Here are the corresponding settings in Word

enter image description here

Sample docx input

enter image description here

Sample pdf output

enter image description here

Code

cls

try
{
    $path = 'C:\temp\'
    $Error.Clear()

    $wordApp = New-Object -ComObject Word.Application
    $wordApp.Visible = $false
    $docOpen = $false

    $wordDocFqPathList = @(Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse)

    foreach ($wordDocFqPath in $wordDocFqPathList)
    {
        $doc = $wordApp.Documents.Open($wordDocFqPath.FullName, $false, $true)
        $docOpen = $true

        $doc.Activate()

        $doc.ActiveWindow.View.Type = [Microsoft.Office.Interop.Word.WdViewType]::wdPrintView
        $doc.ShowRevisions = $true

        #set tracked changes to show change bars only
        $doc.ActiveWindow.View.RevisionsFilter.View = [Microsoft.Office.Interop.Word.WdRevisionsView]::wdRevisionsViewFinal
        $doc.ActiveWindow.View.RevisionsFilter.Markup = [Microsoft.Office.Interop.Word.WdRevisionsMarkup]::wdRevisionsMarkupSimple
        $wordApp.Options.InsertedTextMark = [Microsoft.Office.Interop.Word.WdInsertedTextMark]::wdInsertedTextMarkNone
        $wordApp.Options.DeletedTextMark = [Microsoft.Office.Interop.Word.WdDeletedTextMark]::wdDeletedTextMarkHidden

        $pdfDocFqPath = $wordDocFqPath.FullName.Replace(".docx", ".pdf").Replace(".doc", ".pdf")

        #https://learn.microsoft.com/en-us/office/vba/api/word.document.exportasfixedformat
        $doc.ExportAsFixedFormat($pdfDocFqPath,`
                                    [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF,`
                                    $false,`
                                    [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForPrint,`
                                    [Microsoft.Office.Interop.Word.WdExportRange]::wdExportAllDocument,`
                                    0, 0,`
                                    [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentWithMarkup,`
                                    $true, $false)

        $doc.Close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)
        $docOpen = $false
    }
}
finally
{
    if ($docOpen -eq $true)
    {
        $doc.Close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)
    }

    $wordApp.Quit()
}
VA systems engineer
  • 2,856
  • 2
  • 14
  • 38