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="Arial">^&</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="Verdana">^&</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="2">^&</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="3">^&</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
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="Arial">^&</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="Verdana">^&</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="2">^&</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="3">^&</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