#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
Hi Vivka! It is indeed the fastest method, but it doesn't work in the footnotes, where ^p cannot be replaced. Thanks!
|
#4
|
|||
|
|||
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. |
#5
|
|||
|
|||
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?
|
#6
|
|||
|
|||
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.
|
#7
|
||||
|
||||
If you want to find multiple spaces and/or tabs you can search for "^w^p".
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
Hi Vivka! Sorry, it doesn't work in the footnotes. The only way out is to delete the space before ^p without replacing ^p.
|
#11
|
|||
|
|||
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.
|
#12
|
|||
|
|||
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).
|
#13
|
|||
|
|||
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.
|
#14
|
|||
|
|||
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 |
#15
|
|||
|
|||
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! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to remove gap between text and footnotes on Mac? | gemmajackson7 | Word | 1 | 05-16-2021 07:26 AM |
How to Remove trailing spaces AND line breaks in a block of text | JulianS96 | Word | 4 | 02-04-2020 04:20 AM |
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 |