Inserting a string at the end of a paragraph

A

alfonso gonzales

Hello Everybody,
I am making a macro to convert word pararaphs into html paragraphs.

So need to convert all the Word Paragraphs into HTML paragraphs. And then
purge the Word document of the Word paraqraph marks.
I can insert an opening marker <p> but seem to have a problem with inserting
the closing makrer.
so what I do is:

For Each Par in ActiveDocument.Paragraphs
Set myrange = Par.Range
myrange insertAfter "</p>"
Next

the problem with this is that the "</p>" gets inserted after the next
paragraph mark and not within the relevant paragraph. As a result it gets a
bit tough when I want to process these paragraphs further.


Any help apperciated.

alfonso gonzales
 
J

Jay Freedman

Hi Alfonso,

Don't try to handle this by inserting and deleting items individually.
Use the Find/Replace feature to replace all paragraph marks with the
combination </p><p>.

In the Replace dialog, you would put ^p in the Find What box and put
</p><p> in the Replace With box, and hit Replace All. Then you'll just
have to insert the <p> before the first paragraph and delete the extra
<p> at the end of the document.

In a macro, the code would look like this:

Sub foo()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = "</p><p>"
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With

Set oRg = ActiveDocument.Range
' insert tag at beginning of doc
oRg.InsertBefore "<p>"
' remove extra tag at end of doc
' (this works because the only ^p
' left in the doc is the one at
' the end)
With oRg.Find
.Text = "</p><p>^13"
.Replacement.Text = "</p>^13"
' all other settings stay as before
.Execute Replace:=wdReplaceAll
End With
End Sub
 
K

Klaus Linke

Hi Jay,

I wouldn't use ^13 in "Replace with"... it often inserts a character that looks like a paragraph mark, but doesn't work like one (= doesn't store the style and paragraph formatting). And the last paragraph mark even stores section and document formatting.

It might work, but just seems a bit risky ;-)

BTW, a wildcard replacement
Find what: ([!^13]@)(^13)
Replace with: <p>\1</p>\2
would also insert the tags.

Regards,
Klaus
 

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