Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-05-2023, 07:42 AM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question Remove spaces before each paragraph mark in text and footnotes

The following code does its job, but is there a more efficient way to obtain the same result? Thanks!





Code:
Dim oRng As Range
Dim iType As Integer
  For iType = 1 To 2
Set oRng = ActiveDocument.StoryRanges(iType)
  With oRng.Find
    .ClearFormatting
    .Text = " ^p"
    While .Execute
      oRng.Characters.First.Delete
      oRng.Collapse wdCollapseEnd
    Wend
  End With
    Next iType
Reply With Quote
  #2  
Old 10-05-2023, 08:05 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! As far as I know, Find-Replace is the fastest method:
Code:
Sub Del_Paras_Last_Space

Dim oRng As range
Dim iType As Integer
  For iType = 1 To 2
    Set oRng = ActiveDocument.StoryRanges(iType)
    With oRng.Find
       .ClearFormatting
       .Replacement.ClearFormatting
       .text = " ^p"
       .Replacement.text = "^p"
       .Matchwildcards = False
       .Execute Replace:=wdReplaceAll
    End With
  Next iType
End sub
Reply With Quote
  #3  
Old 10-05-2023, 09:00 AM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi Vivka! It is indeed the fastest method, but it doesn't work in the footnotes, where ^p cannot be replaced. Thanks!
Reply With Quote
  #4  
Old 10-05-2023, 09:36 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, I'm almost sure, the code works on footnotes! The possible reason it doesn't work in your doc is your footnotes are not footnotes proper. To find out their StoryType, place the cursor anywhere on the footnote and run the following code:
Sub Story_Type_Sel()
MsgBox selection.range.StoryType
End Sub
The footnote, as you know, will have value 2. If you get a different value, change the main macro respectively. I had this kind of problem until I changed the value to 3 (wdEndnotesStory).
However, I admit I may be wrong.
Reply With Quote
  #5  
Old 10-05-2023, 10:07 AM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks! But my footnotes have the value 2. If I change the value, I get an error. The Find-Replace method does not work on paragraph marks in the footnotes. Perhaps you can find a workaround?
Reply With Quote
  #6  
Old 10-05-2023, 10:57 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, I've tested the macro and it does the job on footnotes nicely. I don't know what may be wrong with your file (not macro!) without seeing it. It occured to me that the problem may be with multiple spaces preceding paragraph marks. The macro deletes only one space at one run, which is too small to be noticed. Try activating Show All Characters to see all invisible characters.
Reply With Quote
  #7  
Old 10-05-2023, 02:35 PM
Guessed's Avatar
Guessed Guessed is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

If you want to find multiple spaces and/or tabs you can search for "^w^p".
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 10-05-2023, 11:48 PM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi Vivka! Of course there are no multiple spaces in my case (thanks to Guessed for "^w^p"!). If you mean space+^p within a footnote, I agree with you that Find-Replace does its job on footnotes nicely. But what I meant from the start is space+^p at the end of a footnote. In my footnotes there is only one ^p at the end. And this you cannot replace. If you try a Find-Replace without using a macro, you will see what I mean. The code I originally posted works even for the ^p at the end, because it deletes the space. But I wanted a faster solution. Perhaps you could modify the following code (which inserts spaces before ^p in the text and footnotes) to make it DELETE spaces before ^p.

Code:
Sub InsertSpace()
Dim aRng As Range
Dim iType As Integer
Dim Para As Paragraph
  For iType = 1 To 2
Set aRng = ActiveDocument.StoryRanges(iType)
For Each Para In aRng.Paragraphs
Para.Range.Characters.Last.InsertBefore " "
Next Para
Next iType
Set aRng = Nothing
End Sub
Reply With Quote
  #9  
Old 10-06-2023, 01:00 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! I think I've got your point! Without my explanations, try the following. If it is what you want, I will explain the logic later, if not, I'll save your and my time:
Code:
Sub X()

