Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Auto Search Macro (VBA Word 2003)

    Hi,

    Im trying to put a macro together that will search for a reference between two points.

    For example my users always need to search for a reference in a document. Unfortunately the reference will never be the same and I was wondering therefore if it is possible to search for text between start and end characters, i.e. a typical reference would be #AC34567742#. The hash are always at the start and end.

    Their requirements are that they just want to press a button, linked to a macro, in word and the reference will be highlighted for them automatically, saving them time searching for it.

    I cant seem to find anything that suggests searching between two points ( a start and end point) can be done. Has anyone seen this done or maybe suggest a piece of script that can do it?

    Any help will be much appreciated.

    Cheers.
    Lee

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Search Macro (VBA Word 2003)

    You can search with wildcards in Word. Try this macro:
    <code>
    Sub FindReference()
    With Selection.Find
    .ClearFormatting
    .Text = "#*#"
    .MatchWildcards = True
    .Execute
    End With
    End Sub</code>

  3. #3
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Hi Hans,

    Thats perfect.

    Ive just been told though that the start and end markers are in fact * rather than #. Is there a way to change words default wildcard from * to something else?

    Many thanks for your assistance on this.

    Regards.
    Lee

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Use
    <code>
    .Text = "[*]*[*]"
    </code>
    The brackets around * tell Word to interpret it as a literal character instead of a wildcard.

  5. #5
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Hi,

    Excellent.

    Thanks so much for your help Hans, its very much appreciated.

    Regards.
    Lee

  6. #6
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Hi Hans,

    Sorry to trouble you further on this. I need the macro that you helped me with ealier to activate whenever a word document is opened. Im not sure how good an idea it is to use Sub AutoOpen(), as I was under the impression that doings so could cause problems. So Ive tried to use the Document_Open procedure, as follows;

    Public Sub Document_Open()
    Application.Run MacroName:="FindReference"
    End Sub

    But unfortunately, whenever word is opened the 'FindReference' macro isnt being called.

    Do you know where Im going wrong?

    Many thanks.
    Lee

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Search Macro (VBA Word 2003)

    If you want to call the macro whenever *any* document is opened, you can create a macro AutoOpen in a standard module in Normal.dot:

    Sub AutoOpen()
    Call FindReference
    End Sub

    The macro FindReference should also be in a standard module in Normal.dot.

    Or is this for a specific document only?

  8. #8
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Hi Hans,

    The macro is to be called on every document, however Im curious, to help with my learning curve, as to what I would do with a specific document, would there be a better alternative way?

    Regards.
    Lee

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Search Macro (VBA Word 2003)

    If you want to call the macro in a specific document only, you can call the FindReference macro from the Document_Open event procedure in the ThisDocument module:

    Private Sub Document_Open()
    Call FindReference
    End Sub

  10. #10
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Auto Search Macro (VBA Word 2003)

    Ok, no problem.

    Many thanks for your help on all this.

    Regards.
    Lee

Posting Permissions

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