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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Rgonzo1971Commented:
Could you send a dummy?
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.