Hello.
Is there a simple way to navigate all the Heading1 and all the Heading2 styles? I could investigate using GoTo but are there collections that I can iterate?
Basically, I'm going to expand all H1 but collapse all H2.
Navigate Heading1 and Heading2 styles
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Navigate Heading1 and Heading2 styles
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
Hey I impress myself sometimes ;)
(Mine seems a lot easier than code like here.)
I'd still like to know if there is a way to navigate all the headings 1/2 rather than all the paragraphs?
Code: Select all
Sub ExpandAndCollapse12()
Dim doc As Document
Dim para As Paragraph
Set doc = ActiveDocument
Application.ScreenUpdating = False
For Each para In doc.Paragraphs
If para.Style = "Heading 1" Then
para.CollapsedState = False
ElseIf para.Style = "Heading 2" Then
para.CollapsedState = True
End If
Next para
Application.ScreenUpdating = True
End Sub
I'd still like to know if there is a way to navigate all the headings 1/2 rather than all the paragraphs?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Navigate Heading1 and Heading2 styles
Hi Andy,
Word has a few of these features to navigate Heading styles...
Two that I like to use are:
1. The Navigation Pane (View Ribbon, Navigation Pane Check Box)
- Make sure you select Headings under the Search Box
- Right clicking in the naviagtion pane allows you to collase/expand to view different levels of heading
- Click the headings to navigate to those locations in the document
2. Outline View (View Ribbon, Outline)
- This is a special view to not only outline the document, (marking the headings by premote/demote), but you can use the Show Levels option to collapse down to any level and summarize the document or make it easier to navigate. You can even move headings and their associates paragraphs to new positions using the up/down arrows.
Word has a few of these features to navigate Heading styles...
Two that I like to use are:
1. The Navigation Pane (View Ribbon, Navigation Pane Check Box)
- Make sure you select Headings under the Search Box
- Right clicking in the naviagtion pane allows you to collase/expand to view different levels of heading
- Click the headings to navigate to those locations in the document
2. Outline View (View Ribbon, Outline)
- This is a special view to not only outline the document, (marking the headings by premote/demote), but you can use the Show Levels option to collapse down to any level and summarize the document or make it easier to navigate. You can even move headings and their associates paragraphs to new positions using the up/down arrows.
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
Thank you. Yes, there are a number of ways ;)
I tried recording using the Styles Pane, selecting all instances of Heading 2, and Collapse Heading, but, given that this is Office 2013, nothing was recorded.
I tried GoTo with wdGoToHeading, but how would I tell that I've reached the last heading in the document? (Added: I might have to use the Browser Object like here.)
The idea is, although I'm happy with my version, for a huge document it may be better to navigate between headings rather than paragraphs. I'm thinking though, if I'm using GoTo and probably Selection, would this end up being slower than just iterating all paragraphs?
Anyway, I'm happy with my solution, but would still be interested in knowing a way to navigate headings, programmatically.
I tried recording using the Styles Pane, selecting all instances of Heading 2, and Collapse Heading, but, given that this is Office 2013, nothing was recorded.
I tried GoTo with wdGoToHeading, but how would I tell that I've reached the last heading in the document? (Added: I might have to use the Browser Object like here.)
The idea is, although I'm happy with my version, for a huge document it may be better to navigate between headings rather than paragraphs. I'm thinking though, if I'm using GoTo and probably Selection, would this end up being slower than just iterating all paragraphs?
Anyway, I'm happy with my solution, but would still be interested in knowing a way to navigate headings, programmatically.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Navigate Heading1 and Heading2 styles
You can use Selection.GoTo to loop through heading paragraphs, but that won't do what you want since it skips headings that are invisible because a higher-level heading has been collapsed.
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
Thanks Hans.
Yes, I was noticing that and perhaps the Browser Object is a way around it. (All headings can be expanded first with a single command (ExpandAllHeadings), but the GoTo still seems unreliable/inconsistent.)
It's a shame the select all instances option from the Styles Pane doesn't translate to anything.
But never mind. I'm happy with my solution and that I am not missing anything obvious.
Andy
Added: Actually, I could use Find rather than GoTo, and it is easier to stop the search from cycling back to the beginning of the document. But using Find and Selection I am suspicious that it may not improve upon just iterating every paragraph. I'll leave it here :)
Yes, I was noticing that and perhaps the Browser Object is a way around it. (All headings can be expanded first with a single command (ExpandAllHeadings), but the GoTo still seems unreliable/inconsistent.)
It's a shame the select all instances option from the Styles Pane doesn't translate to anything.
But never mind. I'm happy with my solution and that I am not missing anything obvious.
Andy
Added: Actually, I could use Find rather than GoTo, and it is easier to stop the search from cycling back to the beginning of the document. But using Find and Selection I am suspicious that it may not improve upon just iterating every paragraph. I'll leave it here :)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
Well the performance was slow so I was persuaded to continue:
This is significantly faster. (It could be started by first ExpandAllHeadings.)
Code: Select all
Sub Macro1()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do Until Selection.Find.Execute = False
Selection.Paragraphs(1).CollapsedState = True
Loop
Application.ScreenUpdating = True
End Sub
Last edited by agibsonsw on 12 Feb 2016, 16:32, edited 1 time in total.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Navigate Heading1 and Heading2 styles
Fine, but that doesn't do anything with Heading 1 paragraphs...
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
I just added a note at the bottom a minute ago ;) ActiveDocument.ActiveWindow.View.ExpandAllHeadings could be used, or a second version of the code to expand all Heading 1s.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 5StarLounger
- Posts: 629
- Joined: 10 Jan 2016, 15:56
- Location: Madison, Wisconsin
Re: Navigate Heading1 and Heading2 styles
It might help people in assisting you if we knew of the reason you want to do this in code rather than by using the Navigation Pane or Outline view. What would you accomplish doing this?
By the way, while the macro recorder can be useful, it will not pick up many actions that can be done manually in Word, especially those involving the mouse. Also, the code created by the recorder is seldom ideal.
http://word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htmCreating a macro with no programming experience using the recorder
By the way, while the macro recorder can be useful, it will not pick up many actions that can be done manually in Word, especially those involving the mouse. Also, the code created by the recorder is seldom ideal.
http://word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htmCreating a macro with no programming experience using the recorder
Attorney Charles Kenyon
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Navigate Heading1 and Heading2 styles
It wasn't me who wanted to achieve this, I was helping someone. To clarify, I have already pointed out to them a number of alternatives.
If it is of any interest, I subsequently discovered that they want to run this process while giving a presentation. Why they would want to (attempt to) manipulate such a long document during a presentation is beyond me.
But "the problem is sol-ved" (Clouseau).
If it is of any interest, I subsequently discovered that they want to run this process while giving a presentation. Why they would want to (attempt to) manipulate such a long document during a presentation is beyond me.
But "the problem is sol-ved" (Clouseau).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.