0

The macro below was posted here and inserts "Page #." preceding each endnote:

Sub InsertPageNumberForEndnotes()
Dim endNoteCount As Integer
Dim curPageNumber As Integer
If ActiveDocument.Endnotes.Count > 0 Then
  For endNoteCount = 1 To ActiveDocument.Endnotes.Count
    Selection.GoTo What:=wdGoToEndnote, Which:=wdGoToAbsolute, _
      Count:=endNoteCount
    curPageNumber = Selection.Information(wdActiveEndPageNumber)
    ActiveDocument.Endnotes(endNoteCount).Range.Select
    ActiveDocument.Application.Selection.Collapse _
      (WdCollapseDirection.wdCollapseStart)
    ActiveDocument.Application.Selection.Paragraphs(1).Range.Characters(3)._
      InsertBefore "Page " & CStr(curPageNumber) & ". "        
  Next
End If    
End Sub

So for text with superscripts

Yak yak yak yak yak yak.^1 
       :
       :
Yuk yuk yuk yuk yuk yuk yuk.^2

The macro converts endnotes from

^1 Blah blah blah
^2 Blah blah blah 

Into

^1 Page 22. Blah blah blah
^2 Page 119. Blah blah blah

I'd now like add the sentence being referenced. So

^1 Page 22. Yak yak yak yak yak yak. Blah blah blah
^2 Page 119. Yuk yuk yuk yuk yuk yuk yuk. Blah blah blah

I see this done in several non-fiction books. Is it possible in a macro?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
buttonsrtoys
  • 2,359
  • 3
  • 32
  • 52
  • How do you know it is a "sentence" being referenced? A "sentence" could be a really large amount of text. Are you sure you wouldn't be better off asking if it is possible to convert an EndNote to a FootNote? – Variatus Oct 05 '17 at 08:27
  • @Variatus, for my purposes, a large amount of text would be fine. What I'd like is to insert into the endnote all the text from the location of the superscript in the document to the preceding ending punctuation (e.g., period, question mark, exclamation) or paragraph start, whichever comes first. Or maybe VBA has the ability to parse by sentences? – buttonsrtoys Oct 05 '17 at 10:30

1 Answers1

1

Here you go.

Sub AddSourceToEndNote()
    ' 04 Oct 2017

    Dim Note As EndNote
    Dim Txt As String, Tmp As String

    With ActiveDocument
        If .EndNotes.Count Then
            For Each Note In .EndNotes
                With Note
                    With .Reference
                        Txt = "Page" & Format(.Information(wdActiveEndPageNumber), " 0. ")
                        Tmp = .Sentences(1).Text
                        Do While Asc(Right(Tmp, 1)) < 31
                            Tmp = Left(Tmp, Len(Tmp) - 1)
                            If Len(Tmp) < 1 Then Exit Do
                        Loop
                        If InStr(".:?!", Right(Tmp, 1)) = 0 Then Tmp = Tmp & "."
                        Txt = Txt & Tmp
                    End With
                    With .Range
                        .Text = Txt & " " & .Text
                    End With
                End With
            Next Note
        End If
    End With
End Sub
Variatus
  • 14,293
  • 2
  • 14
  • 30
  • Awesome @Variatus Thx! Two caveats: the superscript has to be inside the ending punctuation (...blah blah^3. not ...blah blah.^3) or else Tmp is empty, but that's fine b/c I'll be hiding the superscript, so can move them inside the sentence. Also, the string in Tmp has a special character where the superscript was that looks like a box in Word, but that was simple to remove with a function like here: https://stackoverflow.com/questions/15723672/how-to-remove-all-non-alphanumeric-characters-from-a-string-except-period-and-sp – buttonsrtoys Oct 05 '17 at 15:06
  • I don't understand your caveats. (1) I don't understand which superscript you mean - in the text or in the note - nor why it should be in the sentence, but I do seem to understand that you have it under control. (2) My code does remove the box. It consists of 2 characters (I think it is Chr(2) and Chr(27)). I wasn't sure if there might be a CR or LF as well in some cases. That's why my code is quite radical with the removals. Did it fail to remove the little square in your trials? – Variatus Oct 06 '17 at 01:56
  • Maybe what I'm seeing is specific to Word 2016? (1) The superscript in the text that's inserted when users select References > Insert Endnote. If it's located after the sentence ending punctuation, then Tmp is empty so nothing is inserted into the endnote. Moving the superscript inside the ending punctuation was my workaround. (2) I see now in your code that you're lopping off the last character to get rid of the box, which is great, but if I move the superscript inside, the last char is the punctuation, which is why I was still getting the box. – buttonsrtoys Oct 07 '17 at 11:01
  • I still can't follow. Your approach, based on the `Selection` object, is fundamentally different from mine which is based on the `EndNote` object. I never come across the superscripts. `EndNote.Range` defines the range following the superscripted ID at the bottom of the page. `EndNote.Reference` defines the range before the superscripted ID in the document body. I don't know what the little box represents. In my tests it is a Chr(2) (ASCII for "Start of Text") followed by a Chr(13) (ASCII for Carriage Return). These I am "looping off". – Variatus Oct 07 '17 at 11:58
  • I'm not lopping off any punctuation marks because they all have ASCII codes higher than 31. Could be 2016 is different. I'm using 2010. But could you try the code on an EndNote which you haven't modified in any way? It looks like your modifications have changed the basic structure of the EndNote as Microsoft designed it. – Variatus Oct 07 '17 at 12:03
  • The little box must be connected to the superscripted ID. Since it is no longer at the end you could take it out with `Replace(Txt, Chr(2) , "")` but I fear that will open a new Pandora's box. – Variatus Oct 07 '17 at 12:07
  • I think I'm confusing the discussion. I'm using your approach, but it only works for me if I insert the endnote before the ending punctuation. If after the punctuation, then "Tmp = .Sentences(1).Text" assigns Tmp as "" and only "Page #" is inserted into the endnote. If inserted before the punctuation I get a box at the insertion point which is why I added the filter. – buttonsrtoys Oct 07 '17 at 19:19