Looping macros to add text to beginning and end of a paragraph

P

pachmarhi

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?

'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
 
G

Greg Maxey

Something like this should work for the leading and trailing text:

Sub ScratchMacro()
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = "Heading 2" Then
Set oRng = oPar.Range
oRng.Collapse wdCollapseStart
oRng.Text = "<h1>"
Set oRng = oPar.Range
oRng.Collapse wdCollapseEnd
If Not oRng.End = ActiveDocument.Range.End - 1 Then
oRng.MoveEnd wdCharacter, -1
End If
oRng.Text = "</h1>"
End If
Next oPar
End Sub
 
D

Doug Robbins - Word MVP

Use

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.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg Maxey

Use

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.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Show quoted text -

Doug,

This works great provided (and I don't know why it would be) a Heading
2 paragraph isn't the last paragraph. If it is then the thing gets
caught up in a endless loop.
 
D

Doug Robbins - Word MVP

Hi Greg,

That would be a strange place to have a heading. However, the following
will cope with it

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
If drange.End <> ActiveDocument.Range.End Then
drange.InsertBefore "<h1>"
drange.End = drange.End - 1
drange.InsertAfter "<\h1>"
Selection.Collapse wdCollapseEnd
Else
drange.InsertBefore "<h1>"
drange.End = drange.End - 1
drange.InsertAfter "<\h1>"
Exit Do
End If
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Use

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.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Show quoted text -

Doug,

This works great provided (and I don't know why it would be) a Heading
2 paragraph isn't the last paragraph. If it is then the thing gets
caught up in a endless loop.
 
G

Greg Maxey

Doug,

True. Should we now engage in an endless debate on which method is quicker
(or faster) yours or mine ;-)
 
D

Doug Robbins - Word MVP

My experience would suggest that using find gives a more expedient result
that interating through each of the paragraphs in a document.

Better we debate why it would be illogical to end a document with a
paragraph formatted with a heading style.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

pachmarhi

Great help guys. I opted for Doug's amended version below, but having one
small problem. I need to do the same with another paragraph style which needs
the following added at the start of the paragraph:
<p class="intro">
How do I get those quotation marks in, given that in the macro they are
needed for the whole of that insertion?

Thanks again
 
G

Greg Maxey

Use:

"<p class=" & Chr(32) & "intro" & Chr(32) & ">"

and

"<\p class=" & Chr(32) & "intro" & Chr(32) & ">"

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



pachmarhi said:
Great help guys. I opted for Doug's amended version below, but having one
small problem. I need to do the same with another paragraph style which
needs
the following added at the start of the paragraph:
<p class="intro">
How do I get those quotation marks in, given that in the macro they are
needed for the whole of that insertion?

Thanks again
 
P

pachmarhi

Perfect. Thanks. This is going to save me a huge amount of time.

Of course for HTML markup I don't need to put the class in the <\p>

Greg Maxey said:
Use:

"<p class=" & Chr(32) & "intro" & Chr(32) & ">"

and

"<\p class=" & Chr(32) & "intro" & Chr(32) & ">"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top