D
dvdastor
Hi All,
In the code below, I am attempting to do multiple finds and replaces
for font formatting within a Range. I am successful in finding and
replacing the first occurrence, but anything after that fails. My
range shrinks somehow and I cannot seem to figure out why or how. I
have tried using the Duplicate method (not in the code below), but I
may have been doing this incorrectly. Can you please take a look and
make any suggestions? Thanks!!!!!!!!
----------------------------
Private Function ReturnAsString(ByVal inputRng As Word.Range) As String
Dim iParaCount As Integer
Dim myPara As Word.Paragraph
iParaCount = inputRng.Paragraphs.Count
For iParaCount = inputRng.Paragraphs.Count To 1 Step -1
myPara = inputRng.Paragraphs(iParaCount)
'myPara.Select 'debug info
If Len(myPara.Range.Text) <= 1 Then myPara.Range.Delete()
Next
If inputRng.Characters.First.Text = vbCr Then
inputRng.MoveStart(Word.WdUnits.wdCharacter, 1)
End If
If inputRng.Characters.Last.Text = vbCr Then
inputRng.MoveEnd(Word.WdUnits.wdCharacter, -1)
End If
With inputRng.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 inputRng.Find
.ClearFormatting()
.Font.Italic = 1
.Replacement.ClearFormatting()
.Replacement.Font.Italic = 0
.Execute(findtext:="", ReplaceWith:="<i>^&</i>",
Format:=True, Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Underline = Word.WdUnderline.wdUnderlineSingle
.Replacement.ClearFormatting()
.Replacement.Font.Underline = 0
.Execute(findtext:="", ReplaceWith:="<u>^&</u>",
Format:=True, Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Tahoma"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Tahoma">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Courier"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Courier">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Courier New"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Courier New">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Verdana"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Verdana">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Times New Roman"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font face="Times
New Roman">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Arial"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Arial">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 8
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="1">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 10
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="2">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 12
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="3">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll,
Wrap:=Word.WdFindWrap.wdFindContinue)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 16
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="4">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 18
.Execute(findtext:="", ReplaceWith:="<font
size="5">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 24
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="6">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 32
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="7">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
Return inputRng.Text
End Function
In the code below, I am attempting to do multiple finds and replaces
for font formatting within a Range. I am successful in finding and
replacing the first occurrence, but anything after that fails. My
range shrinks somehow and I cannot seem to figure out why or how. I
have tried using the Duplicate method (not in the code below), but I
may have been doing this incorrectly. Can you please take a look and
make any suggestions? Thanks!!!!!!!!
----------------------------
Private Function ReturnAsString(ByVal inputRng As Word.Range) As String
Dim iParaCount As Integer
Dim myPara As Word.Paragraph
iParaCount = inputRng.Paragraphs.Count
For iParaCount = inputRng.Paragraphs.Count To 1 Step -1
myPara = inputRng.Paragraphs(iParaCount)
'myPara.Select 'debug info
If Len(myPara.Range.Text) <= 1 Then myPara.Range.Delete()
Next
If inputRng.Characters.First.Text = vbCr Then
inputRng.MoveStart(Word.WdUnits.wdCharacter, 1)
End If
If inputRng.Characters.Last.Text = vbCr Then
inputRng.MoveEnd(Word.WdUnits.wdCharacter, -1)
End If
With inputRng.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 inputRng.Find
.ClearFormatting()
.Font.Italic = 1
.Replacement.ClearFormatting()
.Replacement.Font.Italic = 0
.Execute(findtext:="", ReplaceWith:="<i>^&</i>",
Format:=True, Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Underline = Word.WdUnderline.wdUnderlineSingle
.Replacement.ClearFormatting()
.Replacement.Font.Underline = 0
.Execute(findtext:="", ReplaceWith:="<u>^&</u>",
Format:=True, Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Tahoma"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Tahoma">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Courier"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Courier">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Courier New"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Courier New">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Verdana"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Verdana">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Times New Roman"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font face="Times
New Roman">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Name = "Arial"
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
face="Arial">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 8
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="1">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 10
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="2">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 12
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="3">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll,
Wrap:=Word.WdFindWrap.wdFindContinue)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 16
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="4">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 18
.Execute(findtext:="", ReplaceWith:="<font
size="5">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 24
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="6">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
With inputRng.Find
.ClearFormatting()
.Font.Size = 32
.Replacement.ClearFormatting()
.Execute(findtext:="", ReplaceWith:="<font
size="7">^&</font>", Format:=True,
Replace:=Word.WdReplace.wdReplaceAll)
End With
Return inputRng.Text
End Function