Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2014, 06:04 AM
Tommes93 Tommes93 is offline go to next heading, if there is one Windows 7 64bit go to next heading, if there is one Office 2007
Novice
go to next heading, if there is one
 
Join Date: Mar 2014
Posts: 9
Tommes93 is on a distinguished road
Default go to next heading, if there is one

Hi as in the title, my code is going through a word document, heading by heading.
and i need to know if the next heading is the last heading, because with every heading i am opening a <case> in XML and i need to close them at the end</case>.
So if you have an idea, let me know.
thanks!
Reply With Quote
  #2  
Old 04-01-2014, 01:38 AM
macropod's Avatar
macropod macropod is offline go to next heading, if there is one Windows 7 32bit go to next heading, if there is one Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,341
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Is this for a particular Heading Style, or for any Heading Style?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-01-2014, 01:42 AM
Tommes93 Tommes93 is offline go to next heading, if there is one Windows 7 64bit go to next heading, if there is one Office 2007
Novice
go to next heading, if there is one
 
Join Date: Mar 2014
Posts: 9
Tommes93 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Is this for a particular Heading Style, or for any Heading Style?
For any.
And an other question: how do i select a header, to print it somewhere else?

i used to write
Selection.GoTo what:=wdGoToHeading, which:=wdGoToNext
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
but what if the header has two or three lines?
Do i need to take "Selection."?
Thanks
Reply With Quote
  #4  
Old 04-01-2014, 05:57 AM
macropod's Avatar
macropod macropod is offline go to next heading, if there is one Windows 7 32bit go to next heading, if there is one Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,341
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The basic tagging can be done with Find/Replace code like:
Code:
Sub TagHeadings()
Dim i As Long
For i = 1 To 9
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = True
      .MatchWildcards = False
      .Style = "Heading " & i
      .Execute
    End With
    Do While .Find.Found
      .InsertBefore "<case>"
      .Characters.Last.InsertBefore "</case>"
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
End Sub
As for printing somewhere else, there's no need to select anything. In the above code, for example, you could output each heading to another location by adding code within the Do While ... Loop. That would output the headings by level, not by order within the document. The simplest method for outputting them in order, is to add a Table of Contents to the document - right where you want them to appear.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-01-2014, 10:39 AM
fumei fumei is offline go to next heading, if there is one Windows 7 64bit go to next heading, if there is one Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Code:
Do While .Find.Found
.InsertBefore "<case>"
.Characters.Last.InsertBefore "</case>"
.Collapse wdCollapseEnd
.Find.Execute
Loop
Would that not be InsertAfter? Or am I missing something....again.
Reply With Quote
  #6  
Old 04-01-2014, 03:45 PM
macropod's Avatar
macropod macropod is offline go to next heading, if there is one Windows 7 32bit go to next heading, if there is one Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,341
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Gerry,

I was intentionally inserting the "</case>" into the end of the heading paragraph, rather than into the start of the one after it; otherwise, I'd have just used .InsertAfter without reference to .Characters.Last.

I also deliberately implement a loop in anticipation that the OP might want to add some other content in or around either or both of the "<case>" and "</case>" strings (e.g. the heading level and/or the index # of that heading). It would actually be more efficient to use ReplaceAll for what has been specified.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
go to next heading, if there is one How to add space between heading number and heading text Dr Wu Word 4 07-15-2018 03:52 AM
go to next heading, if there is one How to Restore Heading 1, Heading 2, etc. within a Word Document cheech1981 Word 9 01-11-2017 02:14 AM
go to next heading, if there is one Heading title before heading number johnor999 Word 4 12-06-2013 12:39 AM
Control the size of space between heading number and heading text Dr Wu Word 1 07-17-2013 12:24 PM
go to next heading, if there is one Styles: Heading 4 stuck at same heading number Hallet Word 1 05-31-2012 02:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft