Selection is ignored and formatting applied to entire document.

D

dvdastor

Hi All,

I am starting to get the hang of this Word Automation thing, but this
issue has me puzzled to no end.

I have the following text in my document:

[~Alternative~]by installing SQL Server 2000 as a named instance on the
same computer<Shift+Enter>
[~CorrectAnswer~]by installing SQL Server 2000 as a default instance on
another computer<Enter>

I am setting my range to include just the first line, but only the text
between the "]" and the line break. I am then attempting to apply
formatting to that text. If the text contains Arial font, I need it to
look like the following:

<font face='Arial'>by installing SQL Server 2000 as a named instance on
the same computer</font>

However, whenever I run my macro, it ignores my selection and applies
the formatting to the entire thing like so:

[~Alternative~]<font face=&quot;Arial&quot;>by installing SQL Server
2000 as a named instance on the same computer<Shift+Enter>
[~CorrectAnswer~]by installing SQL Server 2000 as a default instance on
another computer</font><Enter>.

Here is the macro I am using:

Public Function ReturnString()

Dim rngResult As Range
Dim myRange As Range

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

With myRange
.Start = 15
.End = 85
End With

myRange.Select

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

myRange.Select

End Function

Can anyone help with this?
 
J

jay

I'm not sure, but your're searching within the whole doc, right?
you are searching for what? empty string? ( .Execute findtext:="",
ReplaceWith:="<font ...)

It would make sense that the empty string you're searching for is found
either everywhere or nowhere...
 
D

dvdastor

Jay, thanks for the followup..

I am only searching for the formatted text within the range, not the
whole document. My range is only the characters 15-85 within the first
line. This can obviously change with the length of the line, but I am
not worried about that right now.

My search is looking for anything formatted with Arial font. The
..Execute findtext is left blank because I am looking for formatting
only. If I am using that incorrectly, please let me know.

Once I have my 70 characters, I only need to apply formatting to them.
However, it seems like the replacement is blowing through the 85th
character and going all the way to the end.
 
J

jay

Truthfully, I haven't used that myself.
It just looks suspicious.
Why don't you just display the found text in a messagebox or something,
as a sanity check?
Then, when you're really sure what you're finding, you can address the
replacement/formatting issue.
regards
Jay
 

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