#1
|
|||
|
|||
Removing trailing spaces from footnotes
I am looking for a way to remove any trailing spaces from all footnotes.
I thought I would try to iterate through them and check if the last character was a space (or other) and delete it, but this isn't working. It also throws a "Cannot edit Range" Error if the last character of a footnote is within a Hyperlink's text (blue and underlined) and is a space. What is the right way to approach this? Footnotes can have multiple hyperlinks and the last character may be within one of them and be a space or other character I want rid of? I've also tried just .Find'ing within the footnote's range, but I can't seem to only remove the trailing space(s). Some sample code that fails: Code:
For i = 1 To ActiveDocument.Footnotes.Count Dim fn As Object Set fn = ActiveDocument.Sections(1).Range.Footnotes(i) myMax = fn.Range.Characters.Count For j = myMax To 1 Step -1 myChar = fn.Range.Characters(j) If myChar = Chr(32) Then myLong = fn.Range.Characters(j).Delete 'This fails if last char is space within a hyperlink Else Exit For End If Next Next |
#2
|
||||
|
||||
Except for ranges terminated by hyperlinks, simply selecting the range (which you could do for all footnotes by clicking on any footnote, then pressing Ctrl-A), then centring the range before reverting it to its original alignment will remove all leading/trailing spaces. No macros required. For spaces in hyperlinks, you'll need to edit the hyperlink display text and, if the space shouldn't be part of the actual hyperlink, the hyperlink itself. A macro could be used for that.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I am trying to make it a macro as it is part of a larger clean-up macro.
I am unsure how to determine if the last space(s) in a footnote's text is part of a hyperlink's .TextToDisplay or if it is just regular text (the Range's "Text" or "FormattedText" or "Characters.Last")? And I'm not sure why the code I posted doesn't delete the last space character (fn.Range.Characters(j).Delete). I created a sample document with some footnotes and added spaces to the end of them. The code runs but doesn't delete the space at the end. It will delete other characters (change "j" to "5", and it deletes a whole range for some reason, so I don't think this is the right approach). |
#4
|
||||
|
||||
Try:
Code:
Sub Demo() Dim Hlnk As Hyperlink, Para As Paragraph With ActiveDocument.StoryRanges(wdFootnotesStory) For Each Hlnk In .Hyperlinks With Hlnk .TextToDisplay = Trim(.TextToDisplay) .Address = Trim(.Address) If .Range.Characters.Last.Next Like "[0-9A-Za-z(]" Then .Range.Characters.Last.Next.InsertBefore " " End If End With Next For Each Para In .Paragraphs With Para.Range Do While .Characters.Last.Previous.Text = " " .Characters.Last.Previous.Text = vbNullString Loop End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Using the wdFootnotesStory was something I also explored, but it didn't let me focus on individual footnotes. I'm trying to get fine grained control, and am removing the end spaces to learn how best to do that.
If a footnote ends in space(s) then I want to remove those spaces, hyperlink or not. But the last space(s) might not be in a Hyperlink.TextToDisplay, so I do not want to trim spaces from all hyperlinks since their spaces may be separating words between Hyperlinks and non hyperlink text. (A footnote can have many hyperlinks within text) But if the last space is not in a hyperlink, and by removing that space the end space may then become one at the end of a Hyperlink, and then it would need to be removed. Same with paragraphs. A footnote could have multiple paragraphs but only the trailing space(s) at the end of the final one of the footnote should be removed. |
#6
|
||||
|
||||
Did you at least try the macro I supplied?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Yes, of course. Thank you. Your macro is great, but it does trim "everything" (all paragraphs in a footnote, some hyperlinks have a space removed when they shouldn't) and doesn't give full conditional footnote-level control to the macro which could then be used for other characters & issues (like stripping spaces from the beginning of the footnote text, ...).
But it is better than what I had. I'll keep working on it. |
#8
|
||||
|
||||
So how would the macro be supposed to tell when a hyperlink shouldn't have the trailing space removed, or when a paragraph in a footnote shouldn't have trailing spaces removed? It seems to me you want it to do what you asked, except when you don't... And you did say you were:
Quote:
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Quote:
Iterating the footnotes and their paragraphs lets you determine which is the last paragraph of a footnote (see my initial post). I will guess that the last hyperlink of the last paragraph of a footnote is the one to potentially trim *if* there isn't any non-hyperlink-text after it (once the last paragraph's characters have been trimmed). |
#10
|
||||
|
||||
Perhaps you should ask yourself when is it ever correct for a paragraph to end with trailing spaces or for a hyperlink to start or end with one. I know of no such situation. It seems to me your faulting the code I posted for no good reason. If anything, the code I posted might have exposed flaws in your footnote content and/or formatting. The solution is to fix your footnotes, not criticise the code for exposing the flaws. That said, you might try:
Code:
Sub Demo() Dim Hlnk As Hyperlink, FtNt As Footnote, Rng As Range With ActiveDocument For Each FtNt In .Footnotes Set Rng = FtNt.Range.Paragraphs.Last.Range With Rng If .Hyperlinks.Count > 0 Then With .Hyperlinks(.Hyperlinks.Count) If .Range.End = Rng.End - 1 Then .TextToDisplay = Trim(.TextToDisplay) .Address = Trim(.Address) End If End With End If Do While .Characters.Last.Previous.Text = " " .Characters.Last.Previous.Text = vbNullString Loop End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
The documents are from others so I must deal with what I'm given. It may end up being better to trim the ends of all paragraphs, but first I'd like to figure out how to do it reliably in all cases.
Your macro returns "Run-time error 6028: The Range cannot be deleted" for this footnote: Code:
Sub AddF() With ActiveDocument.Footnotes .Add Range:=Selection.Range, Text:=" " .Item(1).Range.Hyperlinks.Add Anchor:=.Item(1).Range, Address:="FN " .Item(1).Range.InsertAfter (" ") End With End Sub |
#12
|
||||
|
||||
Your macro is contriving a situation where, not only are there empty spaces at the end of the footnote, but there are also empty spaces in the hyperlink you create before those empty spaces. I am not interested in playing with such contrivances. I have already given you code in post #4 that correctly handles all footnotes, regardless of you having to deal with what you're given.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Removing spaces in activedocument after empty bookmarks | faustino909 | Word VBA | 2 | 08-03-2018 01:34 PM |
How to remove trailing & leading spaces in a cell? | LearnerExcel | Excel Programming | 8 | 02-04-2018 08:22 PM |
removing spaces in cell | Tonykiwi | Excel | 4 | 11-06-2016 09:30 PM |
Mail Merge - Trailing Spaces | osucjb | Mail Merge | 5 | 10-28-2016 12:03 AM |
Word-Help with removing unwanted spaces in text | greshoff | Word | 9 | 12-30-2011 03:24 PM |