Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Modify a macro (2002 SP3)

    HansV posted the below macro for me a while ago. It's worked flawlessly, but I'd now like to hyperlink the footnotes to one another.

    I.e., "[1]" in the body of the text should link to the "[1]" at the bottom of the text.

    I'd like to link just the numbers, not the brackets.

    Many thanks!

    Sub Textnotes()
    ' Original version: Doug Robbins
    ' Adaptation: HansV

    Dim aFootNote As Footnote
    Dim rng As Range

    For Each aFootNote In ActiveDocument.Footnotes
    aFootNote.Range.Copy
    ActiveDocument.Range.InsertAfter vbCr & "[" & aFootNote.Index & "] "
    Set rng = ActiveDocument.Range
    rng.Collapse wdCollapseEnd
    rng.Paste
    aFootNote.Reference.InsertBefore "[" & aFootNote.Index & "]"
    Next aFootNote

    For Each aFootNote In ActiveDocument.Footnotes
    aFootNote.Reference.Delete
    Next aFootNote
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Modify a macro (2002 SP3)

    For others reading this: the thread Jonathan refers to is <post#=312620>post 312620</post#>.

  3. #3
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    Thanks Hans. I see that I actually made this same request in post 313256, which is in the thread you refer to.

    Still, does anyone know how to hyperlink the footnotes? Thanks.

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    Hi Jonathan,

    No small feat to create those hyperlinks. (Forgive me if you know all this, but I figure it's helpful to the group) To hyperlink to a location elsewhere in a document, that location has to be marked with a bookmark. A bookmark name must be unique in a document, can only contain letters, numbers and underscores, and can't begin with a number.

    There are two supporting functions after the main one that take a string and convert it to a valid bookmark. Putting an underscore at the beginning marks it has hidden. To ensure that each bookmark is unique, a four-digit random number is included in the bookmark name.

    The main macro visits each footnote, inserts the contents of the footnote at the end of the document much as the original macro did. But then a bookmark is placed around the footnote , and then a hyperlink to that bookmark is placed just after the original footnote reference. As a bonus, the footnote text is used as the hyperlink tooltip, so you get the same float-over effect as with footnotes.

    After all the hyperlinks are created, the footnotes in the document are deleted (deleting from the bottom up with a For Next loop is more reliable than a For Each, which may miss items when deleting).

    You might need to make minor adjustments to the rngReference range, where the hyperlink is inserted, depending on how the spacing is after footnotes in your document. This is quite a hack, but should get you most of the way to where you're going.

    HTH!

    <pre>Sub FootnotesToHyperlinks()
    ' Requires ConvertStringToValidBookmarkName
    ' Requires StripInvalidBookmarkCharacters
    Dim f As Footnote
    Dim sRef As String
    Dim sFnoteText As String
    Dim sBookmarkName As String
    Dim rng As Range
    Dim rngReference As Range
    Dim i As Integer
    Dim doc As Document

    Set doc = ActiveDocument

    For Each f In doc.Footnotes
    Set rng = doc.Range
    sRef = f.Index
    sFnoteText = f.Range.Text
    sRef = "[" & sRef & "]"
    sBookmarkName = ConvertStringToValidBookmarkName(sFnoteText)
    With rng
    .Collapse wdCollapseEnd
    .InsertAfter vbCr
    .Collapse wdCollapseEnd
    .InsertAfter sRef & sFnoteText
    End With
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rng
    Set rngReference = f.Reference.Duplicate
    rngReference.Collapse wdCollapseEnd
    doc.Hyperlinks.Add Anchor:=rngReference, _
    SubAddress:=doc.Bookmarks(sBookmarkName), _
    TextToDisplay:=sRef, _
    ScreenTip:=sFnoteText
    Next f

    For i = doc.Footnotes.Count To 1 Step -1
    doc.Footnotes(i).Delete
    Next i
    End Sub
    '
    Function ConvertStringToValidBookmarkName(ByVal str As String) As String
    str = StripInvalidBookmarkCharacters(str)
    str = Replace(str, " ", "_")
    str = "_" & str
    str = Int(9000 * Rnd + 100) & str
    str = "_" & str
    ConvertStringToValidBookmarkName = str
    End Function
    '
    Function StripInvalidBookmarkCharacters(ByVal str As String) As String
    Dim i As Integer
    For i = 33 To 255
    Select Case i
    Case 33 To 47, 58 To 64, 91 To 96, 123 To 255
    str = Replace(str, Chr$(i), vbNullString)
    End Select
    Next i
    StripInvalidBookmarkCharacters = str
    End Function
    </pre>


  5. #5
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    Hi Andrew,

    Wow--thanks so much! What an interesting process. I didn't know it was that complicated, though.

    It seems to work flawlessly -- though, as usual, I have one quibble. Would you please hyperlink the footnotes at the bottom (just the numbers, not the brackets) back to the footnotes in the body of the text?

    Again, many, many thanks!

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    So you're looking for a circular hyperlink? Not sure if that's possible, or a good idea. But if you want to try it, you could adapt the code I supplied to do it.

  7. #7
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    Circular hyperlinks, yes. Actually, Word does this automatically if you save a document with footnotes as a Web page.

    Nonetheless, I'm clueless as to which part of the code I should adapt, and where I should adapt it to [img]/forums/images/smilies/smile.gif[/img].

    Thanks.

  8. #8
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    Hi Jonathan,

    Replace the first of the three previous macros with the following to create the hyperlink back into the text.

    <pre>Sub FootnotesToHyperlinks()
    ' Requires ConvertStringToValidBookmarkName
    ' Requires StripInvalidBookmarkCharacters
    Dim f As Footnote
    Dim sRef As String
    Dim sFnoteText As String
    Dim sBookmarkName As String
    Dim rng As Range
    Dim rngReference As Range
    Dim i As Integer
    Dim doc As Document

    Set doc = ActiveDocument

    For Each f In doc.Footnotes
    Set rng = doc.Range
    sRef = f.Index
    sFnoteText = f.Range.Text
    sRef = "[" & sRef & "]"
    sBookmarkName = ConvertStringToValidBookmarkName(sFnoteText)
    With rng
    .Collapse wdCollapseEnd
    .InsertAfter vbCr
    .Collapse wdCollapseEnd
    .InsertAfter sRef & sFnoteText
    End With
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rng
    Set rngReference = f.Reference.Duplicate
    rngReference.Collapse wdCollapseEnd
    doc.Hyperlinks.Add Anchor:=rngReference, _
    SubAddress:=doc.Bookmarks(sBookmarkName), _
    TextToDisplay:=sRef, _
    ScreenTip:=sFnoteText
    ' Add hyperlink back to text
    sBookmarkName = ConvertStringToValidBookmarkName(rngReference.Text )
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rngReference
    rng.Collapse wdCollapseStart
    rng.MoveEndWhile cset:="[01234546789]"
    doc.Hyperlinks.Add Anchor:=rng, _
    SubAddress:=doc.Bookmarks(sBookmarkName)
    Next f
    For i = doc.Footnotes.Count To 1 Step -1
    doc.Footnotes(i).Delete
    Next i
    End Sub
    </pre>


  9. #9
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    That works! Thanks again Andrew.

    Alas, there is one problem: the macro reformats the bottom footnotes, so that

    David Mayer,

  10. #10
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    Again, replace the first of the three with the following.

    <pre>Sub FootnotesToHyperlinks()
    ' Requires ConvertStringToValidBookmarkName
    ' Requires StripInvalidBookmarkCharacters
    Dim f As Footnote
    Dim sRef As String
    Dim sFnoteText As String
    Dim sBookmarkName As String
    Dim rng As Range
    Dim rngReference As Range
    Dim i As Integer
    Dim doc As Document

    Set doc = ActiveDocument

    For Each f In doc.Footnotes
    Set rng = doc.Range
    sRef = f.Index
    sFnoteText = f.Range.Text
    sRef = "[" & sRef & "]"
    sBookmarkName = ConvertStringToValidBookmarkName(sFnoteText)
    With rng
    .Collapse wdCollapseEnd
    .InsertAfter vbCr
    .Collapse wdCollapseEnd
    ' Retain the footnote formatting
    .FormattedText = f.Range.FormattedText
    .InsertBefore sRef & " "
    End With
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rng
    Set rngReference = f.Reference.Duplicate
    rngReference.Collapse wdCollapseEnd
    doc.Hyperlinks.Add Anchor:=rngReference, _
    SubAddress:=doc.Bookmarks(sBookmarkName), _
    TextToDisplay:=sRef, _
    ScreenTip:=sFnoteText
    ' Add hyperlink back to text
    sBookmarkName = ConvertStringToValidBookmarkName(rngReference.Text )
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rngReference
    rng.Collapse wdCollapseStart
    rng.MoveEndWhile cset:="[01234546789]"
    doc.Hyperlinks.Add Anchor:=rng, _
    SubAddress:=doc.Bookmarks(sBookmarkName)
    Next f
    For i = doc.Footnotes.Count To 1 Step -1
    doc.Footnotes(i).Delete
    Next i
    End Sub
    </pre>


  11. #11
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    A thousand thank yous!

  12. #12
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    Hi Jonathan,

    The references are two-way on the system I'm on (Word 2000), both in Word and in HTML generated from the Word files. I'll see if I can spot anything odd in the HTML of your page, but the macro works just fine for me.

  13. #13
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    <P ID="edit" class=small>(Edited by jscher2000 on 19-Jul-04 14:56. Indenting preserved by using [pre] and [/pre] around the code.)</P>Hi Andrew,

    Unfortunately, it seems I jumped the gun on my conclusion; the macro is not fully circular.

    As you can see from http://students.hamilton.edu/2005/jrick/hatespeech.htm, although clicking on the bottom footnotes jumps to their counterparts within the body of the text, clicking on the footnotes within the body does not jump to their counterparts at the bottom.

    Here's the entire macro as I have it (since the tabs aren't coming through in the Lounge, you can see the formatted version at http://students.hamilton.edu/2005/jrick/textnotes.htm.

    <pre>Sub Textnotes()
    ' By Andrew Savikas (http://www.wopr.com/cgi-bin/w3t/show...&Number=388561)
    ' Requires ConvertStringToValidBookmarkName
    ' Requires StripInvalidBookmarkCharacters
    Dim f As Footnote
    Dim sRef As String
    Dim sFnoteText As String
    Dim sBookmarkName As String
    Dim rng As Range
    Dim rngReference As Range
    Dim i As Integer
    Dim doc As Document
    Set doc = ActiveDocument
    For Each f In doc.Footnotes
    Set rng = doc.Range
    sRef = f.Index
    sFnoteText = f.Range.Text
    sRef = "[" & sRef & "]"
    sBookmarkName = ConvertStringToValidBookmarkName(sFnoteText)
    With rng
    .Collapse wdCollapseEnd
    .InsertAfter vbCr
    .Collapse wdCollapseEnd
    ' Retain the footnote formatting
    .FormattedText = f.Range.FormattedText
    .InsertBefore sRef & " "
    End With
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rng
    Set rngReference = f.Reference.Duplicate
    rngReference.Collapse wdCollapseEnd
    doc.Hyperlinks.Add Anchor:=rngReference, _
    SubAddress:=doc.Bookmarks(sBookmarkName), _
    TextToDisplay:=sRef, _
    ScreenTip:=sFnoteText
    ' Add hyperlink back to text
    sBookmarkName = ConvertStringToValidBookmarkName(rngReference.Text )
    doc.Bookmarks.Add Name:=sBookmarkName, Range:=rngReference
    rng.Collapse wdCollapseStart
    rng.MoveEndWhile cset:="[01234546789]"
    doc.Hyperlinks.Add Anchor:=rng, _
    SubAddress:=doc.Bookmarks(sBookmarkName)
    Next f
    For i = doc.Footnotes.Count To 1 Step -1
    doc.Footnotes(i).Delete
    Next i
    End Sub
    '
    Function ConvertStringToValidBookmarkName(ByVal str As String) As String
    str = StripInvalidBookmarkCharacters(str)
    str = Replace(str, " ", "_")
    str = "_" & str
    str = Int(9000 * Rnd + 100) & str
    str = "_" & str
    ConvertStringToValidBookmarkName = str
    End Function
    '
    Function StripInvalidBookmarkCharacters(ByVal str As String) As String
    Dim i As Integer
    For i = 33 To 255
    Select Case i
    Case 33 To 47, 58 To 64, 91 To 96, 123 To 255
    str = Replace(str, Chr$(i), vbNullString)
    End Select
    Next i
    StripInvalidBookmarkCharacters = str
    End Function</pre>

    Any ideas what's wrong? Thanks again.

  14. #14
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Modify a macro (2002 SP3)

    Footnote 7 is two-way in your file.

    Can't tell you why, but it looks like Word has left out the <a name= ""> element in the footnotes themselves in your document. Again, it works fine on my system (and makes the references two-way in Word, which is what you'd asked for, after all).

    I generally stay very far away from HTML generated by Word, so can't offer much insight.

  15. #15
    5 Star Lounger
    Join Date
    Jun 2001
    Location
    USA
    Posts
    708
    Thanks
    48
    Thanked 1 Time in 1 Post

    Re: Modify a macro (2002 SP3)

    <P ID="edit" class=small>(Edited by Jon_Rick on 28-Sep-04 18:53. corrected url)</P>Hi,

    To test the problem, I created

    http://students.hamilton.edu/2005/jrick/test.htm

    which works fine.

    But when I paste the text from the above into

    http://students.hamilton.edu/2005/jrick/hatespeech.htm

    something odd ensues: the footnotes within the text stop working, though the ones at the bottom still work.

    Any ideas why? Many thanks!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •