Solved

Word 2003 Macros - Replacing words throughout a document

Posted on 2012-03-10
5
325 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

808 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