Consulting

Results 1 to 6 of 6

Thread: How to find exact match in a word macro

  1. #1

    How to find exact match in a word macro

    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
    Last edited by kpangman; 02-25-2020 at 02:52 PM.

  2. #2
    Happy to provide more color on this if needed - I really appreciate the help!!

  3. #3
    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!

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Sadly that didnt work - the macro wouldnt even run

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •