none
Word VBA: Find.Text ="##*##" only selects "#"

    Question

  • Hello,
    I am using VBA (in an MS Access procedure) to locate and read text parts from a Word document. Text parts are like ##*## where * is a variable digit number (goes from 37 to 6000 or more).
    So what I want is to locate ##*##, read it and place it in an access table, then replace the text with something else, lets say ##READ##.

    Here is the VBA code:

    ******************************************************
    With objWord.ActiveDocument.Content.Find
        .ClearFormatting
        .MatchWildcards = True
        .Text = "##*##"
        If .Execute = True Then
            Debug.Print Selection.Text 'this is like reading the number
    . . .
    ******************************************************

    Example of the text in the Word document:
    Bla, bla, bla ##45## bla bla bla ##2843## bla, bla, . . . .

    Running the code should at first select ##45##, print ##45## in the Access Immediate window and so on. . .

    The problem is that it only prints: # like it only selects the first character from the ##45##.
    Can anyone help with this?
    I am grateful to anyone who will read my question

    Friday, March 06, 2009 4:32 PM

All replies

  • Can you try with the following pattern

    ##[0-9]{1;}##

    Cheers
    Shasur


    http://www.vbadud.blogspot.com
    Saturday, March 07, 2009 5:10 PM
  • Hi Shasur,
    The pattern was not a problem. It was the command activedocument.content that caused the problem. I copy from Tony Jollans who posted a relevant message in a newsgroup:

    Your Find, using ActiveDocument.Content, will not change the Selection. Either use Selection.Find (although, interestingly, VBA seems to work differently from the UI), or reference .Parent.Text (or rearrange your Withs to access it differently).

    Thank you

    Monday, March 09, 2009 1:35 PM