Show and hide MS Word page sections in VBA

How can I show and hide page sections in MS Word using VBA?

Will hidden sections also be hidden from page numbering and printing?
ag1iffitAsked:
Who is Participating?
 
Rgonzo1971Commented:
then try ( bookmarks names to be changed line 10)

Sub HideSection(strBkMrk As String)
ActiveDocument.Bookmarks(strBkMrk).Range.Font.Hidden = True
End Sub

Sub ShowSection(strBkMrk As String)
ActiveDocument.Bookmarks(strBkMrk).Range.Font.Hidden = False
End Sub
Private Sub cbOheadServYes_Click()
    
    strBkMrks = "Sec001,Sec002" ' to be changed
    For Each bkMrk In Split(strBkMrks, ",")
        If cbOheadServYes.Value = True Then
            ShowSection (bkMrk)
        Else
            HideSection (bkMrk)
        End If
    Next

End Sub

Open in new window

0
 
Rgonzo1971Commented:
Hi,

pls try
Sub HideSection()
ActiveDocument.Sections(1).Range.Font.Hidden = True
End Sub

Sub ShowSection()
ActiveDocument.Sections(1).Range.Font.Hidden = False
End Sub

Open in new window

Regards
0
 
ag1iffitAuthor Commented:
Hi

Thanks, I've tried this with various section indexes but no resulting change to the document.

Sub HideSection()
    ActiveDocument.Sections(1).Range.Font.Hidden = True
End Sub

Sub ShowSection()
    ActiveDocument.Sections(1).Range.Font.Hidden = False
End Sub

Private Sub cbOheadServYes_Click()
    
    If cbOheadServYes.Value = True Then
        ShowSection
    Else
        HideSection
    End If

End Sub

Open in new window


ActiveDocument.Sections
Is there a way to identify the page section by perhaps a name or an ID ?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Rgonzo1971Commented:
Could you send a dummy?
0
 
ag1iffitAuthor Commented:
Red tape, would need authorisation.

Could mock something up, but.....
.... is there a way to ID the sections?
Or even interrogate the type of an object?

If so I can work it out with out too much trouble.
0
 
Rgonzo1971Commented:
The hidden Property will affect the numbering in the printing as well as the printing but it does not hide the text in the Print Layout View but only in the Full Screen Reading view
0
 
Rgonzo1971Commented:
Hi,

you could create Bookmarks

Sub Macro()

For Each Section In ActiveDocument.Sections
    Idx = Idx + 1
    ActiveDocument.Bookmarks.Add "Sec" & Format(Idx, "000"), Section.Range
Next
End Sub

Open in new window


Or simply use Ctrl+G (Goto) Section

Regards
0
 
ag1iffitAuthor Commented:
Thanks that's great,

Is it possible to go the other way?
i.e.
manually add named bookmarks,
reference the bookmarks in VBA,
get the containing page section from the bookmark in VBA for hiding/showing.

If so I can assess against
The hidden Property will affect the numbering in the printing as well as the printing but it does not hide the text in the Print Layout View but only in the Full Screen Reading view

and close the question/solution

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.