Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search and Replace (Word 97 and 2002)

    I have a little bit of a problem. I have recorded a macro to search and replace Bold with Italics. The problem is, I also need to Search for Underline, SmallCaps, etc. I can copy and paste and end up with a large macro. I was hoping someone can show me an example of using arrays that will go search and replace the entire document with all these codes.

    Thanks!

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

    Re: Search and Replace (Word 97 and 2002)

    Do you mean that you want to replace Bold with Italics, Underline with Italics, SmallCaps with Italics etc., or do you want to be more flexible?

  3. #3
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    More flexible. More like I can select from a list even. For example a list box showing the codes to search for and another list box to show the code I want to replace with.

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

    Re: Search and Replace (Word 97 and 2002)

    The attached document contains a userform that may do what you want. I didn't try to put in all possibilities or to make it modular, you may want to do that yourself.

    The userform contains two dropdown lists with format elements: one to find and one to replace with. 'OK' will replace all occurrences of the format to find with the format to replace with, 'Cancel' will just close the form.

    In Word 2002, make sure that Macro Security is not set to High, otherwise you won't be able to use the userform and macro.

  5. #5
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    You're a genius Hans! Thank you very much. It's a great start to learn dialog boxes. I was actually able to create listboxes replacing the combo boxes.

    I have one quick question though, but don't laugh. How do I make the "OK" and "Cancel" buttons neumonic? I tried using "&" but it didn't work.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Search and Replace (Word 97 and 2002)

    Do you want to set an "accelerator" key (e.g., Alt+O for Okay)?

  7. #7
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    Oh geez, LOL. Thanks!!!!

  8. #8
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    Ok, here's another question related to the above and I can't seem to figure this one out.

    What do I do if I want to search for BOLD, unbold it, and add text at the beginning and end of the bolded word??? For example:

    Before the search: BOLDmy nameBOLD
    After the search: ABCmynameABC

    Thanks!

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace (Word 97 and 2002)

    If you will always be replacing exactly one word in each place then you can modify Hans code as follows

    In the Find part of the code add
    .MatchWildcards = True
    .Text = <*>

    In the Replacement part of the code add

    .Text = ABC^&ABC

    (You should be able to find the existing two lines with .Text = "" that you need to replace) I will attach a modified version of Hans code for you.

    If you need this to work for phrases, and for parts of words, then we would need to remove the ReplaceAll and use a loop that worked on each instance of the formatting one at a time.

    StuartR

  10. #10
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    I have modified the macro to add the items in the combo box but don't know how to do it <img src=/S/bwaaah.gif border=0 alt=bwaaah width=123 height=15>

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace (Word 97 and 2002)

    You seem to have added your text string to the combo box that was being used to select the style to modify. This doesn't make much sense. That combo box should contain the name of a style.

    The following questions are intended to help us get a better specification of what you are trying to do...

    Do you want to insert the text in the replacement string, or did you intend to search for the particular text string as well as the format, or both?
    Is this a fixed text string (in which case we can hard code it in the Macro) or is it variable (in which case we need to provide a way to specify it).
    Is there a fixed list of possible text strings, or does the user need to be able to specify any text that they wish?
    Is it always the same text at the beginning and end or could there be 2 different text strings.

    StuartR

  12. #12
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace (Word 97 and 2002)

    Hi Stuart:

    There is a list of codes and strings to search for. Each can be replace by the other. I am learning all these so thank you for your patience. I have tried to build an array for the selection but not successful to load them into the combo box (my other post

    The process should follow what Hans started, just making it a bit more complex. For examples:

    1. If I select "Bold" from the Find box then selects "Italic" on the Replace box, the macro should find all bolded words and turn them into Italics.

    2. If I select "Bold" from the Find box then selects "B1" on the Replace box, the macro should find all bolded words, turn bold off, then insert "B1" at the beginning and end of the string.

    3. If I select "B1" from the Find box then selects "Bold" on the Replace box, the macro should find all the strings beginning with B1 and ends with B1, remove B1 from the string, then turns the string into Bold.


    I hope I am making myself clear, all these coding is making me <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace (Word 97 and 2002)

    This is much more complex than the original code.

    We will need to create additional textbox controls on the form for the extra text to find on the Find and Replace.
    A little experiment shows that a wildcard replace with a find string of (Prefix)<font color=blue>(*)</font color=blue>(Prefix) and a replace sting of NewPrefix<font color=blue>2</font color=blue>NewPrefix gives the correct replacement behaviour.

    Do you have a fixed list of codes (e.g. ABC, B1 etc.) or could it be anything?
    Is there a maximum length for the code or could it be hundreds of characters?

    It will take me a while to get the time to try and write this for you. I will try to post something later today.

    StuartR

  14. #14
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace (Word 97 and 2002)

    I am trying to avoid having to write a complex piece of VBA that manipulates each found string using code.

    I can do most of what you want, but I can't yet see a way to search for BOLD text that will leave the next range of bolded text selected.

    <*> in wildcard mode will find the next bold word, but we need to find the next bold phrase, and that is less easy.

    More later, when I have thought about it for longer.

    StuartR

  15. #15
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace (Word 97 and 2002)

    That took longer than I expected it to. The trick was to use Wildcard searching for strings like
    B1 your text here B1
    and non wildcard searches when searching for stuff without a prefix/sufix pair.

    Does this do what you wanted?

    StuartR

Page 1 of 2 12 LastLast

Posting Permissions

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