Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Word 2003 Macros - Replacing words throughout a document

Posted on 2012-03-10
5
326 Views
Last Modified: 2012-06-21
I'm searching for the word  "recommendation" and then going to the beginning of the line where that word appears and then deleting the entire line.  that's all.  It's a clean up macro.

Two questions:
1.  I would like to search for the word recommendation AND the hard return symbol.  How do I code the hard return symbol into the search request?
currently it says:    .Text = "recommendation"

2.  What do I need to insert into the macro to detect that there are no more instances of the word recommendation (i.e. I've reached the end of the document.)  here is my macro code
Currently I'm running the macro clicking on a button or using a keyboard shortcut.

Sub cleanUp()
'
' cleanUp Macro
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "recommendation"
        .Replacement.Text = "^l"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
0
Comment
Question by:brothertruffle880
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37706345
In Word Finds, the ^l stands for the newline character created with Shift + Enter.

Normally, we just hit enter which creates a new paragraph. If you use the ¶ (Show/Hide) button you will be able to see the difference.

I habitually to use a Range object instead of the Selection object.

This macro redefines the search range after each successful Find and delete. It loops until the Find is unsuccessful.
Sub cleanUp()
'
' cleanUp Macro
Dim rng As Range
    Set rng = ActiveDocument.Range

    With rng.Find
        .Text = "recommendation"
        Do While .Execute
            rng.Expand wdParagraph
            rng.Delete
            rng.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window

0
 

Author Comment

by:brothertruffle880
ID: 37706429
Sometimes, the line of text contains other characters immediately before or after the word recommendation.  I'd like these deleted too.

Example of the lines  i'd like deleted:

recommendation^p
recommendation ^p          space character after the word recommendation and before the hard return
1 recommendation^p
1 recommendation ^p       space character after the word recommendation and before the hard return
30 recommendations^p
30 recommendations ^p      space character after the word recommendation and before the hard return

I'm not familiar with the range object.  I'd like to understand it.  
Why do you use the range object instead of the selection object?  What's the difference?

Also, just so I have a better understanding, using my macro, how would I detect the end of the document, or, how would I detect no more instances of "recommendation" so that I could terminate the macro?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37706729
A range object defines a part of a document, but does not affect the display. The Selection object has a Range property, so it too can define a document part. You can have many range objects, but there is only one Selection object in any instance of the Word application.

It isn't always possible to Find all the target instances in one operation and not to pick up unwanted ones even with wildcards, so multiple goes might be necessary.

Sub cleanUp2()
'
' cleanUp Macro
    Dim i As Integer
    Dim rng As Range
    Dim strArray() As String
    strArray = Split("recommendation^p,recommendation ^p, recommendations^p, recommendations ^p", ",")
    Set rng = ActiveDocument.Range
    For i = 0 To 3
        With rng.Find
            .Text = strArray(i)
            Do While .Execute
                rng.Expand wdParagraph
                rng.Delete
                rng.End = ActiveDocument.Range.End
            Loop
        End With
    Next i
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37706742
When I started explaining about Ranges, I had the idea that it would be best put in an article, so I submitted one here:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/A_9909-Using-the-Word-Range-object-in-code.html

It hasn't been accepted yet, so I don't know if it is visible to you.
0
 

Author Closing Comment

by:brothertruffle880
ID: 37707195
Spectacular!  Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
help with flyer 3 81
The blinking Office button 4 45
Converting large .PDF files. 6 66
Need to style Hyperlink using Word Mac 2011 5 59
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question