![]() |
#1
|
|||
|
|||
![]()
Back once again. I can't this macro to run. It's to delete any spaces on either side of an em dash in a document. Seems simple enough. I started with a recorded one, then made alterations as I saw it wasn't working. It chokes on the line in red; no clue why, since I don't know VBA.
Thanks. Code:
Sub Ems() ' ' Ems Macro ' ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "(Chr(32)^+)" .Replacement.Text = "^+" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "(^+Chr(32))" .Replacement.Text = "^+" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Selection.Find.Execute Replace:=wdReplaceAll Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.Delete Unit:=wdCharacter, Count:=1 ActiveWindow.Close Application.Quit End Sub |
#2
|
||||
|
||||
![]()
The reason nothing happens is simple: your first 'Find' expression simply configures Word to find the first instance of a space before an em-dash - but then does nothing. Your second 'Find' expression then tries to find a space after an em-dash in the same selection. There is also no need for the parentheses.
Try: Code:
Sub Ems() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Wrap = wdFindContinue .MatchWildcards = False .Replacement.Text = "^+" .Text = " ^+" .Execute Replace:=wdReplaceAll .Text = "^+ " .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you, Paul. I'm not a coder and don't really your explanation, simply because I don't speak the language, which, like math or music, has it's own vocabulary, but as always I join the countless who owe you a huge debt of gratitude for generously sharing your exceptional expertise with us.
|
#4
|
||||
|
||||
![]()
When you record code it relies heavily on the selection object (what is currently selected). The Find dialog behaves differently with a selection vs an insertion point. If there is a selection of some text when you run a find, it only searches inside the selected text. However if the selection is collapsed, the search will either search across the whole document or down to the bottom or up to the top depending on your other settings.
Your code (as recorded) moves the selection object so subsequent searches start from different places and may indeed start with a selected range. If you keep the above info in your mind, stepping through your code will show you why your later searches fail to find the instances you seek. Paul's code avoids moving the selection object and hence is unaffected by those other factors.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
Thanks, Andrew. While I am familiar with the find/replace behavior and the difference between pre-selecting and not, I also know that sometimes a recording macro appears to pay no attention. Your explanation is very helpful, and makes me all the more grateful that individuals like you and Paul are so willing to share your knowledge and skills. I'll take another look at my macro and see if I can grasp what you've explained.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to Remove Paras with Line Spac 6; Macro to Convert Paragraphs to Outline Numbered | venganewt | Word VBA | 0 | 01-25-2022 06:28 PM |
Calling macro from loop-all-files macro only opens one file | Peterson | Word VBA | 0 | 03-27-2021 07:58 PM |
![]() |
Le_Blanc | Word VBA | 10 | 03-22-2021 11:38 AM |
Spell check macro within macro button field doesn't work in one document | samuelle | Word VBA | 0 | 07-20-2016 02:27 AM |
![]() |
LadyAna | Word | 1 | 12-06-2014 10:39 PM |