Help with Find and Replace Function

D

dvdastor

Hi All,

I've been scouring the Message Boards for a solution to this and it
seems like others have had similar problems. I've applied various
fixes I have found from these messages, but I still seem to have an
issue.

I am creating a Function in a VB.NET app which calls the Word Object.
(I've created it as a Macro in Word and it works just fine.)

My function looks through a selection and applies various formatting
changes within that selection. As you can see below, I need to look
for bolding, font names and font sizes. The Bolding and Font Name
replacements work flawlessly. However, when it gets to the "Font Size"
replace block, the range collapses to the beginning during the Execute
and the formatting is applied to the entire document, not just my
selection. The "Font Size" block is set up just like the others, so
I'm not sure what is wrong.

Can anyone suggest something to help?
------------------------------
Public Function ReturnString(ByVal iStart As Integer, ByVal iEnd As
Integer) As String

Dim rngResult As Word.Range
Dim myRange As Word.Range
Dim cChars As Long
Dim rngStartMarker As Word.Range
Dim _activeSelection As Word.Selection

Dim iParaCount As Integer
Dim myPara As Word.Paragraph

rngResult = oDoc.Range
myRange = rngResult.Duplicate

With myRange
.Start = iStart
.End = iEnd
End With

If myRange.Characters.First.Text = vbCr Then
Do
myRange.MoveStart(Word.WdUnits.wdCharacter, 1)
Loop Until Not myRange.Characters.First.Text = vbCr
End If

If myRange.Characters.Last.Text = vbCr Then
Do
myRange.MoveEnd(Word.WdUnits.wdCharacter, -1)
Loop Until Not myRange.Characters.Last.Text = vbCr
End If


myRange.Select()

_activeSelection = oDoc.ActiveWindow.Selection

myRange = _activeSelection.Range
With _activeSelection.Find
.ClearFormatting()
.Font.Bold = 1
.Replacement.ClearFormatting()
.Replacement.Font.Bold = 0
.Text = ""
.Replacement.Text = "<b>^&</b>"
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
_activeSelection.Collapse()
End With

myRange.Select()
_activeSelection = oDoc.ActiveWindow.Selection
myRange = _activeSelection.Range


With _activeSelection.Find
.ClearFormatting()
.Font.Name = "Arial"
.Replacement.ClearFormatting()
.Text = ""
.Replacement.Text = "<font
face=&quot;Arial&quot;>^&</font>"
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
_activeSelection.Collapse()
End With

myRange.Select()
_activeSelection = oDoc.ActiveWindow.Selection
myRange = _activeSelection.Range


With _activeSelection.Find
.ClearFormatting()
.Font.Name = "Verdana"
.Replacement.ClearFormatting()
.Text = ""
.Replacement.Text = "<font
face=&quot;Verdana&quot;>^&</font>"
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
_activeSelection.Collapse()
End With

myRange.Select()
_activeSelection = oDoc.ActiveWindow.Selection
myRange = _activeSelection.Range

'Everything so far is fine up to this point

With _activeSelection.Find
.ClearFormatting()
.Font.Size = 10
.Replacement.ClearFormatting()
.Text = ""
.Replacement.Text = "<font size=&quot;2&quot;>^&</font>"
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)

'After this Execute, the Range somehow becomes collapsed and the
formatting has been applied to the entire document, not just the
selection.

_activeSelection.Collapse()
End With

myRange.Select()
_activeSelection = oDoc.ActiveWindow.Selection
myRange = _activeSelection.Range


With _activeSelection.Find
.ClearFormatting()
.Font.Size = 12
.Replacement.ClearFormatting()
.Text = ""
.Replacement.Text = "<font size=&quot;3&quot;>^&</font>"
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
_activeSelection.Collapse()
End With

myRange.Select()
_activeSelection = oDoc.ActiveWindow.Selection
myRange = _activeSelection.Range



End Function
 
C

Cindy M -WordMVP-

Hi (e-mail address removed),
I am creating a Function in a VB.NET app which calls the Word Object.
(I've created it as a Macro in Word and it works just fine.)

My function looks through a selection and applies various formatting
changes within that selection. As you can see below, I need to look
for bolding, font names and font sizes. The Bolding and Font Name
replacements work flawlessly. However, when it gets to the "Font Size"
replace block, the range collapses to the beginning during the Execute
and the formatting is applied to the entire document, not just my
selection. The "Font Size" block is set up just like the others, so
I'm not sure what is wrong.
You don't mention which version of Word is involved here. Nor do you say
whether you're trying to search directly applied font formatting (vs.
formatting applied by a style).

I ran into a problem like this a while back that was due to how font
sizes were being written for styles, in the background. Ever since Word
supports right-to-left, there are at least two places where font
information is saved, and some of it's not written to the right-to-left
property. And in the problem versions, Find/Replace looks at BOTH
left-to-right and right-to-left. If it doesn't match both, Find won't
stop there.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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