Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-04-2013, 05:08 AM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default 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:
  • Add two spaces after a period (a recent "edict" that we all hate!)
  • Remove spaces between words and before punctuation marks of various types
  • Search for two periods and replace with one
Because I don't know how to write "elegant" code, I created separate macros and strung them together in a single macro. It gets the job done with spaces, but each time one of the macros runs, there's a message about continuing the search from the begining of the document (I used Replace all).



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.
Attached Files
File Type: docx AddRemoveSpaces Macro.docx (17.5 KB, 8 views)
Reply With Quote
  #2  
Old 10-04-2013, 11:32 AM
gmaxey gmaxey is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows 7 32bit How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-04-2013, 02:35 PM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 10-04-2013, 02:57 PM
macropod's Avatar
macropod macropod is online now How to Turn Off Message "Search from the beginning of a document" in a Macro Windows 7 32bit How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 10-12-2013, 01:59 AM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 10-12-2013, 03:51 AM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default

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:
You can configure Apache HTTP Server as a reverse proxy server with SSL encryption , web content caching and user authentication , and insertion of dynamic user roles .. It’s a logical choice for states using Apache/Tomcat architecture , but is not required as long as a proxy server with equivalent capabilities is deployed. (See Section 1.5.4, “Reverse Proxy Server ,” for a discussion of proxy servers.) You can configure Apache HTTP Server as a reverse proxy server with SSL encryption , web content caching and user authentication, and insertion of dynamic user roles . It’s a logical choice for entities using Apache/Tomcat architecture , but is not required as long as a proxy server with equivalent capabilities is deployed . (See Section 1.5.4, “Reverse Proxy Server ,” for a discussion of proxy servers.) You can configure Apache HTTP Server as a reverse proxy server with SSL encryption , web content caching and user authentication , and insertion of dynamic user roles. It’s a logical choice for entities using Apache/Tomcat architecture , but is not required as long as a proxy server with equivalent capabilities is deployed. (See Section 1.5.4, “Reverse Proxy Server , ” for a discussion of proxy servers.) Chart 1-1 : Description of This and That. Figure 1-1 : Depiction of the Other.. F. Lee Bailey, George W. Bush
Reply With Quote
  #7  
Old 10-12-2013, 06:40 AM
gmaxey gmaxey is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows 7 32bit How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 10-13-2013, 07:40 AM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default

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
Also, the macro does not deal with a space before a quote. Here's the example: , "
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
I promise this is my last question! At least on this topic.
Reply With Quote
  #9  
Old 10-13-2013, 11:06 AM
gmaxey gmaxey is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows 7 32bit How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 10-15-2013, 01:56 PM
sleake sleake is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows XP How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2007
Advanced Beginner
How to Turn Off Message "Search from the beginning of a document" in a Macro
 
Join Date: Jul 2011
Posts: 60
sleake is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 10-15-2013, 03:43 PM
gmaxey gmaxey is offline How to Turn Off Message "Search from the beginning of a document" in a Macro Windows 7 32bit How to Turn Off Message "Search from the beginning of a document" in a Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Glad I could help.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
clean up code, suppress messages



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Turn Off Message "Search from the beginning of a document" in a Macro 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
How to Turn Off Message "Search from the beginning of a document" in a Macro 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft