PowerShell: Update/Refresh the TOC of a Word-Document
I am working on a PowerShell-Script to update / refresh automatically the table of contents in a Word Document (which earlier was exported from an application that doesn't do that automatically)
The TOC in an exported document arrives like that:
On click (Inhaltsverzeichnis aktualisieren) it generates the Table of Contents properly.
However, I want this to happen automatically using PowerShell and came up with the following script (mind, that I am a beginner in PowerShell):
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible=$true
$doc=$word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
$toc.Update()
$latestFile.save()
$latestFile.close()
Following errors I receive - but I don't completely understand and I also don't know how to fix them:
You cannot call a method on a null-valued expression. At line:6 char:1
+ $toc.Update()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Method invocation failed because [System.IO.FileInfo] does not contain a method named 'save'. At line:7 char:1
+ $latestFile.save()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound Method invocation failed because [System.IO.FileInfo] does not contain a method named 'close'. At line:8 char:1
+ $latestFile.close()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
microsoft-word powershell
add a comment |
I am working on a PowerShell-Script to update / refresh automatically the table of contents in a Word Document (which earlier was exported from an application that doesn't do that automatically)
The TOC in an exported document arrives like that:
On click (Inhaltsverzeichnis aktualisieren) it generates the Table of Contents properly.
However, I want this to happen automatically using PowerShell and came up with the following script (mind, that I am a beginner in PowerShell):
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible=$true
$doc=$word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
$toc.Update()
$latestFile.save()
$latestFile.close()
Following errors I receive - but I don't completely understand and I also don't know how to fix them:
You cannot call a method on a null-valued expression. At line:6 char:1
+ $toc.Update()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Method invocation failed because [System.IO.FileInfo] does not contain a method named 'save'. At line:7 char:1
+ $latestFile.save()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound Method invocation failed because [System.IO.FileInfo] does not contain a method named 'close'. At line:8 char:1
+ $latestFile.close()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
microsoft-word powershell
add a comment |
I am working on a PowerShell-Script to update / refresh automatically the table of contents in a Word Document (which earlier was exported from an application that doesn't do that automatically)
The TOC in an exported document arrives like that:
On click (Inhaltsverzeichnis aktualisieren) it generates the Table of Contents properly.
However, I want this to happen automatically using PowerShell and came up with the following script (mind, that I am a beginner in PowerShell):
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible=$true
$doc=$word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
$toc.Update()
$latestFile.save()
$latestFile.close()
Following errors I receive - but I don't completely understand and I also don't know how to fix them:
You cannot call a method on a null-valued expression. At line:6 char:1
+ $toc.Update()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Method invocation failed because [System.IO.FileInfo] does not contain a method named 'save'. At line:7 char:1
+ $latestFile.save()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound Method invocation failed because [System.IO.FileInfo] does not contain a method named 'close'. At line:8 char:1
+ $latestFile.close()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
microsoft-word powershell
I am working on a PowerShell-Script to update / refresh automatically the table of contents in a Word Document (which earlier was exported from an application that doesn't do that automatically)
The TOC in an exported document arrives like that:
On click (Inhaltsverzeichnis aktualisieren) it generates the Table of Contents properly.
However, I want this to happen automatically using PowerShell and came up with the following script (mind, that I am a beginner in PowerShell):
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible=$true
$doc=$word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
$toc.Update()
$latestFile.save()
$latestFile.close()
Following errors I receive - but I don't completely understand and I also don't know how to fix them:
You cannot call a method on a null-valued expression. At line:6 char:1
+ $toc.Update()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Method invocation failed because [System.IO.FileInfo] does not contain a method named 'save'. At line:7 char:1
+ $latestFile.save()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound Method invocation failed because [System.IO.FileInfo] does not contain a method named 'close'. At line:8 char:1
+ $latestFile.close()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
microsoft-word powershell
microsoft-word powershell
edited Jan 22 at 13:16
SteffPoint
asked Jan 22 at 13:02
SteffPointSteffPoint
1074
1074
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Please see your script amended below:
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $doc.TablesOfContents
$toc.item(1).update()
$doc.save()
$doc.close()
The 1st issue you were having is that you assigned the document to the object $doc and then on the next line tried to call the document directly again instead of referencing the object $doc:
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
The 2nd issue is as mentioned by the other user, you need to reference which ToC you want to update:
$toc.item(1).update()
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
add a comment |
One can have multiple Tables Of Contents, which is why this is called
TablesOfContents.
You should use TablesOfContents(1).Update()
.
Thank you very much!
– SteffPoint
Jan 22 at 15:34
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1397014%2fpowershell-update-refresh-the-toc-of-a-word-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please see your script amended below:
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $doc.TablesOfContents
$toc.item(1).update()
$doc.save()
$doc.close()
The 1st issue you were having is that you assigned the document to the object $doc and then on the next line tried to call the document directly again instead of referencing the object $doc:
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
The 2nd issue is as mentioned by the other user, you need to reference which ToC you want to update:
$toc.item(1).update()
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
add a comment |
Please see your script amended below:
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $doc.TablesOfContents
$toc.item(1).update()
$doc.save()
$doc.close()
The 1st issue you were having is that you assigned the document to the object $doc and then on the next line tried to call the document directly again instead of referencing the object $doc:
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
The 2nd issue is as mentioned by the other user, you need to reference which ToC you want to update:
$toc.item(1).update()
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
add a comment |
Please see your script amended below:
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $doc.TablesOfContents
$toc.item(1).update()
$doc.save()
$doc.close()
The 1st issue you were having is that you assigned the document to the object $doc and then on the next line tried to call the document directly again instead of referencing the object $doc:
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
The 2nd issue is as mentioned by the other user, you need to reference which ToC you want to update:
$toc.item(1).update()
Please see your script amended below:
$latestFile = Get-ChildItem -Path C:ExportedDocuments -File -Filter "*.docx" | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $doc.TablesOfContents
$toc.item(1).update()
$doc.save()
$doc.close()
The 1st issue you were having is that you assigned the document to the object $doc and then on the next line tried to call the document directly again instead of referencing the object $doc:
$doc = $word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
The 2nd issue is as mentioned by the other user, you need to reference which ToC you want to update:
$toc.item(1).update()
answered Jan 22 at 14:16
CraftyBCraftyB
1,06429
1,06429
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
add a comment |
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
Thank you very very much! Now it is working as desired :)
– SteffPoint
Jan 22 at 15:34
add a comment |
One can have multiple Tables Of Contents, which is why this is called
TablesOfContents.
You should use TablesOfContents(1).Update()
.
Thank you very much!
– SteffPoint
Jan 22 at 15:34
add a comment |
One can have multiple Tables Of Contents, which is why this is called
TablesOfContents.
You should use TablesOfContents(1).Update()
.
Thank you very much!
– SteffPoint
Jan 22 at 15:34
add a comment |
One can have multiple Tables Of Contents, which is why this is called
TablesOfContents.
You should use TablesOfContents(1).Update()
.
One can have multiple Tables Of Contents, which is why this is called
TablesOfContents.
You should use TablesOfContents(1).Update()
.
answered Jan 22 at 13:54
harrymcharrymc
258k14270572
258k14270572
Thank you very much!
– SteffPoint
Jan 22 at 15:34
add a comment |
Thank you very much!
– SteffPoint
Jan 22 at 15:34
Thank you very much!
– SteffPoint
Jan 22 at 15:34
Thank you very much!
– SteffPoint
Jan 22 at 15:34
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1397014%2fpowershell-update-refresh-the-toc-of-a-word-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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