Consulting

Results 1 to 15 of 15

Thread: Find and replace macro to start on page 4

  1. #1

    Find and replace macro to start on page 4

    Hello, I'm writing a macro to find a specific string of words within a document, and them delete them. I need it to start on page 4 of the document instead of on page 1, and I'm not sure how to code this.

    The working part of the macro is as follows:

          Selection.Find.ClearFormatting
       Selection.Find.Replacement.ClearFormatting
       With Selection.Find
           'replace the string below with the text you want removed
           .Text = "Appraisal Report Number:????????????"
           .Replacement.Text = ""
           .Forward = True
           .Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchWildcards = True
           .MatchSoundsLike = False
           .MatchAllWordForms = False
       End With
       Selection.Find.Execute Replace:=wdReplaceAll
    Is there a line I can add to command this macro to start on page 4, or more simply not run on page 3?

    Thanks in advance!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument
      Set Rng = .GoTo(What:=wdGoToPage, Name:=4)
      Rng.End = .Range.End
      With Rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Appraisal Report Number:????????????"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thank you very much for the response, however this did not seem to work. It could be because I am the most literal definition of a noob when it comes to programming; please forgive me.

    Basically, I have a table of contents on page 3 of a document. The same words that I am trying to delete also appear in the ToC. So my goal is to delete the words from the document while maintaining the same words in the ToC.

    This is what I have now:

    Sub AppraisalMac()
        Application.ScreenUpdating = False
        Dim Rng As Range
        With ActiveDocument
            Set Rng = .GoTo(What:=wdGoToPage, Name:=4)
            Rng.End = .Range.End
            With Rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Appraisal Report Number:????????????"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=wdReplaceAll
            End With
            With Rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Appraisal Item:"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=wdReplaceAll
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    When I run this macro as it stands, it successfully deletes the "Appraisal Report Number:" and the "Appraisal Item:" from the document, but it also deletes them from the ToC located on page 3. The MatchWildcards has to = True for the appraisal report number section as that line is followed by a 12 digits number.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
        Application.ScreenUpdating = False
        Dim Rng As Range
        With ActiveDocument
            Set Rng = .GoTo(What:=wdGoToPage, Name:=4)
            Rng.End = .Range.End
            With Rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Text = "Appraisal Report Number:????????????"
                .Replacement.Text = ""
                .Execute Replace:=wdReplaceAll
                .Text = "Appraisal Item:"
                .Execute Replace:=wdReplaceAll
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    As for your TOC, if it references content in the body of the document and you delete that content, the TOC, too, will lose those references as soon as it is refreshed. Conversely, a Find/Replace cannot permanently delete content from a TOC; anything deleted from a TOC will re-appear as soon as it is refreshed.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    This worked perfectly. Thank you so very much.

    The intention here is to not refresh the ToC. This document is being provided for a user who likely doesn't know what "web browser" means. They will not have any need, nor know how to refresh the ToC.

    Thanks again, this is a tremendous help.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Knightfall View Post
    The intention here is to not refresh the ToC. This document is being provided for a user who likely doesn't know what "web browser" means. They will not have any need, nor know how to refresh the ToC.
    And what do you suppose will happen if they print, or do a print preview of, the document?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    It's going to say "Error! Bookmark Not Defined!"

    Shoot. It appears I have a new problem.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The
    "Error! Bookmark Not Defined!" display is something I'd expect to see for a cross-reference to a deleted bookmark, not a TOC.
    You might try locking the TOC and any affected cross-references (e.g. by selecting it and pressing Ctrl-F11), but that will prevent TOC page #s etc. updating, and clicking on an affected cross-reference will simply take you to the top of the document, not to the location of original content.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    I obviously didn't expect to see it at all. I just want the document to printable once all macros are done being run. Clicking to go directly to the content is needed. Once the ToC pages are listed, there should be reason for the user to add to the document. The next step would be to print it to paper or pdf.

    I tried selecting the ToC and pressing CTRL-F11 and then running the macro, but when I go to print it still changes the page numbers to "Error! Bookmark not defined."

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Knightfall View Post
    Once the ToC pages are listed, there should be reason for the user to add to the document. The next step would be to print it to paper or pdf.
    Be that as it may, if you distribute a Word document, the pagination is liable to change as soon as the document is opened on a computer using a different print driver, throwing your TOC out of whack. That doesn't happen with PDFs.

    Quote Originally Posted by Knightfall View Post
    I tried selecting the ToC and pressing CTRL-F11 and then running the macro, but when I go to print it still changes the page numbers to "Error! Bookmark not defined."
    As I said, that's not an error one expects to see with a Word TOC (i.e. a TOC field). How did you create your TOC? Furthermore, if you locked a field before it showed that error, it could not display it after being locked - unless you unlocked it again (e.g. via Ctrl-Shift-F11).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    Quote Originally Posted by macropod View Post
    Be that as it may, if you distribute a Word document, the pagination is liable to change as soon as the document is opened on a computer using a different print driver, throwing your TOC out of whack. That doesn't happen with PDFs.
    This is another good point that I had not thought about ahead of time.

    I created the ToC using the built in tool in Word 2016. Reference > Table of Contents > Automatic Table 2. I have the title of each section stylized As Heading 1 or Heading 2, including the aforementioned lines I was trying to delete with the macro.

  12. #12
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Instead of using the built-in tool, which embeds the TOC in a content control that might compromise the lacking, etc., you'd do better to create your TOC via Ctrl-F9, then typing TOC between the field braces.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    Quote Originally Posted by macropod View Post
    Instead of using the built-in tool, which embeds the TOC in a content control that might compromise the lacking, etc., you'd do better to create your TOC via Ctrl-F9, then typing TOC between the field braces.
    I do not even know how to create a TOC using that method. I will look into this.

    Do you have any recommendations for help on this matter?
    Last edited by Knightfall; 01-10-2018 at 09:13 AM.

  14. #14
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It's as simple as described - Press Ctrl-F9 where you want the TOC to appear. That will create a pair of field braces (i.e. { }). Type 'TOC' between them, thus {TOC}, then press F9.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  15. #15
    Quote Originally Posted by macropod View Post
    It's as simple as described - Press Ctrl-F9 where you want the TOC to appear. That will create a pair of field braces (i.e. { }). Type 'TOC' between them, thus {TOC}, then press F9.
    Thank you, this worked. So now I think everything is working like I had hoped.
    I create a TOC using CTRL-F9, then F9. It creates the TOC.
    Then I select the entire TOC and press CTRL-F11 to lock it.
    Then I run the macro, which ignores the TOC, but deletes the specific words in the rest of the document
    Then when I click print, I no longer receive the "Error! Bookmark Not Defined." error.

    This seems to work perfectly now. I'm going to test this one a bunch of documents to see if I get any other unforeseen errors.

    Thanks again, you have been a tremendous help on this matter, and honestly I could not have done it without your help.

    Cheers!
    Last edited by Knightfall; 01-10-2018 at 03:40 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •