writing macros for word 2007

gridsystems

New member
Member
Local time
3:00 AM
Messages
31
Hi,

I can record simple search and replace macros in word 2007 but my abilities stop there.

Back in the days when I was using Word Perfect I could easily write macros that search for a string, exit the search function, select all text until a different string occurred, and then marked that text red, and then repeat until end of document.
In that way I could easily, e.g. in text formatted as below, find and colour everything Bob says red. I would search for "Bob:" turn selection on, move and select everything until the next ^p (which signals that Bob has stopped speaking), change formatting to red, and then repeat until end of document.

Ted: Hi, Bob. How are you?
Bob: Fine. And you?
Ted: I've got a splitting head ache.
Bob: You-d better have an aspirin, then.
// ...etc etc etc... //


In MS-Word 2007 I have not been able to perform such automation, and I have no luck googling for tutorials either. I would appreciate if someone could point me in the right direction.

Thanks!
 

My Computer My Computer

Computer Manufacturer/Model Number
built it myself
OS
Windows 7 Pro 64-bit
CPU
Intel i7 950
Motherboard
Asus Sabertooth X58
Memory
18GB
Graphics Card(s)
Gigabyte Radeon HD7700
Sound Card
onboard Realtek HD audio chip
Monitor(s) Displays
2 x Dell U2412M
Screen Resolution
1920x1200
Hard Drives
3 SSD
4 Western Digital Caviar Green
PSU
SeaSonic x650
Case
Fractal Design R3 case
Cooling
Magehalems rev. B CPU cooler
Hope this help...

Code:
Sub FindBob()
'
    ' create a loop that continues until the end of the doc
    Do Until ActiveDocument.Bookmarks("\Sel").Range.End = _
                ActiveDocument.Bookmarks("\EndOfDoc").Range.End
    
        ' find Bob:
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "Bob:"
        End With
        Selection.Find.Execute
        
        ' Once Bob: is found then select to the paragraph mark
        Selection.GoTo what:=wdGoToBookmark, Name:="\para"
        
        ' format the selected para
        Selection.Font.ColorIndex = wdRed
        
        ' move insertion point to the end of the para so the find can continue
        Char = Selection.EndOf(Unit:=wdParagraph, Extend:=wdMove)
    Loop
End Sub
 

My Computer My Computer

Computer type
PC/Desktop
OS
Windows 7
Back
Top