Automating Word using range objects

P

Peter

Hi,
I'm trying to automate word to insert tags before and after formating. For
example: a bold line, would end up like this: <B>a bold line<P>
I'm using the range object to do this and following code works. However
there must be a more efficient way of doing this? Like setting the range to
all bold formatted text only and then using the methods like insertbefore.
Whilst the find method is ok, it occaisonally gets caught in a loop (hence
the MOVE property) and behaves weird the odd time depending on the format of
the doc. The doc always comes in a table.

Dim rng As Range
Set rng = ActiveDocument.Range()

With rng.Find
' bold
rng.StartOf wdStory
.Font.Bold = True
.Execute FindText:="", Format:=True
While .Found
rng.InsertBefore "<B>"
rng.InsertAfter "<P>"
rng.Font.Bold = False
rng.Move Unit:=wdCharacter, Count:=2
.Font.Bold = True
.Execute FindText:="", Format:=True, Forward:=True
Wend
.Font.Bold = False
' italics
rng.StartOf wdStory
.Font.Italic = True
.Execute FindText:="", Format:=True, Forward:=True
While .Found
rng.InsertBefore "<I>"
rng.Font.Italic = False
rng.InsertAfter "<P>"
rng.Move Unit:=wdCharacter, Count:=2
.Execute FindText:="", Format:=True, Forward:=True
Wend
.Font.Italic = False
 
K

Klaus Linke

I'm trying to automate word to insert tags before and after formating. For
example: a bold line, would end up like this: <B>a bold line<P>


Hi Peter,

Looks like Quark?

You can replace "bold" with <B>^&<P> (("not bold")), using "ReplaceAll".
One thing to be careful about is searching all StoryRanges (... if there may
be footnotes, headers, ...).

Greetings,
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