Google Answers Logo
View Question
 
Q: MS-Word Macro? How to put in BOLD all text between strings #1 and #2 x manytimes ( Answered,   1 Comment )
Question  
Subject: MS-Word Macro? How to put in BOLD all text between strings #1 and #2 x manytimes
Category: Computers > Software
Asked by: johnbibby-ga
List Price: $10.00
Posted: 22 Apr 2006 08:58 PDT
Expires: 22 May 2006 08:58 PDT
Question ID: 721722
In MS-Word, I need to put in BOLD all text between string #1 and
string #2 whenever they appear.

It's a long document, so it needs to be automated. (Use macro?)

I'll be even more delighted if the macro can be easily adapted so taht
BOLD can be replaced by ITALIC or BLUE or RED WITH FONTSIZE 20

JOHN
Answer  
Subject: Re: MS-Word Macro? How to put in BOLD all text between strings #1 and #2 x manytimes
Answered By: maniac-ga on 16 May 2006 18:56 PDT
 
Hello Johnbibby,

I have attached a Visual Basic macro that can be added to a document
(or to your "Normal" file) which reformats the document as you asked.

To use the macro, do the following steps:

[0] Start MS Word (an empty file is fine). Depending on the version of
Word, you may also have to change the "security" (or "Macro Virus
Protection") preferences so macros can run - tell me the specific
version of Word in a request for clarification if you have problems
running the macro.

[1] Use the menu
  Tools -> Macro -> Visual Basic Editor
At this point, Visual Basic should start. In the upper left is a
"Project" window listing the open files.

[2] Select one of the files (Normal.Dot refers to a file always opened
by MS Word) and use the menu
  Insert -> Module
Another window should open.

[3] Copy / paste the macro from the answer into this module (or code) window.

[4] Make any changes you find necessary (explained below).

[5] Close & return to MS Word (another menu selection - usually under "File").

At this point, using the menu
  Tools -> Macro -> Macros...
will bring up a list of the available macros. "jb" should be listed.
Selecting that macro & run will cause the macro to execute and should
do all the changes you requested.

If you put the macro into Normal.DOT, Word may prompt you to save
changes - say yes (or OK) to save the macro. If you put the macro into
an ordinary file, it will be saved with that file.

There are a few locations you may wish to change the macro:
 o The strings A$ and B$ which represent the start / end phrases of the search.
 o The formatting near the end - currently it makes the text bold and blue.
I added some comments at these locations to remind you of these
changes and describing some of the alternatives that are available.

Please make a request for clarification if any part of the answer is
unclear or you have any difficulty using the macro with your files.

Good luck with your work.

  --Maniac

Sub jb()
'
'   Macro written by Maniac on May 16, 2006
'   Reformats text between two strings
'

'   Specify search phrases (start / end)
    A$ = "abc"
    B$ = "def"
    
'   Start at the beginning and reset all
'   search formatting options

    ActiveDocument.Range(0).Select
    Selection.Find.ClearFormatting
'   Loop repeats until first phrase not found
    While Selection.Find.Execute(A$)
        StartReformat = Selection.End
        Selection.MoveRight
        Selection.Find.Execute (B$)
        StopReformat = Selection.Start
        Selection.MoveRight
'   Add formatting to the following section
'   Options include:
'   .Bold, .Italic, .Underline, .StrikeThrough (true / false)
'   .Size = font size
'   .Font.Color = wdColorGreen (Red, Blue, etc... see help)

        With ActiveDocument.Range(StartReformat, StopReformat)
            .Bold = True
            .Font.Color = wdColorBlue
        End With
    Wend
End Sub
Comments  
Subject: Re: MS-Word Macro? How to put in BOLD all text between strings #1 and #2 x manytimes
From: daniel_buenosaires-ga on 15 May 2006 22:10 PDT
 
Since Word cannot handle individual formatting in replacements (the
character attributes refer to the whole replacement string), there is
no way to do what you want with a single replacement.

One way is to mark the replacements by surrounding them with a
sequence not used anywhere else in the text ("$$" in this case):

Enable wildcards and...
1) Search for: string1(*)string2
    Replace by: $$string1$$\1$$string2$$               plus BOLD
2) Search for: $$(*)$$
    Replace by: \1                         plus NOT BOLD

If string1 and string2 are used somewhere else besides delimiters, the
above replacement will fall "out of sync" and should be repeated from
that point on. This will be evident, as whole sections will become
bold.

Daniel

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy