|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How to Turn Off Message "Search from the beginning of a document" in a Macro
I've put together a macro (attached) for our group of editors to use for cleaning up documents to:
How do I suppress those messages, yet ensure that Word continues searching from the beginning? It's a pain to keep clicking OK (and not very elegant besides). Also, if you have any suggestions about re-writing the code to make it cleaner, I'd appreciate your help. |
#2
|
|||
|
|||
You realize that your two spaces after periods could make a mess out of your text (things like Mr. Smith, Dr. Adams, Mrs. Jones etc.)
When you need to post code. Use the "Go Advanced" and wrap your code in code tags. Code:
Sub FixSpacing() Dim oRng As Word.Range Set oRng = ActiveDocument.Range Set oRng = ActiveDocument.Range With oRng.Find 'Spaces after colon to 2 spaces .ClearFormatting .Replacement.ClearFormatting .Text = "(:)( {1,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With TwoSpacesAfterSentence Set oRng = ActiveDocument.Range 'Spaces after a comma or numerical digit and between words to 1 space With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([,0-9A-Za-z])( {2,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range 'Space before to no space. With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "( {1,9})([,.:;])" .Replacement.Text = "\2" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = ".." .Replacement.Text = "." .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub Sub TwoSpacesAfterSentence() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .MatchWildcards = True .Text = "(*{2})([.\!\?]) ([A-Z])" .Replacement.Text = "\1\2 \3" 'Two spaces between 2 and \ .Execute Replace:=wdReplaceAll .Text = "([.\!\?]) {3,}([A-Z])" .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll 'This should prevent most cases of improper double spacing 'in names (e.g., F. Lee Bailey, George W. Bush, etc.) .Text = "([!A-Z][A-Z].) ([A-Z])" 'Two spaces between ) and ( .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
I realized that the two spaces would affect the handful of documents that have a reference page with 3-6 references, and have alerted the other editors to this.
Didn't know about Go Advanced -- will do that in the future. I scanned the code you prepared. Wow. I'll study it more carefully tomorrow morning to get a better understanding. Thank you so much! Will be back in touch. |
#4
|
||||
|
||||
Frankly, unless you're using left-aligned text, I doubt anyone reading the printed documents could tell whether a given sentence was followed by one or two spaces. Having two spaces after each sentence in electronic documents that are shared with others will probably just annoy them - it's a throwback to the old fixed-pitch typewriter days and shouts out to the world that the one behind it has Neanderthal relatives. AFAIK no professional publisher uses it ...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Loved your comment about "Neanderthal relatives." Yes, this practice is indeed antediluvian. My colleague editors will feel vindicated when they see your comments. We're just hoping our customer doesn't go searching in US government surplus warehouses to supply us with IBM Selectrics with correction tape! (We do use left-aligned text in all documents.)
When we first heard about the directive, I went searching online, and found a lengthy blog about the horrors of the American Psychological Association's recent change to its guidelines to put two spaces after periods. Don't know what could have possessed them, and I sure hope AP doesn't follow their lead. But, of course, to us it makes no difference. We have to dance to the tune given to us, regrettably. |
#6
|
|||
|
|||
Greg: Thank you for very much for the macro. You've thought of things to search for that I didn't imagine. And I sure couldn't have figured this out! It works great, except for two things I didn't think of: Putting two spaces after a period and a quote mark -- ." and two spaces after a sentence in parentheses followed by a new sentence. -- .) A-Z (see text text below)
Could you help me with searching for those as well? PS: I changed the sub titled TwoSpacesAfterSentence to TwoSpacesAftSentence because of an "ambiguous" message. Test text is below (not sure I was supposed to put quote tags): Quote:
|
#7
|
|||
|
|||
Code:
Sub FixSpacing() Dim oRng As Word.Range Set oRng = ActiveDocument.Range Set oRng = ActiveDocument.Range With oRng.Find 'Spaces after colon to 2 spaces .ClearFormatting .Replacement.ClearFormatting .Text = "(:)( {1,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With TwoSpacesAfterSentence Set oRng = ActiveDocument.Range With oRng.Find 'Spaces after ." and .) to 2 spaces .ClearFormatting .Replacement.ClearFormatting .Text = "(."")( {1,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find 'Spaces after ." and .) to 2 spaces .ClearFormatting .Replacement.ClearFormatting .Text = "([.\)])( {1,9})([A-Z])" .Replacement.Text = "\1 \3" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range 'Spaces after a comma or numerical digit and between words to 1 space With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([,0-9A-Za-z])( {2,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range 'Space before to no space. With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "( {1,9})([,.:;])" .Replacement.Text = "\2" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = ".." .Replacement.Text = "." .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub Sub TwoSpacesAfterSentence() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .MatchWildcards = True .Text = "(*{2})([.\!\?]) ([A-Z])" .Replacement.Text = "\1\2 \3" 'Two spaces between 2 and \ .Execute Replace:=wdReplaceAll .Text = "([.\!\?]) {3,}([A-Z])" .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll 'This should prevent most cases of improper double spacing 'in names (e.g., F. Lee Bailey, George W. Bush, etc.) .Text = "([!A-Z][A-Z].) ([A-Z])" 'Two spaces between ) and ( .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#8
|
|||
|
|||
Greg: Again, thank you. I discovered in looking at the results, that I had overlooked two other possible things to look for. Spaces after ," and space between a comma and a quote -- , "
Trying to do this on my own with your models, I created the following, but my syntax must be wrong, because it doesn't work: Code:
'Space after ," to one space With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([,""])( {2,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range I tried inserting "" in the following of your code, but no change: Code:
'Space before to no space. Code:
With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "( {1,9})(["",.:;])" .Replacement.Text = "\2" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With |
#9
|
|||
|
|||
Code:
ting .Replacement.ClearFormatting .Text = "(."")( {1,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find 'Spaces after ." and .) to 2 spaces .ClearFormatting .Replacement.ClearFormatting .Text = "([.\)])( {1,9})([A-Z])" .Replacement.Text = "\1 \3" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range 'Spaces after a comma or numerical digit and between words to 1 space With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([,0-9A-Za-z])( {2,9})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range 'Space before to no space. With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "( {1,9})([,.:;])" .Replacement.Text = "\2" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With 'Space before close quote to no space. With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "( {1,9})(" & Chr(148) & ")" .Replacement.Text = "\2" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With 'Space after comma and closed quote to one space. With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "(," & Chr(148) & ")( {2,})" .Replacement.Text = "\1 " .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = ".." .Replacement.Text = "." .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub Sub TwoSpacesAfterSentence() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .MatchWildcards = True .Text = "(*{2})([.\!\?]) ([A-Z])" .Replacement.Text = "\1\2 \3" 'Two spaces between 2 and \ .Execute Replace:=wdReplaceAll .Text = "([.\!\?]) {3,}([A-Z])" .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll 'This should prevent most cases of improper double spacing 'in names (e.g., F. Lee Bailey, George W. Bush, etc.) .Text = "([!A-Z][A-Z].) ([A-Z])" 'Two spaces between ) and ( .Replacement.Text = "\1 \2" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#10
|
|||
|
|||
Greg: Quite frankly, I am humbled by the amount of work you have done creating the FixSpacing macro. What I didn't realize I was asking for was a long way from how to turn off messages about searching from the beginning.
I did a doc comparison to see what you did and now see that I should have done a more careful job of identifying patterns at the gitgo -- and I that surely have a lot to learn. There was some missing code at the top, that I replaced using the doc comparison, and, of course, it runs perfectly and cleans up the mess I intentionally created in my test document. I learned a lot from you just from looking at the code. I'm going to return to your site to see what you have there on searching. I had no idea I was asking for so much. Thank you so very much. |
#11
|
|||
|
|||
Glad I could help.
|
Tags |
clean up code, suppress messages |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
can't turn off "same as previous" | traumatiziert | Word | 8 | 12-12-2012 03:31 AM |
Instant Search's "Display search results as I type when possible" with Exchange | lwc | Outlook | 0 | 06-01-2011 01:56 AM |
Instant search not functional because of continuing "indexing" | arrigo | Outlook | 1 | 12-07-2010 09:13 PM |
Outlook search defaulting to "All Mail Items"??? | Ninafel | Outlook | 0 | 12-02-2010 02:01 PM |
Want to search for "class", replacing with document property | YetAnotherAuthor | Word | 0 | 10-30-2009 09:43 AM |