• Macro to Update TOC/Print (2000/SP3)

    Author
    Topic
    #413251

    I’ve created a macro that when doing Control+P (or File+Print) will set a bookmark, go to the TOC and update it then go back to the newly create bookmark and have the print dialog box open. Right now, it will do everything except update the TOC. Is what I’m trying to accomplish doable? This is what I have so far:

    Sub FilePrint()
    With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:=”Start”
    End With
    Selection.GoTo What:=wdGoToBookmark, Name:=”TOC”
    Selection.Fields.Update
    With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    Selection.Fields.Update
    Selection.GoTo What:=wdGoToBookmark, Name:=”Start”
    With ActiveDocument.Bookmarks
    End With
    Dialogs(wdDialogFilePrint).Show
    End Sub

    Viewing 1 reply thread
    Author
    Replies
    • #910204

      Are you sure your TOC is getting selected before you update the selection? If not, any reason you can’t update all fields in the document (if any link to remote resources it could be slow…)? That would be the most reliable way to have the TOC update…

      I also think you could delete a few lines that don’t seem to have any effect:

      Sub FilePrint()
           With ActiveDocument.Bookmarks
                .Add Range:=Selection.Range, Name:="Start"
           End With
           Selection.GoTo What:=wdGoToBookmark, Name:="TOC" 'Does this select the TOC?
           Selection.Fields.Update
           With ActiveDocument.Bookmarks
                .DefaultSorting = wdSortByName
                .ShowHidden = False
           End With
           Selection.GoTo What:=wdGoToBookmark, Name:="Start"
           Dialogs(wdDialogFilePrint).Show
      End Sub
      • #910210

        Thanks for the reply. I’m assuming the TOC is selected because I can briefly see that it is selected then it jumps back to the print dialog screen. I’m guessing that because of the 2 different options for updating a TOC that it isn’t working correctly. As far as updating entire document, there will be a letter with form fields in it that aren’t to be updated.

        Also, thanks for removing unneeded lines – I had been doing that but have recorded and re-recorded so many times that I didn’t clean it before posting.

        • #910216

          To update just the page numbers in the TOC, use

          ActiveDocument.TablesOfContents(1).UpdatePageNumbers

          To update the entire TOC, use

          ActiveDocument.TablesOfContents(1).Update

          This assumes that you have only one TOC in the document. No need to set bookmarks and to change the selection.

          • #910218

            Thanks Hans, works like a charm! I should have posted a couple of hours earlier.

          • #910219

            Thanks Hans, works like a charm! I should have posted a couple of hours earlier.

        • #910217

          To update just the page numbers in the TOC, use

          ActiveDocument.TablesOfContents(1).UpdatePageNumbers

          To update the entire TOC, use

          ActiveDocument.TablesOfContents(1).Update

          This assumes that you have only one TOC in the document. No need to set bookmarks and to change the selection.

      • #910211

        Thanks for the reply. I’m assuming the TOC is selected because I can briefly see that it is selected then it jumps back to the print dialog screen. I’m guessing that because of the 2 different options for updating a TOC that it isn’t working correctly. As far as updating entire document, there will be a letter with form fields in it that aren’t to be updated.

        Also, thanks for removing unneeded lines – I had been doing that but have recorded and re-recorded so many times that I didn’t clean it before posting.

    • #910205

      Are you sure your TOC is getting selected before you update the selection? If not, any reason you can’t update all fields in the document (if any link to remote resources it could be slow…)? That would be the most reliable way to have the TOC update…

      I also think you could delete a few lines that don’t seem to have any effect:

      Sub FilePrint()
           With ActiveDocument.Bookmarks
                .Add Range:=Selection.Range, Name:="Start"
           End With
           Selection.GoTo What:=wdGoToBookmark, Name:="TOC" 'Does this select the TOC?
           Selection.Fields.Update
           With ActiveDocument.Bookmarks
                .DefaultSorting = wdSortByName
                .ShowHidden = False
           End With
           Selection.GoTo What:=wdGoToBookmark, Name:="Start"
           Dialogs(wdDialogFilePrint).Show
      End Sub
    Viewing 1 reply thread
    Reply To: Reply #910205 in Macro to Update TOC/Print (2000/SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel