none
Word VBA: How to show correct headers when showing hidden sections again in a document. RRS feed

  • Question

  • Dear colleagues in MSDN

    When a hidden section appears in a word document, the header which is not connected to the section before is not shown on the first page of the section. The first page is shown with the header of the section before. That's my headache.

    I use in the Checkbox_Change procedure of a check box the following code:

    ActiveDocument.Sections.Item("Number of corresponding section").Range.Select

    If Checkbox.Value = True Then
            With Selection.Font
                .Hidden = False
            End With  

    Else
            With Selection.Font
                .Hidden = True
            End With

    End if

    It works fine but when the section appears, the first page has always the header of the section before. 

    Even if I switch off all other sections besides of the first one, there is always after section 1 a blank page with the header of the next hidden section.

    I hope that someone of you could provide some help on this topic. 

    Thank you very much

    Andres




    Friday, September 5, 2014 1:22 PM

Answers

  • Dear All,

    This very easy way is how I solved it to hide or unhide sections together with their corresponding headers. Just creating a bookmark "Header_Section_2" with the header of section 2 and another bookmark "Section_2" and hiding them together. The bookmark "Section_2" contains section 2 together with the section break before section 2. The same I applied for all the other sections. My fault was probably to think of a word document as a linear object. A bookmark of a section does not contain the header because that one is somehow in a different "plane" of the document.  

    Regards,

    Andres

    Here is the code that is placed in the change-procedure of the ActiveX-checkbox "ChB_Show_Section2".

    Private Sub ChB_Show_Section2_Change()

        If ChB_Show_Section2.Value = True Then
            ActiveDocument.Bookmarks("Section_2").Range.Font.Hidden = False
            ActiveDocument.Bookmarks("Header_Section_2").Range.Font.Hidden = False
        Else
            ActiveDocument.Bookmarks("Section_2").Range.Font.Hidden = True
            ActiveDocument.Bookmarks("Header_Section_2").Range.Font.Hidden =True   
        End If
    End Sub

    Wednesday, September 17, 2014 3:10 PM

