Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

Candia Huang

Technical User
Nov 6, 2020
1
0
0
CA
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top