Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-29-2018, 05:50 PM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 08-29-2018, 06:32 PM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #3  
Old 08-29-2018, 08:01 PM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default

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).
Reply With Quote
  #4  
Old 08-29-2018, 09:36 PM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #5  
Old 08-29-2018, 11:47 PM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 08-30-2018, 12:19 AM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Did you at least try the macro I supplied?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-30-2018, 09:10 AM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 08-30-2018, 02:23 PM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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:
looking for a way to remove any trailing spaces from all footnotes.
and:
Quote:
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
It also correctly handles the situation where:
Quote:
their spaces may be separating words between Hyperlinks and non hyperlink text
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-30-2018, 02:58 PM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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:

and:
Exactly this. Iterating through paragraphs of the wdFootnotesStory will not indicate to which footnote they belong.


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).
Reply With Quote
  #10  
Old 08-30-2018, 03:35 PM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #11  
Old 08-30-2018, 05:28 PM
risomd risomd is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2016
Novice
Removing trailing spaces from footnotes
 
Join Date: Aug 2018
Posts: 6
risomd is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 08-30-2018, 06:00 PM
macropod's Avatar
macropod macropod is offline Removing trailing spaces from footnotes Windows 7 64bit Removing trailing spaces from footnotes Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing trailing spaces from footnotes Removing spaces in activedocument after empty bookmarks faustino909 Word VBA 2 08-03-2018 01:34 PM
Removing trailing spaces from footnotes How to remove trailing & leading spaces in a cell? LearnerExcel Excel Programming 8 02-04-2018 08:22 PM
Removing trailing spaces from footnotes removing spaces in cell Tonykiwi Excel 4 11-06-2016 09:30 PM
Removing trailing spaces from footnotes 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

Other Forums: Access Forums

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