Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 03-03-2009, 04:24 AM
shanew shanew is offline
Spammer
 
Join Date: 25 Feb 2009
Location: India
MS Office Version:Access
Posts: 32
shanew is becoming part of the community
Looping macros to add text to beginning and end of a paragraph

Please Register to Remove these Ads

Hi friends,

I want to convert word documents to HTML and need to insert appropriate paragraph styles throughout. I found a macro to help me to add text to the beginning of the paragraph, but having difficulty getting it to do the same for the end. What can I add so that at the end of each paragraph formatted with Heading 2, it will insert </h1> before the paragraph mark?

'
Code:
This example inserts "<h1>" at the beginning of
' every paragraph formatted with the Heading 2 style.
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleHeading2
'The Do...Loop statement repeats a series of
' actions each time this style is found.
Do While .Execute(Forward:=True, Format:=True) = True
With .Parent
'If the found text is the last
' paragraph in the document...
If .End = ActiveDocument.Content.End Then
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "<h1>"
Exit Do
'If the found text is *not* the last
' paragraph in the document...
Else
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "<h1>"
.Move Unit:=wdParagraph, Count:=1
End If
End With
'Goes back to the beginning of the Do...Loop statement.
Loop
End With
Reply With Quote
  #2  
Old 04-18-2009, 05:26 AM
ExlGuru's Avatar
ExlGuru ExlGuru is offline
Forum Administrator
 
Join Date: 17 Mar 2009
Location: India
MS Office Version:2003,2007
Posts: 222
ExlGuru is becoming part of the community
Re: Looping macros to add text to beginning and end of a paragraph

Use this code:
Code:
Dim drange As Range
Selection.HomeKey wdStory
Selection.Find.Style = "Heading 2"
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set drange = Selection.Range
drange.InsertBefore "<h1>"
drange.End = drange.End - 1
drange.InsertAfter "<\h1>"
Selection.Collapse wdCollapseEnd
Loop
End With
Hope this helps
__________________
ExlGuru
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump