Share via

Loop through Document: Find and Replace in a Selection

Anonymous
2015-09-25T16:28:09+00:00

Hi there, I use an Access database to merge client information to a number of letters. I select multiple records in access, and the merge outputs to one large document, that has a letter on each page. I have a macro to split the document into individual ones, but I want to use the "permit number" in the document name. The permit number is formatted with a forward slash (i.e. #100/15), and so it can't be saved as a file name. I tried to mask the permit number when merging to replace the "/" with "-" and that didn't work. So now I just want to write a new macro to loop through the top section of the document and remove the "/" in two places, but leave any other slashes in the rest of the letter as is.

So the question: How do I loop through a multi-page document, where I find and replace the "/" with "-" in a selection on each page? Is this doable? If so, how?

The point is do have this done in one click, I don't want to have to do it manually, it takes to long when I have 100+ letters to do each week.

Thanks in advance for the help!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2015-09-26T00:24:16+00:00

    See also my reply to your other thread: http://answers.microsoft.com/en-us/office/forum/office_2007-word/using-merge-fields-in-a-split-document-macro/d25dd623-d515-45dc-82d1-ac89e5fb24ea. The approach described there would obviate the need for this.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-09-25T18:35:41+00:00

    If the pattern contain the slashes you want to replace are consistent you could try:

    Sub ScratchMacro()

    'A basic Word macro coded by Greg Maxey

    Dim oRng As Word.Range

      Set oRng = ActiveDocument.Range

      With oRng.Find

        .Text = "(#[0-9]{3})(/)([0-9]{2})"

        .MatchWildcards = True

        .Replacement.Text = "\1-\3"

        .Execute Replace:=wdReplaceAll

      End With

    lbl_Exit:

      Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments