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









share|improve this question




















  • 1





    FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

    – James C.
    Dec 31 '18 at 16:29











  • According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    – LotPings
    Dec 31 '18 at 18:17
















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









share|improve this question




















  • 1





    FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

    – James C.
    Dec 31 '18 at 16:29











  • According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    – LotPings
    Dec 31 '18 at 18:17














0












0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 4:29







Elijah Strug

















asked Dec 31 '18 at 14:50









Elijah StrugElijah Strug

11




11








  • 1





    FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

    – James C.
    Dec 31 '18 at 16:29











  • According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    – LotPings
    Dec 31 '18 at 18:17














  • 1





    FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

    – James C.
    Dec 31 '18 at 16:29











  • According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    – LotPings
    Dec 31 '18 at 18:17








1




1





FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

– James C.
Dec 31 '18 at 16:29





FYI Powershell doesn't need the ; line terminator, it's only used when you have two seperate commands on a single line.

– James C.
Dec 31 '18 at 16:29













According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

– LotPings
Dec 31 '18 at 18:17





According to this link you might try: $Doc = $Word.Documents.Open($filepath, $False, $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

– LotPings
Dec 31 '18 at 18:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988721%2fopening-large-set-of-word-documents-with-powershell-automation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988721%2fopening-large-set-of-word-documents-with-powershell-automation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas