Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Selecting specific characters (WinNT/Word97)

    Apologies if this has been covered before, but I couldn't find anything when I searched!

    Can anyone tell me how to use VBA to select a specific string of characters, where the first and last characters are known but the length of the string in between is not?

    I need to find instances of a code we use which is laid out along the lines of (( ... )) ie, it always starts with two open brackets and ends, between 2 and ~6 characters later, with two close brackets. I can find the open brackets but I can't work out how to extend the selection to cover the rest of the code!

    Any suggestions gratefully received!
    Beryl M


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

    Re: Selecting specific characters (WinNT/Word97)

    Are you looking for text in a document? I recorded a macro to do that; I have included the code below (after a bit of cleaning up). I have made the parts that are most relevant bold:
    <UL><LI>.MatchWildCards = True means that you search with wildcards
    <LI>.Text = "((*))" means that you search for two opening brackets, then any number of characters, then two closing brackets. The brackets are prefixed by backslashes to make them into literal characters; this is because brackets can be used in wildcard searches to delimit parts of the search string. The * stands for any number of characters.[/list]<font face="Georgia">With Selection.Find
    .ClearFormatting
    .Text = "((*))"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    End With</font face=georgia>

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting specific characters (WinNT/Word97)

    Yes, sorry Hans, the 'code' is just text, so your suggestion would work perfectly. Many thanks!

    Can I add a bit to my question? I need to search a document for this text, if found delete it, if not carry on - I presume I need to dim a variable to do this but I'm not sure how to implement it? I tried a couple of ways but couldn't get the variable to pick up a 'value' (true or false, according to whether it found it or not). The 'code' will usually only appear once in any one document, but it may appear two or three times, always at the top of a page ...

    Your help is much appreciated!
    Beryl M


  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting specific characters (WinNT/Word97)

    Sorry, Hans, ignore my last question! If I use replace with nothing it doesn't matter how many times it appears, if at all! <img src=/S/doh.gif border=0 alt=doh width=15 height=15>

    Thanks again!
    Beryl M


  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Selecting specific characters (WinNT/Word97)

    Hans, just thought you might like to know - I don't know if you saw my posts a few days ago in the Word forum, at the bottom of the thread Controlling paper trays? Well, with the help Stuart gave me there, and this macro, that's several months work not only made easier, but automated so thoroughly I don't have to do the work at all!

    <img src=/S/joy.gif border=0 alt=joy width=23 height=23> You'll excuse me if I feel like celebrating! Many, many thanks!! <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>
    Beryl M


Posting Permissions

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