Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-23-2020, 08:24 AM
AlfredStreich AlfredStreich is offline Macro for Find/replace to remove space before Hard Return Windows 10 Macro for Find/replace to remove space before Hard Return Office 2019
Novice
Macro for Find/replace to remove space before Hard Return
 
Join Date: Jan 2020
Posts: 4
AlfredStreich is on a distinguished road
Default Macro for Find/replace to remove space before Hard Return

I have been trying to set up a Word Macro to remove a space before a hard return. I am able to get it to work on the main text properly, but when I added lines to apply it to the footnotes, it doesn't work properly. Instead of just removing the space it removes the space and then after the hard return, it adds a space and a hard return. I can't figure out how to tweak the text of the macro to resolve this. Here is the macro I am trying to use:
Sub SpaceBeforeHardReturn()
'
' SpaceBeforeHardReturn Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting


Set Footnotes = ActiveDocument.StoryRanges(wdFootnotesStory)
Footnotes.Find.ClearFormatting
Footnotes.Find.Replacement.ClearFormatting
With Footnotes.Find
.Text = " ^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Footnotes.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
End Sub

Thanks for any advice.
Reply With Quote
  #2  
Old 01-23-2020, 09:23 AM
gmaxey gmaxey is offline Macro for Find/replace to remove space before Hard Return Windows 10 Macro for Find/replace to remove space before Hard Return Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
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

I suppose it is because of some of the "SMART" features of Word sometimes deleting spaces in front of paragraph marks can be confounding. This is exacerbated when dealing with the footnote storyrange because it is a Medusa in itself. You might try:

Code:
Sub ScratchMacro()
Selection.Find.Replacement.ClearFormatting
Dim oRng As Range
Set oRng = ActiveDocument.StoryRanges(wdFootnotesStory)
  With oRng.Find
    .ClearFormatting
    .Text = " ^p"
    While .Execute
      oRng.Characters.First.Text = "~"
      oRng.Characters.First.Delete
      oRng.Collapse wdCollapseEnd
    Wend
  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 01-23-2020, 09:57 AM
AlfredStreich AlfredStreich is offline Macro for Find/replace to remove space before Hard Return Windows 10 Macro for Find/replace to remove space before Hard Return Office 2019
Novice
Macro for Find/replace to remove space before Hard Return
 
Join Date: Jan 2020
Posts: 4
AlfredStreich is on a distinguished road
Default

That seems to work. Thanks.

Since I'm still new to working in VBA, what is the purpose of the:
Quote:
Code:
oRng.Characters.First.Text = "~"
      oRng.Characters.First.Delete
      oRng.Collapse wdCollapseEnd
Reply With Quote
  #4  
Old 01-23-2020, 12:09 PM
gmaxey gmaxey is offline Macro for Find/replace to remove space before Hard Return Windows 10 Macro for Find/replace to remove space before Hard Return Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
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

Well with testing here, I first had to replace the "space" preceding the paragraph mark with a temporary character "~" and then delete it. Otherwise the space wouldn't be deleted.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 01-23-2020, 03:59 PM
gmaxey gmaxey is offline Macro for Find/replace to remove space before Hard Return Windows 10 Macro for Find/replace to remove space before Hard Return Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
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

Okay, it appears it is the option "Smart Cut and Paste that causes the issue with not being able to delete the space directly:


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim bSCP As Boolean
  bSCP = Options.PasteSmartCutPaste
  Options.PasteSmartCutPaste = False
  Set oRng = ActiveDocument.StoryRanges(wdFootnotesStory)
  With oRng.Find
    .Text = " ^p"
    While .Execute
      oRng.Characters.First.Delete
      oRng.Collapse wdCollapseEnd
    Wend
  End With
  Options.PasteSmartCutPaste = bSCP
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for Find/replace to remove space before Hard Return Find / Replace hard Carriage Return with Line break. GreenBoy Word 2 03-11-2018 02:32 AM
Macro for Find/replace to remove space before Hard Return Code to find numerical string + space throughout document & replace them with Comma Robert Kay Word VBA 6 02-21-2018 04:41 PM
Macro for Find/replace to remove space before Hard Return Find and Replace with space cheech1981 Word 3 03-29-2017 02:33 PM
Hard page return? markg2 Outlook 0 09-11-2012 08:49 AM
Macro for Find/replace to remove space before Hard Return remove color from find/replace? Cobb78 Word 1 05-26-2012 06:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:37 PM.


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