Dim oRng As range
Dim iType As Integer
  For iType = 1 To 2
    Set oRng = ActiveDocument.StoryRanges(iType)
    With oRng.Find
       .ClearFormatting
       .Replacement.ClearFormatting
       .text = "^w^p"
       .Replacement.text = "^p"
       .MatchWildcards = False
       .Execute Replace:=wdReplaceAll
    End With
  Next iType
  Set oRng = ActiveDocument.StoryRanges(2)
    With oRng.Find
       .ClearFormatting
       .Replacement.ClearFormatting
       .text = "^p^p"
       .Replacement.text = "^v"
       .MatchWildcards = False
       .Execute Replace:=wdReplaceAll
       .text = "^v"
       .Replacement.text = ""
       .Execute Replace:=wdReplaceAll
    End With
 Set oRng = Nothing
End Sub
Reply With Quote
  #10  
Old 10-06-2023, 01:34 AM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi Vivka! Sorry, it doesn't work in the footnotes. The only way out is to delete the space before ^p without replacing ^p.
Reply With Quote
  #11  
Old 10-06-2023, 01:41 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

This is exactly what the macro does. At least in my document. The macro does the job of your original code but much faster. Or maybe I don't understand your objective.
Reply With Quote
  #12  
Old 10-06-2023, 10:18 AM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi Vivka! I've tested again your code, but space+^p (the last and only ^p) in the footnotes remains intact. The same is true when I try this Find-Replace: "^w^p" ---> "^p" (the first part of your macro).
Reply With Quote
  #13  
Old 10-06-2023, 11:54 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobeNew, I've checked and retested my macro some 7 times without any problem. I'm lost for ideas. The only way out is seeing your file. Maybe there are some things I'm overlooking. Or at least paste here the before & desired outcome footnote strings. I got excited and take it as a challenge. Probably, it's a sort of childish behavior.
Reply With Quote
  #14  
Old 10-06-2023, 11:35 PM
RobiNew RobiNew is offline Remove spaces before each paragraph mark in text and footnotes Windows 10 Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
Remove spaces before each paragraph mark in text and footnotes
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question

Hi Vivka! Of course I will post an example of my text, but first I'd like to ask you again: since my footnotes do not contain multiple spaces, but only one space before the only ^p at the end, couldn't you simply modify the code here below so as to delete the space instead of inserting it? Thanks!

Code:
Sub InsertSpace()
Dim aRng As Range
Dim iType As Integer
Dim Para As Paragraph
  For iType = 1 To 2
Set aRng = ActiveDocument.StoryRanges(iType)
For Each Para In aRng.Paragraphs
Para.Range.Characters.Last.InsertBefore " "
Next Para
Next iType
Set aRng = Nothing
End Sub
Reply With Quote
  #15  
Old 10-07-2023, 12:46 AM
vivka vivka is offline Remove spaces before each paragraph mark in text and footnotes Windows 7 64bit Remove spaces before each paragraph mark in text and footnotes Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! It's quite simple:
replace:
Para.range.Characters.Last.InsertBefore " "
with:
Para.range.Characters.Last.Previous.Delete

But this method deletes any (not anly space!) before the para sign and it is not the fastest one. To delete only spaces, the following condition should be included, which also will make the code a millisecond slower:
If Para.range.Characters.Last.Previous = " " Then
Para.range.Characters.Last.Previous.Delete
End If


RobiNew, I really can't understand why the code from post 9 doesn't work for you! I have retested it this morning again - and no problem! I'm eager to see your footnote!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove spaces before each paragraph mark in text and footnotes How to remove gap between text and footnotes on Mac? gemmajackson7 Word 1 05-16-2021 07:26 AM
Remove spaces before each paragraph mark in text and footnotes How to Remove trailing spaces AND line breaks in a block of text JulianS96 Word 4 02-04-2020 04:20 AM
Remove spaces before each paragraph mark in text and footnotes Remove Paragraph / Spaces sharina1985 Mail Merge 1 10-05-2019 03:52 AM
big spaces appearing automatically in certain parts of the text - how to remove them pratodifuoco Word 2 05-26-2017 12:55 PM
editing text and remove spaces romanticbiro Word VBA 5 07-04-2014 07:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:10 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