Ranges, Finds, and Replacements....oh my!! (please help)

D

dvdastor

Hi All,

I am attempting to understand Word Programming, but I have run into a
snag on a project I am working on.

A little background:

I have created a document that contains pre-set tags that users then
supply text for. Example:

[~TagName~]User supplied information
[~AnotherTag~]More information

What I have done in my code is move the starting and ending points of
the document range to get the tag name and then the tag value. After it
finds a match, it moves on, etc... This part is no problem and it works
just fine.

Certain tag values will have formatting based on what the user types
in. So if the user types "My name is Fred", and Fred is in Bold Text
12pt Arial, my ending value has to read "My name is <font face="arial"
size="3"><b>Fred</b></font>.

My issue has to do with the font names and sizes. My range stays as it
should when I apply the <b></b> tags, but for a reason I can't
understand, when the font name and/or size find and replace occurs, it
blows away the range and formats the entire document all the way to the
end. I only need it to format that one tag value though.


Below is part of the macro that I am using to do this:

Sub FormatTag()
Dim rngResult As Range
Dim myRange As Range

Set rngResult = ActiveDocument.Range
Set myRange = rngResult.Duplicate

With myRange
.Start = 13
.End = 1272
End With

myRange.Select 'so I can see what is happening on screen

With myRange.Find
.ClearFormatting
.Font.Bold = 1
.Replacement.ClearFormatting
.Replacement.Font.Bold = 0
.Execute findtext:="", ReplaceWith:="<b>^&</b>",
Format:=True, Replace:=Word.WdReplace.wdReplaceAll
End With

With myRange.Find
.ClearFormatting
.Font.Name = "Times New Roman"
.Replacement.ClearFormatting
.Execute findtext:="", ReplaceWith:="<font face=&quot;Times
New Roman&quot;>^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll
End With

With myRange.Find
.ClearFormatting
.Font.Size = 10
.Replacement.ClearFormatting
.Execute findtext:="", ReplaceWith:="<font
size=&quot;2&quot;>^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll
End With

With myRange.Find
.ClearFormatting
.Font.Name = "Arial"
.Replacement.ClearFormatting
.Execute findtext:="", ReplaceWith:="<font
face=&quot;Arial&quot;>^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll
End With




End Sub

Any help anyone can lend would be great.
 

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