Solved

Word 2003 Macros - Replacing words throughout a document

Posted on 2012-03-10
5
322 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Spectacular!  Thanks!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now