Adamski
05-23-2012, 07:26 AM
Hello,
I am writing a function to format some Captions.
The problem I am having is getting the find/replace code to loop corectly.
I have seen various ways to find all, find\replace all using 'wdReplaceAll', 'while .execute' and 'while .found' but what is the prefered method.
I can't do wdReplaceAll as I want to modify each range which is found after it is replaced.
Example Document Content:
123 Table 1-1: format and modify this para
8 Table 123-456: this one too
34 Table 2-12: and me
12 Figure 7-1: this as well
Required Styles:
Caption
Table Caption
Figure Caption
Simplified Function:
Sub ProcessCaptionType(oDocument As Document, sCaptionType As String)
Dim sBookmarkPrefix As String
sBookmarkPrefix = "s"
Dim oOldStyle As Style
Set oOldStyle = oDocument.Styles("Caption")
Dim oNewStyle As Style
Set oNewStyle = oDocument.Styles(sCaptionType & " Caption")
Dim oRange As Range
Set oRange = oDocument.Content.Duplicate
' Setup Find Object
With oRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = oOldStyle
.Text = "([0-9]{1,}) " & sCaptionType & " [0-9]{1,}-[0-9]{1,}: "
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' Find All
Do While oRange.Find.Execute(Replace:=wdReplaceOne)
' Get the ID for the Bookmark
Dim sID As String
sID = oRange.Text
' Change the Paragraph Style
oRange.Paragraphs.First.Range.Style = oNewStyle
' Modify the Range (Simplified)
oRange.Text = sCaptionType & sID & ": "
' Create Bookmark
oDocument.Bookmarks.Add Name:=sBookmarkPrefix & sID, Range:=oRange
Loop
End Sub
Sub test()
Call ProcessCaptionType(ActiveDocument, "Table")
Call ProcessCaptionType(ActiveDocument, "Figure")
End Sub
The modification to the range is more complex than this - adding fields etc.
The code works for the first match the the loop ends.
If I use this, it loops correctly:
Do While oRange.Find.Execute
(Note there is no replace parameter)
I can use that and get the sID as the first word, but then the code is specific to the find expression - If the find expression changes I'll need to modify the code too.
So...
How to loop find\replace while modifying each found range??
Thanks
I am writing a function to format some Captions.
The problem I am having is getting the find/replace code to loop corectly.
I have seen various ways to find all, find\replace all using 'wdReplaceAll', 'while .execute' and 'while .found' but what is the prefered method.
I can't do wdReplaceAll as I want to modify each range which is found after it is replaced.
Example Document Content:
123 Table 1-1: format and modify this para
8 Table 123-456: this one too
34 Table 2-12: and me
12 Figure 7-1: this as well
Required Styles:
Caption
Table Caption
Figure Caption
Simplified Function:
Sub ProcessCaptionType(oDocument As Document, sCaptionType As String)
Dim sBookmarkPrefix As String
sBookmarkPrefix = "s"
Dim oOldStyle As Style
Set oOldStyle = oDocument.Styles("Caption")
Dim oNewStyle As Style
Set oNewStyle = oDocument.Styles(sCaptionType & " Caption")
Dim oRange As Range
Set oRange = oDocument.Content.Duplicate
' Setup Find Object
With oRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = oOldStyle
.Text = "([0-9]{1,}) " & sCaptionType & " [0-9]{1,}-[0-9]{1,}: "
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' Find All
Do While oRange.Find.Execute(Replace:=wdReplaceOne)
' Get the ID for the Bookmark
Dim sID As String
sID = oRange.Text
' Change the Paragraph Style
oRange.Paragraphs.First.Range.Style = oNewStyle
' Modify the Range (Simplified)
oRange.Text = sCaptionType & sID & ": "
' Create Bookmark
oDocument.Bookmarks.Add Name:=sBookmarkPrefix & sID, Range:=oRange
Loop
End Sub
Sub test()
Call ProcessCaptionType(ActiveDocument, "Table")
Call ProcessCaptionType(ActiveDocument, "Figure")
End Sub
The modification to the range is more complex than this - adding fields etc.
The code works for the first match the the loop ends.
If I use this, it loops correctly:
Do While oRange.Find.Execute
(Note there is no replace parameter)
I can use that and get the sID as the first word, but then the code is specific to the find expression - If the find expression changes I'll need to modify the code too.
So...
How to loop find\replace while modifying each found range??
Thanks