Opening Large Set of Word Documents With Powershell - Automation

0

I am in the process of assigning a footer to hundreds of word documents with their current filepath. Here is my code, which does the job:

I plan to have $Word.Visible set to false, but it isn't for now for debugging purposes.

This gets all the word docs in a directory, adds footer with their file path, then saves and closes.

I am trying to handle a case like this:

error-window

I just want to skip this, or possibly force open and continue. Not sure the best way to go about this, however, and am seeking some help.

Thanks, Elijah

Set-ExecutionPolicy bypass;
$path = 'somepath';

$documents = Get-ChildItem -Path $path *.docx -Recurse -Force
$filepaths = foreach ($document in $documents) {$document.fullname}
$Word = New-Object -ComObject Word.application;
$Word.Visible = $true;
foreach ($filepath in $filepaths){

    $Doc = $Word.Documents.OpenNoRepairDialog($filepath);
    $Doc.Unprotect();
    $Selection = $Word.Selection;
    $Doc.ActiveWindow.ActivePane.View.SeekView = 4;
    $Selection.ParagraphFormat.Alignment = 1;
    $Selection.TypeText($filepath);
    $Doc.Save();
    $Doc.Close();
}
$Word.Quit();

Edit1: I've made an edit where it adds the dynamic field object for the file path, rather than just typing in the file path, that way if you happen to move the file, the file path can be updated to the new path. You will have to press F9 while selecting the footer in word, but this is the best you can do without making a macro and saving the file as a .docm.

Here is the amended code:

$documents = Get-ChildItem -path *docx -recurse -force
$filepaths = foreach($document in $documents){$document.FullName}
Set-Variable -Name wdFieldFileName -Value 29 -Option constant -Force -ErrorAction SilentlyContinue
$word = New-Object -ComObject Word.Application
#$word.Visible = $true
foreach($filepath in $filepaths){
    $doc = $word.Documents.Open($filepath)
    $sections = $doc.Sections
    $item1 = $sections.Item(1)
    $footer = $item1.Footers.Item(1)
    $range = $footer.Range
    $doc.Fields.Add($range, $wdFieldFileName, '\p')
    $doc.Save()
    $doc.Close()
}
$word.Quit()

I am still running into the error window when trying to open corrupted or document "in need of repair" as diagnosed by word. Passing in multiple arguments to the Open() method does not yield results as expected. Here is an example:

Exception calling "Open" with "16" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At line:1 char:1
+ $doc = $word.Documents.Open($filepath, $False, $False, $False, $null, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
powershell
automation
ms-word
asked on Stack Overflow Dec 31, 2018 by Elijah Strug • edited Jan 2, 2019 by Elijah Strug

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0