PDA

View Full Version : [SOLVED:] How to update the TOC header number when I hide some section in word document use VBA



Candia
11-06-2020, 07:57 AM
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.

Chas Kenyon
11-06-2020, 01:30 PM
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?

Candia
11-06-2020, 01:55 PM
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.

macropod
11-06-2020, 06:37 PM
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).

macropod
11-06-2020, 06:45 PM
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.php?faq=new_faq_item#faq_new_faq_item3

Candia
11-09-2020, 06:24 AM
Thanks, I will delete one forum

Candia
11-09-2020, 06:31 AM
Try to delete one forum, but I can not find the delete post botton.