PDA

View Full Version : [SOLVED:] Find and replace macro to start on page 4



Knightfall
01-09-2018, 03:41 PM
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!

macropod
01-09-2018, 04:14 PM
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

Knightfall
01-09-2018, 05:12 PM
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.

macropod
01-09-2018, 05:53 PM
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.

Knightfall
01-09-2018, 06:08 PM
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.

macropod
01-09-2018, 06:19 PM
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?

Knightfall
01-09-2018, 06:37 PM
It's going to say "Error! Bookmark Not Defined!"

Shoot. It appears I have a new problem.

macropod
01-09-2018, 06:53 PM
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.

Knightfall
01-09-2018, 07:01 PM
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."

macropod
01-09-2018, 07:24 PM
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.



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).

Knightfall
01-09-2018, 07:40 PM
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.

macropod
01-09-2018, 07:52 PM
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.

Knightfall
01-10-2018, 08:17 AM
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?

macropod
01-10-2018, 02:43 PM
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.

Knightfall
01-10-2018, 03:04 PM
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!