Option
Explicit
Public
Sub
FieldsToWildcards()
Dim
pRange
As
Word.Range
Dim
aField
As
field
Dim
currFieldCode
As
String
Dim
wildcard
As
String
Dim
skipNext
As
Boolean
skipNext =
False
Dim
mfIfParamsCount1
As
Integer
mfIfParamsCount1 = 6
Dim
mfIfParamsCount2
As
Integer
mfIfParamsCount2 = 7
Dim
mfIfParams1
As
Variant
Dim
param1Pos
As
Integer
param1Pos = 4
Dim
mfIfParams2
As
Variant
Dim
param2Pos
As
Integer
param2Pos = 1
Dim
param3Pos
As
Integer
param3Pos = 3
Dim
param4Pos
As
Integer
param4Pos = 5
Dim
mfTypePos
As
Integer
mfTypePos = 2
Dim
mfVarStart
As
Integer
mfVarStart = 13
Dim
dqASCIICode
As
Integer
dqASCIICode = 34
For
Each
pRange
In
ActiveDocument.StoryRanges
For
Each
aField
In
pRange.Fields
Set
pRange = aField.Code
currFieldCode = aField.Code
wildcard =
"${"
If
InStr(1, currFieldCode,
"IF"
) = mfTypePos
Then
skipNext =
True
mfIfParams2 = Split(currFieldCode, Chr(dqASCIICode), mfIfParamsCount2)
mfIfParams1 = Split(mfIfParams2(0),
" "
, mfIfParamsCount1)
wildcard = wildcard & Trim(mfIfParams1(param1Pos)) &
";"
wildcard = wildcard & Trim(mfIfParams2(param2Pos)) &
";"
wildcard = wildcard & Trim(mfIfParams2(param3Pos)) &
";"
wildcard = wildcard & Trim(mfIfParams2(param4Pos)) &
"}"
Else
If
InStr(1, currFieldCode,
"MERGEFIELD"
) = mfTypePos
Then
If
skipNext =
True
Then
skipNext =
False
Else
skipNext =
False
wildcard = wildcard & Trim(Mid(currFieldCode, mfVarStart, Len(currFieldCode) - mfVarStart)) &
"}"
End
If
End
If
End
If
aField.Delete
pRange.Collapse wdCollapseStart
pRange.Text = wildcard
Next
Next
End
Sub