Consulting

Results 1 to 7 of 7

Thread: How to update the TOC header number when I hide some section in word document use VBA

  1. #1
    VBAX Newbie
    Joined
    Nov 2020
    Posts
    4
    Location

    Smile How to update the TOC header number when I hide some section in word document use VBA

    Hi, All:
    Nice to meet you all here, I have a question about the Word VBA program. How I can update my table of content, when I hide one section? I use one click code like this:
    Private Sub CheckBox1_Click()
    Dim Doc As Document
    Set Doc = ActiveDocument
    If CheckBox1.Value Then
    Doc.Sections(7).Range.Font.Hidden = True
    Else
    Doc.Sections(7).Range.Font.Hidden = Fault
    End If

    Application.ScreenRefresh
    End Sub
    *********************************************
    It can hide the section, but after I update the TOC, the format as below:
    5. 33
    5.1 33
    5.2 33
    5.3 33
    5.4 33
    5.5 33
    5.5.1 33
    5.5.2 33
    5.5.3 33
    6. SHREDDER SYSTEM OPERATION 33
    6.1 TOPPER SHREDDER (SHREDDER 1) 33
    6.1.1 JOG MODE 36
    6.2 BOTTOM SHREDDER 37
    6.2.1 JOG MODE 37
    6.3 DIVERTER 38
    6.3.1 CONTROL OPERATORS 38
    6.3.2 JOG MODE 38
    *****************************************
    Above I use the code like this:
    Sub FieldsUpdates()
    Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
    oStory.Fields.Update
    If oStory.StoryType <> wdMainTextStory Then
    While Not (oStory.NextStoryRange Is Nothing)
    Set oStory = oStory.NextStoryRange
    oStory.Fields.Update
    Wend
    End If
    Next oStory
    Set oStory = Nothing

    Dim TOC As TableOfContents
    For Each TOC In ActiveDocument.TablesOfContents
    TOC.Update
    Next
    End Sub
    ********************************************************
    The section already hide in the document, but my hide section TOC should also hide and header section 6 should change number to header 5, how I can do it please?
    I am not a professional programmer, I am Electrical Engineer. Thanks for your help, I appreciate your time for me.

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    118
    Location
    I am a lawyer. Lawyers went to law school because they were afraid of math.

    Doc.Sections(7).Range.Font.Hidden = Fault
    I think you want False instead of Fault.

    It looks to me like your numbering is not being hidden.

    After you hide the section, if you look at your document, is it hidden? Or, do your numbers still appear?
    What happens if after you hide the section you manually update your TOC? Same thing?

  3. #3
    VBAX Newbie
    Joined
    Nov 2020
    Posts
    4
    Location

    Thanks for your reply

    Quote Originally Posted by Chas Kenyon View Post
    I am a lawyer. Lawyers went to law school because they were afraid of math.

    Doc.Sections(7).Range.Font.Hidden = Fault
    I think you want False instead of Fault.

    It looks to me like your numbering is not being hidden.

    After you hide the section, if you look at your document, is it hidden? Or, do your numbers still appear?
    What happens if after you hide the section you manually update your TOC? Same thing?
    I think you are one of the the lawyers that not afraid math? ^-^^-^
    The code will hide the section in the document and also the heading number with text. The page number is correct in TOC, because have one code is renumber the page, but I can not update the number, some friend suggest use the hyperlink of the page to write the code, make the heading in the TOC hide too and also some code for cascade the heading number which can auto correction. I am not a programmer, so it might take me for a long time. I am not afraid math, but some concept need to search. As the phrase said, different field like we climbing the different mountain. If I manually update the TOC is the same format as the code shows. I will be patiently waiting for the help.
    Thanks for your reply, it is my first time come to this forum because this challenge question.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    To get the TOC entry numbering correct, you would have to change the formatting of the hidden headings to remove the numbers from those, too (e.g. by substituting with a different Style).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.tek-tips.com/viewthread.cfm?qid=1806948
    Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Newbie
    Joined
    Nov 2020
    Posts
    4
    Location
    Thanks, I will delete one forum

  7. #7
    VBAX Newbie
    Joined
    Nov 2020
    Posts
    4
    Location
    Try to delete one forum, but I can not find the delete post botton.

Posting Permissions

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