Results 1 to 5 of 5
  1. #1
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    Hi all,

    I'm still wrestling with Tables of Contents and VBA. I must be missing something.

    If I have a TOC in my document and use VBA to try to edit it --
    Code:
    Sub EditTOC
    Dialogs(wdDialogInsertTableOfContents).Show
    End Sub
    The dialog does not reflect what's already in the document and displays the dialog's defaults. This is not true when you run Insert Table of Contents from the UI.

    Then, once changes are made to the dialog, they are not applied to the existing TOC. In fact, it doesn't look as if anything at all happens. Again, this differs from what happens when you run Insert Table of Contents from the UI.

    My experience with the Dialogs object in the past has not been so mystifying. Indeed, I have now seen a posting or two that indicates using the TOC dialog from VBA will drive me crazy. However, I want the user to be able to interact with the dialog and can't find a way to simply call the Word Command (TableOfContentsInsert) from the midst of my VBA code.

    What am I missing here?

    Thanks!
    Kim

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by kmurdock View Post
    I want the user to be able to interact with the dialog and can't find a way to simply call the Word Command (TableOfContentsInsert) from the midst of my VBA code.
    [s]Maybe:

    Application.Run "InsertTableOfContents"

    I think you need your insertion point to be in the TOC already.[/s]

    Edit: Please ignore that. The problem actually is selecting the entire TOC, after which the Dialogs().Show is better.

    Code:
    ' Select current TOC
    Dim iCount As Integer
    With ActiveDocument.TablesOfContents
        For iCount = 1 To .Count
            If Selection.Range.Start > .Item(iCount).Range.Start And _
               Selection.Range.Start < .Item(iCount).Range.End Then
                .Item(iCount).Range.Select
                Exit For
            End If
        Next
    End With
    ' Show InsertTOC dialog with current TOC settings
    Dialogs(wdDialogInsertTableOfContents).Show
    Note that you shouldn't run this code if there are no tables.

  3. #3
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    Thank you Jefferson, but I think this only gets me halfway there.

    I had tried selecting the TOC (a different way) but I was unable to update the TOC once I changed the Dialog.

    Here's what happens. I have a TOC that displays 2 outline levels. Using your code, the Dialog displays with the correct current settings, but when I click OK, nothing happens to the TOC. It remains selected and is not updated with my changes.

    Am I still doing something wrong? Is there a way to capture the changes the user makes and execute them?

    Thanks,
    Kim

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by kmurdock View Post
    Using your code, the Dialog displays with the correct current settings, but when I click OK, nothing happens to the TOC.
    Doh! I didn't bother actually testing whether it saved the changes.

    I'm not sure why it isn't working. Would it be too radical to remove the old TOC as part of the process?

    Code:
    Sub ReplaceTOC()
    ' Select current TOC
    Dim iCount As Integer
    With ActiveDocument.TablesOfContents
        For iCount = 1 To .Count
            If Selection.Range.Start > .Item(iCount).Range.Start And _
               Selection.Range.Start < .Item(iCount).Range.End Then
                .Item(iCount).Range.Select
                Exit For
            End If
        Next
    End With
    ' Show InsertTOC dialog with current TOC settings
    With Dialogs(wdDialogInsertTableOfContents)
        If .Display = -1 Then
            ' User selected OK, remove old and insert new
            Selection.Delete
            .Execute
        End If
    End With
    End Sub

  5. #5
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    Quote Originally Posted by jscher2000 View Post
    I'm not sure why it isn't working. Would it be too radical to remove the old TOC as part of the process?
    Radical?! If it works?! Not hardly. And I believe that's what Word does anyway. This was perfect Jefferson!

    Thank you thank you thank you. (bananas doing the happy dance in your honor)
    Kim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •