Change hidden section break to non-hidden using code

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Change hidden section break to non-hidden using code

Post by Charles Kenyon »

I am trying to help someone in another forum who is trying to remove all hidden text from a document using vba.
That is not hard. However, it turns out that someone creating the document managed to mark one or more section breaks as hidden text. When the section breask are removed, is messes up layout!

I had no idea that one even could make a functioning section break hidden. I have no idea why anyone would want to do this! However, you can, and someone did.

Thoughts on code to unhide section breask and only section breaks? (This is relaxation for me. No pressure. I expect that Doug Robbins will work it out on the Answers forum.)

Here is the original question thread on the Answers forum in case you want to just chime in there.
https://answers.microsoft.com/en-us/sub ... e925b02919

The following code will unhide a section break. I need to loop it until there is no hidden section break.

Code: Select all

Sub FixSectionNotHidden()
'
' FixSectionNotHidden Macro
'
'
    With Selection.Find
        .ClearFormatting
        .Font.Hidden = True
        .Text = "^b"
        .Forward = True
        .Wrap = wdFindContinue
        .Execute
    End With
    With Selection.Font
        .Hidden = False
    End With
End Sub
Note, it is not vba that he is using but C#. Looks to translate through the object model, though. I think the vba would help. I answered his original problem that he couldn't get his code to run deleting the hidden text but then this popped up.

The next steps are to use range object rather than selection object and loop until not found.

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change hidden section break to non-hidden usnig code

Post by HansV »

This macro should unhide all hidden section breaks:

Code: Select all

Sub FixSectionNotHidden()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Font.Hidden = True
        .Text = "^b"
        With .Replacement
            .ClearFormatting
            .Font.Hidden = False
            .Text = "^&"
        End With
        .Execute Replace:=wdReplaceAll
    End With
End Sub
I have posted this in the Microsoft Community thread too.
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Change hidden section break to non-hidden usnig code

Post by Charles Kenyon »

Thank you! I'm sure the code will help. I will leave it to him to convert to C#.
My coding of Find/Replace remains rudimentary.

User avatar
HansV
Administrator
Posts: 78417
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change hidden section break to non-hidden using code

Post by HansV »

It worked! :joy:
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Change hidden section break to non-hidden using code

Post by Rudi »

HansV wrote:It worked! :joy:
Was there any doubt. :grin: :clapping:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.