none
Insert values before matched string in VBA RRS feed

  • Question

  • I'm completely new to VBA. I wanted to match a string, and for every occurrence, insert values before it. So far, from googling, i have:

    Sub EditFindLoop()
    'find text where the string equals [00:00:00] or numeric sequence as per input mask
    'then insert myText and myAutoTextFieldValue before it
    Dim myText As String
    Dim myAutoTextFieldValue As String
    Dim myFind As String
    myFind = "\[[0-9]*[0-9]*[0-9]\]"
    myAutoTextFieldValue = "fignum"
    myText = "Figure"
        With ActiveDocument.Content.Find
            '.Text = myFind
            '.ClearFormatting
            .MatchWildcards = True
            Do While .Execute(findText:=myFind, Forward:=True) = True
                        .InsertBefore myText & myAutoTextFieldValue
            Loop
        End With
    End Sub

    This gives me get "Method or Data Member not found" when I try to run it.


    Friday, November 6, 2015 2:26 PM

Answers

  • Here's what I ended up with, slightly different than I originally thought but it works well.

    Sub EditFindLoop()
        Dim myText As String
        Dim myFind As String
        Dim x As Integer
        myFind = "\[[0-9]*[0-9]*[0-9]\]"
        myText = "Figure "
        mySpace = ". "
        x = 1
        Dim oRange As Word.Range
        Set oRange = ActiveDocument.Range
        
        With oRange.Find
            .Text = myFind
            .ClearFormatting
            .MatchWildcards = True
            .MatchCase = False
            .MatchWholeWord = False
            
            Do While .Execute = True
                If .Found Then
                    oRange.InsertBefore (myText & x & mySpace)
                End If
                oRange.Start = oRange.End
                oRange.End = ActiveDocument.Range.End
                x = x + 1
            Loop
        End With
       
    End Sub

    Friday, November 6, 2015 9:22 PM

All replies

  • Hi,

    The .insertBefore is not a member of .Execute and thus will produce the error.

    Try this: edited the .InsertBefore line....

    Sub EditFindLoop()
     'find text where the string equals [00:00:00] or numeric sequence as per input mask
     'then insert myText and myAutoTextFieldValue before it
     Dim myText As String
     Dim myAutoTextFieldValue As String
     Dim myFind As String
     myFind = "\[[0-9]*[0-9]*[0-9]\]"
     myAutoTextFieldValue = "fignum"
     myText = "Figure"
         With ActiveDocument.Content.Find
             '.Text = myFind
             '.ClearFormatting
             .MatchWildcards = True
             Do While .Execute(findText:=myFind, Forward:=True) = True
                    ActiveDocument.Content.InsertBefore myText & myAutoTextFieldValue
                    
                         
             Loop
         End With
     End Sub
    

    That should do the trick

    Maurice


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. Thank You

    Friday, November 6, 2015 3:25 PM
  • Here's what I ended up with, slightly different than I originally thought but it works well.

    Sub EditFindLoop()
        Dim myText As String
        Dim myFind As String
        Dim x As Integer
        myFind = "\[[0-9]*[0-9]*[0-9]\]"
        myText = "Figure "
        mySpace = ". "
        x = 1
        Dim oRange As Word.Range
        Set oRange = ActiveDocument.Range
        
        With oRange.Find
            .Text = myFind
            .ClearFormatting
            .MatchWildcards = True
            .MatchCase = False
            .MatchWholeWord = False
            
            Do While .Execute = True
                If .Found Then
                    oRange.InsertBefore (myText & x & mySpace)
                End If
                oRange.Start = oRange.End
                oRange.End = ActiveDocument.Range.End
                x = x + 1
            Loop
        End With
       
    End Sub

    Friday, November 6, 2015 9:22 PM
  • Thanks for input Maurice, I really appreciate it.
    Friday, November 6, 2015 9:24 PM
  • Your code could be simplified, made more reliable and to execute more quickly, as:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Text = "\[[0-9\:]@\]"
        .Replacement = ""
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found = True
        x = x + 1
        .InsertBefore "Figure" & Chr(160) & x & "." & Chr(160)
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    The above will insert 'Figure x. ' before any numeric string within [ ], regardless of whether a ':' is there. Depending on your requirements you may want to limit it to just those containing the ":', in which case you could replace:
        .Text = "\[[0-9\:]@\]"
    with:
        .Text = "\[[0-9]{2}\:[0-9]{2}\:[0-9]{2}\]"

    The code I've posted also inserts non-breaking spaces into the 'Figure x. ' string so that it will stay on the same line as the number string referred to.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 6, 2015 10:28 PM
  • This is great guidance. Thank you!
    Saturday, November 7, 2015 6:46 PM