Consulting

Results 1 to 4 of 4

Thread: Word Dynamic Array

  1. #1

    Word Dynamic Array

    Hi,

    I am creating a script that will go through a word document and highlight instances of key phrases.
    I have created an array "Sales Figures", "Token", "Party Line String". When this is hardcoded into the VBA it works fine and highlights any instance found. What I would like to do is have a form with a text box on it that a user can populate with their search string. I can only get the VBA to read the very first word in my text box though.

    My code is below:

    Private Sub CommandButton1_Click()
    Dim range As range
    Dim i As Long
    Dim TargetList
    
    
    
    
    TargetList = Array(TextBox1) ' put list of terms to find here
    'TargetList = Array("Sales Figures", "Token", "Party Line String") 'Hardcoded List
    
    
    For i = 0 To UBound(TargetList)
    
    
    Set range = ActiveDocument.range
    
    
    With range.Find
    .Text = TargetList(i)
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    
    
    Do While .Execute(Forward:=True) = True
    range.HighlightColorIndex = wdYellow
    
    
    Loop
    
    
    End With
    Next
    End Sub
    I've attached a screenshot of my userform too.
    2017-08-26.jpg

    Any help would be much appreciated.
    Thanks

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Split function

    Option Explicit
    
    
    Sub test()
        Dim k As String
        Dim s
        
        k = InputBox("keywords List separated by comma")
        If k = "" Then Exit Sub
        s = Split(k, ",")
        MsgBox s(LBound(s))
        MsgBox s(UBound(s))
    
    
    End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Not tested and no error checking


     Private Sub CommandButton1_Click() 
        Dim range As range 
        Dim i As Long 
        Dim TargetList  As Variant
         
         
         
         'No quotes around strings, and separated by commas
        TargetList = Split (TextBox1.Caption, ",")    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         
         
        For i =  LBound(TargetList) To UBound(TargetList) 
             
             
            Set range = ActiveDocument.range 
             
             
            With range.Find 
                .Text = TargetList(i) 
                .Format = True 
                .MatchCase = True 
                .MatchWholeWord = False 
                .MatchWildcards = False 
                .MatchSoundsLike = False 
                .MatchAllWordForms = False 
                 
                 
                Do While .Execute(Forward:=True) = True 
                    range.HighlightColorIndex = wdYellow 
                     
                     
                Loop 
                 
                 
            End With 
        Next 
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Thanks very much. Works a treat.

Tags for this Thread

Posting Permissions

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