PowerShell: Update/Refresh the TOC of a Word-Document












0















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:



enter image description here



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









share|improve this question





























    0















    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:



    enter image description here



    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









    share|improve this question



























      0












      0








      0








      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:



      enter image description here



      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









      share|improve this question
















      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:



      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 22 at 13:16







      SteffPoint

















      asked Jan 22 at 13:02









      SteffPointSteffPoint

      1074




      1074






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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()





          share|improve this answer
























          • Thank you very very much! Now it is working as desired :)

            – SteffPoint
            Jan 22 at 15:34





















          1














          One can have multiple Tables Of Contents, which is why this is called
          TablesOfContents.



          You should use TablesOfContents(1).Update().






          share|improve this answer
























          • Thank you very much!

            – SteffPoint
            Jan 22 at 15:34











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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()





          share|improve this answer
























          • Thank you very very much! Now it is working as desired :)

            – SteffPoint
            Jan 22 at 15:34


















          1














          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()





          share|improve this answer
























          • Thank you very very much! Now it is working as desired :)

            – SteffPoint
            Jan 22 at 15:34
















          1












          1








          1







          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()





          share|improve this answer













          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()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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





















          • 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















          1














          One can have multiple Tables Of Contents, which is why this is called
          TablesOfContents.



          You should use TablesOfContents(1).Update().






          share|improve this answer
























          • Thank you very much!

            – SteffPoint
            Jan 22 at 15:34
















          1














          One can have multiple Tables Of Contents, which is why this is called
          TablesOfContents.



          You should use TablesOfContents(1).Update().






          share|improve this answer
























          • Thank you very much!

            – SteffPoint
            Jan 22 at 15:34














          1












          1








          1







          One can have multiple Tables Of Contents, which is why this is called
          TablesOfContents.



          You should use TablesOfContents(1).Update().






          share|improve this answer













          One can have multiple Tables Of Contents, which is why this is called
          TablesOfContents.



          You should use TablesOfContents(1).Update().







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 13:54









          harrymcharrymc

          258k14270572




          258k14270572













          • 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





          Thank you very much!

          – SteffPoint
          Jan 22 at 15:34


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