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