Hi, I am trying to create a Word C# add-in that will find all of the endnotes
in a document, re-sort them so that the first one will be #1, and add a
hyperlink back to the section heading that the endnote reference resides in.
Most everything is working well except I can't do two things:
1) Add formatted text programmatically to the endnote text. Endnotes.Add
doesn't allow a range (just text) and when I try to update the endnote after
adding a temporary text string, the resulting endnote text is blank:
Range = aDoc.Range(ref textStart, ref textEnd);
aDoc.Endnotes[1].Range = findE;
2) Add the hyperlink back to the section heading. This is most likely due to
my issue with the formatted text, but I have tried to work around it by using
a Find and trying to find the Endnote Text, but that isn't working either.
Are we not able to find Endnote Text programmatically? I tried a VBA macro as
well and it didn't work. Here is my C# approach:
Range findEndnote = aDoc.Content;
findEndnote.Find.Forward = true;
object objTrue = true;
object objEndnoteStyle = "Endnote Text";
object objEndnoteFind = aDoc.Styles.get_Item(ref objEndnoteStyle);
findEndnote.Find.set_Style(ref objEndnoteFind);
findEndnote.Find.Text = "Section";
findEndnote.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref objTrue, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
Doug Robbins - Word MVP - 02 Mar 2008 23:16 GMT
There may be something in the following code that will help you:
' Macro created 29/09/99 by Doug Robbins to replace endnotes with textnotes
at end of document
' to replace the endnote reference in the body of the document with a
superscript number.
'
Dim aendnote As Endnote
For Each aendnote In ActiveDocument.Endnotes
ActiveDocument.Range.InsertAfter vbCr & aendnote.Index & vbTab &
aendnote.Range
aendnote.Reference.InsertBefore "a" & aendnote.Index & "a"
Next aendnote
For Each aendnote In ActiveDocument.Endnotes
aendnote.Reference.Delete
Next aendnote
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = True
End With
With Selection.Find
.Text = "(a)([0-9]{1,})(a)"
.Replacement.Text = "\2"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Hi, I am trying to create a Word C# add-in that will find all of the
> endnotes
[quoted text clipped - 29 lines]
> ref missing, ref missing, ref missing, ref objTrue, ref missing,
> ref missing, ref missing, ref missing, ref missing, ref missing);
daswifty - 03 Mar 2008 15:27 GMT
Thanks Doug, but this macro doesn't help me with much. All it appears to do
(at least on Word 2007) is convert all of the Endnotes text to regular text
and change the references to superscript.
I need something to go the other way. To convert/copy/insert regular text
into the endnote text section as formatted text.
> There may be something in the following code that will help you:
>
[quoted text clipped - 84 lines]
> > ref missing, ref missing, ref missing, ref objTrue, ref missing,
> > ref missing, ref missing, ref missing, ref missing, ref missing);
Jean-Guy Marcil - 04 Mar 2008 16:13 GMT
> Hi, I am trying to create a Word C# add-in that will find all of the endnotes
> in a document, re-sort them so that the first one will be #1, and add a
[quoted text clipped - 12 lines]
> Are we not able to find Endnote Text programmatically? I tried a VBA macro as
> well and it didn't work. Here is my C# approach:
I believe that before you can tackle #2, you have to sort out #1.
From your description of what you are attempting to do and the code snippets
you posted, there seems to be a mismatch.
"Endnotes.Add" does not add text to an existing Endnote, it actually inserts
a new Endnote.
Check out the following code:
Sub Test_1()
Dim endCustom As Endnote
Dim rgeInsertEnd As Range
Set rgeInsertEnd = ActiveDocument.Paragraphs(3).Range.Words(4)
rgeInsertEnd.Collapse wdCollapseEnd
Set endCustom = ActiveDocument.Endnotes.Add(rgeInsertEnd, , _
"First endnote inserted programmatically.")
With endCustom.Range
.Font.Name = "Arial"
.Font.Size = 14
.Italic = True
End With
End Sub
This will insert a new endnote, anchored to the fourth word of the third
paragraph in the document. Once it is inserted, you can manipulate this text
as much as you want with the range property. But I have the feeling that you
do not want to create new endnotes.
If, however, you need to manipulate existing Endnotes, you need to use the
Endnotes collection, for example:
Sub Test_2()
Dim endsDocument As Endnotes
Dim rgeTemp As Range
Dim i As Long
With ActiveDocument.Endnotes
If .Count > 0 Then
For i = 1 To .Count
Set rgeTemp = .Item(i).Range
'Manipulate the endnote text
With rgeTemp
.InsertAfter " New Text " & i & "."
With .Font
.Bold = False
.Italic = True
.Size = "8"
End With
End With
'Manipulate the endnote reference in the main text
Set rgeTemp = .Item(i).Reference
rgeTemp.Font.Bold = True
Next
Else
MsgBox "There are no endnotes in this document.", _
vbExclamation, "Cancelled"
End If
End With
End Sub
Also, I am mystified as to why you want to sort the endnotes... Aren't they
already sorted? Isn't that the default behaviour when inserting end notes?