PDA

View Full Version : [SOLVED:] How to find exact match in a word macro



kpangman
02-25-2020, 01:07 PM
I am using the below macro to search a document to find a ticker that I type into an input box from an excel document and use it to populate a report based on the horizontal data that corresponds to the mail merge fields in word. I am using the below macro - which for the most part works perfectly. However, if I type in a ticker with less than 4 characters it produces the first instance in the list that includes the letters in the input box. For example, if I type in AM it returns AKAM which appears first in the list. I want to be able to type in AM and get AM. Help!

Sub Macro()
Dim numRecord As Integer
Dim Ticker As String
Ticker = InputBox("Enter the Ticker:")
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=Ticker, Field:="Ticker") = True Then
numRecord = dsMain.ActiveRecord
End If
End Sub

kpangman
02-26-2020, 07:56 AM
Happy to provide more color on this if needed - I really appreciate the help!! :thumb:hi:

kpangman
03-03-2020, 09:12 AM
Is this even possible to do within VBA? I've noticed there are a lot of views but no replies - perhaps this isnt possible to do. Would love some insight either way. Thanks all!

macropod
03-03-2020, 05:24 PM
Try:

Dim dsMain As MailMergeDataSource, numRecord As Long, Ticker As String
Ticker = InputBox("Enter the Ticker:")
With ActiveDocument.MailMerge.DataSource
Do While .FindRecord(FindText:=Ticker, Field:="Ticker") = True
If .DataFields("Ticker") = Ticker Then
numRecord = .ActiveRecord
Exit Do
End If
Loop
End With
MsgBox numRecord

kpangman
03-09-2020, 09:50 AM
Sadly that didnt work - the macro wouldnt even run

macropod
03-09-2020, 02:43 PM
Well, given that what I posted isn't an entire macro, but is just some code one might insert into a larger routine demonstrating how one might go about getting an exact match, that's hardly surprising.