All replies

  • Hi Andres,

    What is the Word version?

    What is the header setting?

    >>the first page has always the header of the section before. <<

    I'm not quite sure what the issue is. Would you mind uploading a simple document through OneDrive and show the steps or providing any figures?

    As I researched, if all sections in a document are hidden, Word will add a new blank page since there is nothing displayed in this document.

    Regards,

    George.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 9, 2014 6:41 AM
    Moderator
  • Hi George,

    thank you for your help. I use Word 2010.

    The header setting is:

    1) No connected to section before.

    2) In a section first page the same as all others.

    Here is the link for an example Word file:

    https://onedrive.live.com/redir?resid=D2F7F959B7769697!106&authkey=!ACExqkrqoBgtqAQ&ithint=file%2cdocm

    Let me know if you got it.

    I want to be able to show or hide sections with the corresponding headers of each section. For that I use check boxes that are placed in section 1.   

    What happens when I switch on a section is that, when this section appears, the first header always takes the header of the section before. Even If I switch off section 2, 3 and 4 using the check boxees shown on the section 1 then a second page is seen in the document showing the header of section 2.

    May be there is an issue about where the next section starts?

    Thank you

    Andres

    Tuesday, September 9, 2014 8:08 AM
  • Dear George,

    here is a link to OneDrive that shows what exactly is the issue.

    "https://onedrive.live.com/redir?resid=D2F7F959B7769697!113&authkey=!AC_CN4G1QkITmkY&ithint=file%2cdocx"

    The link is in quotes since MSDN won't allow me to place images or links until they are able to verify my account, for which I don't know if action is required from my side.

    Looking forward to your reply!

    Best regards,

    Andres

    Wednesday, September 10, 2014 10:12 AM
  • Salü Andres

    Is the Section Break (at the end of the section of text being hidden) formatted as "hidden" when this occurs? If you doe NOT format it as hidden does that make a difference?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 10, 2014 6:00 PM
    Moderator
  • Salü Cindy,

    I am not quite sure if it is hidden. Do you mean the section break at the end of the section that I want to hide, if that one is being hidden or the one before?

    Yesterday I was trying to use MoveStart and  MoveEnd, which was funny because I had it from an answer from you to a post to Steve Richter on November 18th 2012.

    But it reacts strange. The code is:

            Selection.MoveStart wdCharacter, -2
            Selection.MoveEnd wdCharacter, -1

    It looks fine when I hide the desired section. But when I unhide it again and I hide it the next time it reacts like the section would grow more and more in direction to the start of the document. If I don't care at the end it hides the "hide/unhide"-check boxes that I have in section 1! And it seems to be irreversible if I save the document.

    I went then and studied what happens exactly and found that the first time the Selection.Start-position jumps only 2 wdCharacter-steps back as expected. After unhiding the section, and selecting the section again the Selection.Start is still on the correct position like at the beginning. When executing now " Selection.MoveStart wdCharacter, -2"  it jumps now 4 wdCharacter-steps back or even more, from 95 back to 62! When it jumped back to 62 I tried to do a loop with  " Selection.MoveStart wdCharacter, 1" to see if I were able to reach the desired position of 94 but it went straight to 96 and when I went stepwise back with "Selection.MoveStart wdCharacter, -1"  to come down to 94 it jumps immediately to 93. It seems that position 94 and 95 are not reachable anymore. I can jump the whole day between 93 and 96 if I like!

    I imagine that the explanation above could give you a feeling if the section break 1 is being formatted as hidden.

    Contrary to that the "Selection.MoveEnd wdCharacter, -1" never makes problems.

    Thank you for your help!

    Regards

    Andres


    Thursday, September 11, 2014 6:37 AM
  • Dear Cindy

    There is also an observation with the docm-document when seen in Word after I applied the Hiding routine that uses the "Selection.MoveStart"-function. As I show in the link below, 

    „https://onedrive.live.com/redir?resid=D2F7F959B7769697!115&authkey=!AIO4tMyNL8fzfYc&ithint=file%2cpptx“

    the first section break now reacts different when activating "Show Paragraph marks" on the Menu "Paragraph":

    •  when "Show Paragraph marks" is not activated the text of page 1 of section 2 seems to be in section 1.
    • when "Show Paragraph marks" is activated the text of page 1 of section 2 jumps to the second page (which is section 2), which is the correct position. 

    It's strange that clicking "Show Paragraph marks" makes the text to be displayed on a different position than when paragraphs marks are not shown.

    Somehow the behaviour of the section break was changed by using "Selection.MoveStart wdCharacter -1" and applying thereafter the hiding of selection 2. Can you help me understand what happens?

    Best regards,

    Andres

    Thursday, September 11, 2014 8:25 AM
  • Hoi Andres

    <<Do you mean the section break at the end of the section that I want to hide, if that one is being hidden or the one before?>>

    Yes. The break always follows the section it controls - the last section break in the document (and the one that's in any new document) is part of the last paragraph mark.

    As to moving the Selection a certain number of characters: If you have anything other than "plain text" in the document - and you do, since you mention checkboxes - then how/where the Selection moves can be affected by the behind-the-scenes code that manages such things. At this point, I have no idea WHY things are behaving as they are, I can just say what may be a reason.

    If I understand correctly, your purpose is to hide checkboxes? And it seems these are ActiveX checkboxes (not form fields or content controls)? If yes, the Checkbox object should have a Visible property. Alternately, you could try creating a Character style (that looks just like the font in the paragraph style you're using), apply that to the checkboxes and then change the Hidden property of the STLYE to False. In this manner, you don't have to worry about what is selected.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 12, 2014 7:00 PM
    Moderator
  • Hoi Cindy,

    thank you for your answer! Now I am surprised because "Hoi" is used in Switzerland!

    I don't want to hide only checkboxes. I need to hide sections that deal with different type of technical issues. The real goal is to facilitate sales people and customers answering in a correct and comprehensive manner questions to technical issues they have, without killing immediately their motivation with too detailed questionaires. I only want to make them see the section of the technical area they want a solution from us, but being able to show or hide other sections if they need them as well.   

    I think the path with       

    Selection.MoveStart wdCharacter, -2

    Selection.MoveEnd wdCharacter, -1

    before  Selection.Font.Hidden = True/False

    has a probability to help solving the issue. However for the second section, I need to use "Selection.MoveStart wdCharacter,-1" while for the other sections 3 and 4 I need to use "Selection.MoveStart wdCharacter,-2" which I don't know why. If I do it this way and I show thereafter all the sections and then I start to hide first section 4, then 3, then 2 it does what I want: I see only section 1 and I am able to show one desired section if I click on a checkbox. But if I alter the order of hiding, then blank pages appear again and it doesn't work as I expect. I am near to loosing the confidence to find a solution to this task.

    What I experienced in this forum is that it's important to explain in a comprehensive manner the problem. May be there are some tips because it must be really difficult for someone that is not dealing with exact the same problem to understand the issue someone else is posting.

    Best regards,

    Andres

    Sunday, September 14, 2014 5:10 PM
  • Dear All,

    This very easy way is how I solved it to hide or unhide sections together with their corresponding headers. Just creating a bookmark "Header_Section_2" with the header of section 2 and another bookmark "Section_2" and hiding them together. The bookmark "Section_2" contains section 2 together with the section break before section 2. The same I applied for all the other sections. My fault was probably to think of a word document as a linear object. A bookmark of a section does not contain the header because that one is somehow in a different "plane" of the document.  

    Regards,

    Andres

    Here is the code that is placed in the change-procedure of the ActiveX-checkbox "ChB_Show_Section2".

    Private Sub ChB_Show_Section2_Change()

        If ChB_Show_Section2.Value = True Then
            ActiveDocument.Bookmarks("Section_2").Range.Font.Hidden = False
            ActiveDocument.Bookmarks("Header_Section_2").Range.Font.Hidden = False
        Else
            ActiveDocument.Bookmarks("Section_2").Range.Font.Hidden = True
            ActiveDocument.Bookmarks("Header_Section_2").Range.Font.Hidden =True   
        End If
    End Sub

    Wednesday, September 17, 2014 3:10 PM