Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word 2003 Macros - Replacing words throughout a document

Posted on 2012-03-10
5
Medium Priority
?
330 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 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 …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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 …

721 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