2
votes

I need help, and I think this is pretty easy if you know VBA:

I want to have a button or dropdown menu that expands/collapses specific menu headings.

Here is an outline of the layout of the document (there are menus and data within the Department levels as well, but I don't need to collapse/expand those levels):

- Daily Processes

  • Department A
  • Department B
  • Department C
  • Department D

- Weekly Processes

  • Department A
  • Department B
  • Department C
  • Department D

- Monthly Processes

  • Department A
  • Department B
  • Department C
  • Department D

- Annual Processes

  • Department A
  • Department B
  • Department C
  • Department D

- Appendix

I want there to be buttons for each department, hiding all other departments. For example, if I click Department A, all Dept B, C, D menus will collapse, only showing Dept A.

I would imagine these would be the buttons that I would need:

  1. ExpandAll
  2. ShowDeptA
  3. ShowDeptB
  4. ShowDeptC
  5. ShowDeptD

This is the pseudocode for one dept that I think would work:

  1. ExpandAll
  2. Start at top of Document
  3. Search Format=Heading2
  4. If Heading2=DepartmentA: expand heading, Else: collapse heading

What I have so far: (All I need the command to collapse headings)

Sub OpenDeptA()
'
' OpenDeptA Macro
' Show only DeptA sections
'
'    Expand all menus
    ActiveDocument.ActiveWindow.View.ExpandAllHeadings
'    Move cursor to the top
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Selection.HomeKey Unit:=wdStory
'    Find first menu using format: Heading 2
    Selection.Find.Style = ActiveDocument.Styles("Heading 2")
    Selection.Find.Execute
'    Loop through document collapsing heading if not equal to "DeptA"
Do Until Selection.Find.Found = False
    If Selection.Text = "DeptA" Then
        Selection.Find.Style = ActiveDocument.Styles("Heading 2")
        Selection.Find.Execute
    Else: Selection.CollapseHeading  ***NOT SURE HOW TO COLLAPSE MENUS***
        Selection.Find.Style = ActiveDocument.Styles("Heading 2")            
        Selection.Find.Execute
    End If
Loop
End Sub
1
the thing is that we don't write code for free here. We provide support if you have already tried something on your own. So, have you?Kazimierz Jawor
you can always re-edit your question instead of adding comments which are difficult to read.Kazimierz Jawor
@KazJaw, thanks. I updated my original post.acfest
can anyone help me on this?acfest
the thing is that you are wrong in your first sentence in your question where you write: I think this is pretty easy if you know VBA. Indeed, it isn't so easy...Kazimierz Jawor

1 Answers

0
votes

I don't have Word 2013, so I can't provide a complete answer. But I can help with two aspects of what you are doing.

  1. Comparing text. Right now you are comparing just words "Dept A", but it is likely that the actual selection contains the line break as well. You could try

    Selection.Text = "DeptA" & vbCR
    

    or

    Selection.Text Like "DeptA*"
    

    It depends a bit on how tightly controlled those headings are.

  2. Collapsing the content. I assume you are already putting the document into Outline View. If so, the command is

    ActiveWindow.View.CollapseOutline Selection.Range

    the thing about CollapseOutline, though, is that it only collapses by one level. So if you had a Heading 3 under your Heading 2, that would be collapsed but your Heading 2 would not be. From what I can tell there's no harm in running CollapseOutline several times, so I'd work out ahead of time how deeply the levels in your document go and then run the method enough times to accommodate that. Ok, revisiting this with a copy of Word 2013 in front of me. There doesn't seem to be a method for collapsing the text; instead, you set its collapsed state to true. And, it works on paragraphs, not ranges or selections. So here is the final portion of your code, with the collapsing code included:

    Do Until Selection.Find.Found = False
        If Selection.Text Like "Department A*" Then
            Selection.Find.Style = ActiveDocument.Styles("Heading 2")
            Selection.Find.Execute
        Else: Selection.Paragraphs(1).CollapsedState = True
            Selection.Find.Style = ActiveDocument.Styles("Heading 2")
            Selection.Find.Execute
        End If
    Loop
    

As to your question about Word Commands that you can see in the Macros list, you can run these, but their real power lies in the ability to recode them to do what you prefer. In this case what you need to do is supported by the object model so I wouldn't bother with them.

Lastly, if you intend to do a lot of VBA coding, I'd recommend getting familiar with the Object Browser (F2 from the VB Editor). You can search for something you think might exist there; for instance, in looking into this question I searched for "Collapseheading". In this case that wasn't fruitful on its own, but it did put me in the neighborhood of CollapsedState.

Hope this helps.