Word find and replace macro

Night_Rain

Board Regular
Joined
Jan 1, 2005
Messages
181
I'm stumped, and hope someone here might be able to help. I have been trying to cut out extraneous code written by the macro recorder, and I find my paired down version does two things I do not want:

1. It inserts a "0" just prior to all of the inserted page-breaks
2. It sometimes fails to replace all, only replacing the first instance

Here is the current code:

Code:
Sub Futures_and_Expedites()
'5/23/05: rewritten  bpg
    Application.ScreenUpdating = False
    With ActiveDocument.Content.Find
        .Execute FindText:="prior to^p", ReplaceWith:="prior to ", Replace:=wdReplaceAll
        .Execute FindText:="there is^p", ReplaceWith:="there is ", Replace:=wdReplaceAll
        .Execute FindText:="invoice^p", ReplaceWith:="invoice ", Replace:=wdReplaceAll
        .Execute FindText:="necessary^p", ReplaceWith:="necessary ", Replace:=wdReplaceAll
        .Execute FindText:="can^p", ReplaceWith:="can ", Replace:=wdReplaceAll
        .Execute FindText:="do not^p", ReplaceWith:="do not ", Replace:=wdReplaceAll
        .Execute FindText:="provide  status", ReplaceWith:="provide the status", Replace:=wdReplaceAll
        .Execute FindText:="^p^p^p^p", ReplaceWith:="^p", Replace:=wdReplaceAll
        .Execute FindText:="^p^p^p", ReplaceWith:="^p", Replace:=wdReplaceAll
        .Execute FindText:="Please^p", ReplaceWith:="Please ", Replace:=wdReplaceAll
        .Execute FindText:="required^p", ReplaceWith:="required ", Replace:=wdReplaceAll
        .Execute FindText:="these^p", ReplaceWith:="these ", Replace:=wdReplaceAll
        .Execute FindText:="delivery^p", ReplaceWith:="delivery.^p", Replace:=wdReplaceAll
        .Execute FindText:="Thank you,", ReplaceWith:="Thank you,^p^"
        .Execute FindText:="posted.^p", ReplaceWith:="posted.^p^p "
    End With
    With ActiveDocument.Content.Find
        .Execute FindText:="Contact: __________________", ReplaceWith:="", Replace:=wdReplaceAll
        .Execute FindText:=Date & "                        Future Delivery", ReplaceWith:="^m" _
            & Date & "                        Future Delivery", Replace:=wdReplaceAll
        .Execute FindText:=Date & "                      Purchasing", ReplaceWith:="^m" & Date _
            & "                      Purchasing", Replace:=wdReplaceAll
    End With
    Selection.HomeKey Unit:=wdStory
    Selection.Delete wdCharacter, 2
    With ActiveDocument.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = InchesToPoints(0.2)
        .BottomMargin = InchesToPoints(0.2)
        .LeftMargin = InchesToPoints(0.25)
        .RightMargin = InchesToPoints(0.25)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
        .SectionStart = wdSectionNewPage
    End With
    ActiveWindow.View.Type = wdPageView
End Sub

What is a clean why to code multiple replace alls?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I've found something that seems to work a bit better, but I am still getting zeroes inserted before the page breaks. Any ideas?

Code:
Sub Expedite()
    Dim FindValue(1 To 9)
    Dim ReplaceValue(1 To 9)
    Dim i As Byte
    
    Application.ScreenUpdating = False
            
    'define values to find and replace
    FindValue(1) = "to^p"
    FindValue(2) = "is^p"
    FindValue(3) = "invoice^p"
    FindValue(4) = "necessary^p"
    FindValue(5) = "can^p"
    FindValue(6) = "not^p"
    FindValue(7) = "Contact: __________________"
    FindValue(8) = "^p^p^p^p"
    FindValue(9) = Date & "                      Purchasing"
    ReplaceValue(1) = "to "
    ReplaceValue(2) = "is "
    ReplaceValue(3) = "invoice "
    ReplaceValue(4) = "necessary "
    ReplaceValue(5) = "can "
    ReplaceValue(6) = "not "
    ReplaceValue(7) = ""
    ReplaceValue(8) = "^p"
    ReplaceValue(9) = "^m" & Date & "                      Purchasing"
    
    'run loop to execute find/replace
    For i = 1 To 9
        With Selection.Find
            .Text = FindValue(i)
            .Replacement.Text = ReplaceValue(i)
            .Execute replace:=wdReplaceAll
        End With
    Next 'i
    
    'make sure first page is selected
    Selection.HomeKey Unit:=wdStory
    'page setup
    With ActiveDocument.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = InchesToPoints(0.2)
        .BottomMargin = InchesToPoints(0.2)
        .LeftMargin = InchesToPoints(0.25)
        .RightMargin = InchesToPoints(0.25)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
        .SectionStart = wdSectionNewPage
    End With
    ActiveWindow.View.Type = wdPageView
End Sub
 
Upvote 0
Sorry, Night rain. I know it's an old thread, but I've looked and I see nothing in your code that should cause the zeroes.
 
Upvote 0
LOL... thanks. I've remained baffled, myself. I've found I can get rid of them using another find and replace searching for "0^m" and substituting "^m", so I have a workaround, at least. Their appearance seems a bit inconsistent, too. It is a very old version of Word. I am hoping it will just vanish when we get the 2003 upgrade rolled out.
 
Upvote 0

Forum statistics

Threads
1,224,851
Messages
6,181,395
Members
453,034
Latest member
mikdadhussain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top