nhmabv
08-29-2013, 09:30 PM
I have a UDF that finds text in a word document where "Fields" are in Bold throughout the document. EG
The city name "Oakland" is what is returned from the following"
City Oakland State
However, I found a flaw. Consider the following:
cant have this |
v
Address 1234 City Park Blvd City Oakland State
To get the address my UDF uses the find text function to find the left "Field" which is in this example "Address" . It then does a control-shift right until the right side of the selection equals the right "Field" "City" and then back spaces to unselect the right hand "Field".
This has been working for a long time.
However, in the above example, it fails because the actual data contains the right hand field name "City"
The address is never bold but the "Fields" always are. I cant seem to find a way to test for the assistance of bold text contained in a selection.
msgbox Get_It4("City","State",3,True,"N")
returns "Oakland"
msgbox Get_It4("Address","City",3,True,"N")
returns Null
If anyone has a better approach or solution I would be ever so gratefull.
Thanks
'========================================================================== ======================================
Code:
Function Get_It4(name As Variant, Limit As Variant, Cnt As Variant, Bld As Boolean, Neither As Variant) As String
'Set Neither to "Y" if search for wild cards. In the checklist, the search for agent phone has both bold and non bold
Dim N As Integer
N = 1
Selection.GoTo what:=wdGoToPage, which:=wdGoToFirst
Selection.Find.ClearFormatting
With Selection.Find
.Text = name
.Forward = True
.MatchCase = False
.MatchWholeWord = True
.MatchSoundsLike = False
.MatchAllWordForms = False
If Neither = "Y" Then
.MatchWildcards = True
Else
.Font.Bold = Bld
.MatchWildcards = False
End If
End With
If Selection.Find.Execute Then
Selection.MoveRight Unit:=wdCharacter, Count:=2 'put cursor on first char of field
Do Until UCase(Right(RTrim(Selection.Text), Len(Limit))) = UCase(Limit) Or N > 12 'This is what needs to change?
'12 is arbitraray, dont want this going to infinity looking for "Limit"
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
N = N + 1
Loop
Selection.MoveLeft Unit:=wdWord, Count:=Cnt, Extend:=wdExtend
Get_It4 = Trim(Strip_XChars(Selection.Text)) 'Strip_XChars strips out tabs and other junk if present
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
Get_It4 = "Not Found"
End If
End Function
The city name "Oakland" is what is returned from the following"
City Oakland State
However, I found a flaw. Consider the following:
cant have this |
v
Address 1234 City Park Blvd City Oakland State
To get the address my UDF uses the find text function to find the left "Field" which is in this example "Address" . It then does a control-shift right until the right side of the selection equals the right "Field" "City" and then back spaces to unselect the right hand "Field".
This has been working for a long time.
However, in the above example, it fails because the actual data contains the right hand field name "City"
The address is never bold but the "Fields" always are. I cant seem to find a way to test for the assistance of bold text contained in a selection.
msgbox Get_It4("City","State",3,True,"N")
returns "Oakland"
msgbox Get_It4("Address","City",3,True,"N")
returns Null
If anyone has a better approach or solution I would be ever so gratefull.
Thanks
'========================================================================== ======================================
Code:
Function Get_It4(name As Variant, Limit As Variant, Cnt As Variant, Bld As Boolean, Neither As Variant) As String
'Set Neither to "Y" if search for wild cards. In the checklist, the search for agent phone has both bold and non bold
Dim N As Integer
N = 1
Selection.GoTo what:=wdGoToPage, which:=wdGoToFirst
Selection.Find.ClearFormatting
With Selection.Find
.Text = name
.Forward = True
.MatchCase = False
.MatchWholeWord = True
.MatchSoundsLike = False
.MatchAllWordForms = False
If Neither = "Y" Then
.MatchWildcards = True
Else
.Font.Bold = Bld
.MatchWildcards = False
End If
End With
If Selection.Find.Execute Then
Selection.MoveRight Unit:=wdCharacter, Count:=2 'put cursor on first char of field
Do Until UCase(Right(RTrim(Selection.Text), Len(Limit))) = UCase(Limit) Or N > 12 'This is what needs to change?
'12 is arbitraray, dont want this going to infinity looking for "Limit"
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
N = N + 1
Loop
Selection.MoveLeft Unit:=wdWord, Count:=Cnt, Extend:=wdExtend
Get_It4 = Trim(Strip_XChars(Selection.Text)) 'Strip_XChars strips out tabs and other junk if present
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
Get_It4 = "Not Found"
End If
End Function