Solved

Word 2003 Macros - Replacing words throughout a document

Posted on 2012-03-10
5
328 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
[X]
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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Suggested Courses

623 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