I'm working on a macro that will loop through my Word document, and select all of the XE codes, to parse through.
I'm trying to use Find
to get all such texts, but my regex searches aren't working at all.
I've tried: </{ XE*/}>
, <{*XE*}>
to no avail.
To remind, the text will look like this:
{ XE "Superhero - means a person with some powers" }
{ XE "Mountain is a tall hill, not always climbable." }
{ XE "Hill is a smaller mountain; not always large." }
I want to loop through each of these, and add two characters after each ;
found. How can I find these using RegEx?
Edit: For some reason, the .Find
never executes, even with the below code:
Sub escape_Special_Characters_in_XE_Text()
Dim regExSearch$
Dim doc As Word.Document
Dim rngDoc As Word.Range, rngXE As Word.Range, rng As Word.Range
Dim bFound As Boolean
Dim iFieldCodeBegin&
Set doc = ActiveDocument
Set rngDoc = doc.Content
Set rngXE = rngDoc.Duplicate
bFound = True
iFieldCodeBegin = 6 '?
rngDoc.Find.ClearFormatting
With rngDoc.Find
.Text = "^d XE"
'.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rngDoc.Find.Execute
rngDoc.Select ' This selects the whole document for some reason?
End Sub
Is there something I'm overlooking with regard to my Document? Why can't it find anything. If I record a macro, it uses Selection
instead of a variable, but I can run that over and over. When I try to use variables is when it seems to disagree with me.
Actually, you can find the starting braces of a field code using Word's Find by specifying the ESC combination: ^d
And when Find picks up the starting point of a field, the entire field is included in the Selection/Range. So you can loop all the XE fields in a document along the following lines:
Sub LoopAllXE()
Dim doc As word.Document
Dim rngDoc As word.Range
Dim rngXE As word.Range
Dim bFound As Boolean
Dim iFieldCodeBegin As Long
'Initialization
Set doc = ActiveDocument
Set rngDoc = doc.content
Set rngXE = rngDoc.Duplicate
bFound = True
iFieldCodeBegin = 6
With rngDoc.Find
.ClearFormatting
.ClearAllFuzzyOptions
.Text = "^d XE"
.Format = False
.wrap = wdFindStop
End With
'Get the XE fields' content
Do While bFound
bFound = rngDoc.Find.Execute
If bFound Then
Set rngXE = rngDoc.Duplicate
'Do something with the XE field content
'Below, the content is being trimmed of field code info
Debug.Print Mid(rngXE.Text, iFieldCodeBegin, _
Len(rngXE) - iFieldCodeBegin - 1)
'Content in table cells needs to be processed differently
'in order to avoid looping endlessly on the first "found"
Do While rngDoc.Information(wdWithInTable) And bFound
rngDoc.Collapse wdCollapseEnd
'Remainder of cell content after XE-field
rngDoc.End = rngDoc.Cells(1).Range.End - 1
bFound = rngDoc.Find.Execute
Set rngXE = rngDoc.Duplicate
'Do something with the XE field content
'May make sense to put this in a separate procedure
Debug.Print Mid(rngXE.Text, iFieldCodeBegin, _
Len(rngXE) - iFieldCodeBegin - 1)
If Not bFound Then
rngDoc.MoveStart wdCell, 1
End If
Loop
rngDoc.Collapse wdCollapseEnd
rngDoc.End = doc.content.End
End If
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.