Run MS Word macro for each line of a Word document

O

olson_ord

Hi,
I would be grateful if someone could try to answer this:

Question:
How to call a macro for every line of my Word Document?

Explanation:
I have a MS Word document having a huge number of lines that are like

7. maudlin: excessively sentimental.

This is to say that a line contains a few spaces initially, then a
number, then a word and finally the meaning of the word. I want to
delete the number, and make the word Bold. If I place my cursor at the
beginning of the line then I can do something like

Sub trial()
'
' trial Macro
'
Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.TypeBackspace
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Bold = wdToggle
End Sub


I don't know much about Macros or Visual Basic - this is what I
recorded from the MS Word Macro Recorder - and it seems to work. Now I
want to know how to call this for each line of my document.

Thanks a lot to any one who has ideas.
O.O.
 
G

Graham Mayor

Did you read the article?

If your layout is exactly as described then replace
[0-9]{1,}.[ ](<*>:)
with
\1 - format font bold.

This will embolden any word followed by a colon that follows any number
followed by a full stop and a space (period). If your layout differs then
adjust the search string.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
O

olson_ord

Before my last post I had briefly flipped through the article - but now
I read it more closely. I had missed the "Note" that you had - and I
just looked at the examples none of which had formatting issues.
I did not realize that I need to type \1 in the replace box and then
press Ctrl-B or go down and select the Format button and set the font
to BOLD.

Anyway now this partially does work. My problem is that each of my
lines begin with two spaces and then the number. Your search

[0-9]{1,}.[ ](<*>:)

Just catches the initial number. I tried to get the spaces also - but
I was unsuccessful. (I am not used to regular expressions.) I tried the
following

(^w) [0-9]{1,}.[ ](<*>:)
Here it tells me that ^w is not a valid search character.

(*^s) [0-9]{1,}.[ ](<*>:)
Here it does not find any matches i.e. search term not found.

[ ] [0-9]{1,}.[ ](<*>:)
This seems to capture one space i.e. it deletes one space at the
beginning of the line - but one space is still there. (I wish we could
modify this one to capture two spaces in that initial [])

[space] [space][0-9]{1,}.[ ](<*>:)
This is modified from Ex. 1 on that page. I am not sure if in that
explanation you are referring to using the word space or typing it from
the space bar. Anyway
[ ] [ ][0-9]{1,}.[ ](<*>:)
Also does not work.

Thanks a lot for your help though.
Regards,
O.O.
 
G

Graham Mayor

The formatting issues have nothing really to do with the wildcard option. If
you have two spaces in front of the string then add those spaces to the
string. If there is an indeterminate number of spaces then

[ ]{1,}[0-9]{1,}.[ ](<*>:)


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Before my last post I had briefly flipped through the article - but
now I read it more closely. I had missed the "Note" that you had -
and I just looked at the examples none of which had formatting issues.
I did not realize that I need to type \1 in the replace box and then
press Ctrl-B or go down and select the Format button and set the font
to BOLD.

Anyway now this partially does work. My problem is that each of my
lines begin with two spaces and then the number. Your search

[0-9]{1,}.[ ](<*>:)

Just catches the initial number. I tried to get the spaces also - but
I was unsuccessful. (I am not used to regular expressions.) I tried
the following

(^w) [0-9]{1,}.[ ](<*>:)
Here it tells me that ^w is not a valid search character.

(*^s) [0-9]{1,}.[ ](<*>:)
Here it does not find any matches i.e. search term not found.

[ ] [0-9]{1,}.[ ](<*>:)
This seems to capture one space i.e. it deletes one space at the
beginning of the line - but one space is still there. (I wish we could
modify this one to capture two spaces in that initial [])

[space] [space][0-9]{1,}.[ ](<*>:)
This is modified from Ex. 1 on that page. I am not sure if in that
explanation you are referring to using the word space or typing it
from the space bar. Anyway
[ ] [ ][0-9]{1,}.[ ](<*>:)
Also does not work.

Thanks a lot for your help though.
Regards,
O.O.







Graham said:
Did you read the article?

If your layout is exactly as described then replace
[0-9]{1,}.[ ](<*>:)
with
\1 - format font bold.

This will embolden any word followed by a colon that follows any
number followed by a full stop and a space (period). If your layout
differs then adjust the search string.
 
O

olson_ord

Thanks a lot Graham. This is exactly what I wanted. I knew that I had
to add the spaces to the search string, and in my last post I was
trying to figure out a way. I think I misunderstood this entire regular
expression i.e. I thought the {1,} corresponded to the \1 later on.
However now I have understood it.
Thanks again.
O.O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top