Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-18-2022, 02:18 PM
Ulodesk Ulodesk is offline Help, please, with macro bug Windows 10 Help, please, with macro bug Office 2016
Word 2013 Expert Cert
Help, please, with macro bug
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Help, please, with macro bug

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

Reply With Quote
  #2  
Old 02-18-2022, 04:47 PM
macropod's Avatar
macropod macropod is offline Help, please, with macro bug Windows 10 Help, please, with macro bug Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,372
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

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]
Reply With Quote
  #3  
Old 02-19-2022, 08:08 AM
Ulodesk Ulodesk is offline Help, please, with macro bug Windows 10 Help, please, with macro bug Office 2016
Word 2013 Expert Cert
Help, please, with macro bug
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 02-19-2022, 04:33 PM
Guessed's Avatar
Guessed Guessed is offline Help, please, with macro bug Windows 10 Help, please, with macro bug Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 02-21-2022, 06:46 AM
Ulodesk Ulodesk is offline Help, please, with macro bug Windows 10 Help, please, with macro bug Office 2016
Word 2013 Expert Cert
Help, please, with macro bug
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
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
Help, please, with macro bug Footnote extraction macro [Why is this macro so slow? / anyway to make it faster?] 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
Help, please, with macro bug Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:08 PM.


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