none
Replacing endnotes and cross-references with text RRS feed

  • Question

  • Hi, hope you can help.

    The following code worked great in Word 2003, taking my endnotes and cross-references and replacing them with text. However, using the same macro in Word 2010, although it appears to have completed the task, the cross-references although appearing to have been converted, remain as "field results" and therefore when I copy and paste, or click the result and press F9, get something similar to the following: {REF _Ref249586 \* MERGEFORMAT}

    Can you please help?

    Public Sub ConvertFootnotes()

      'This procedure converts all EndNotes in a document to normal text.

      'Word stores the EndNotes as EndNote objects in the EndNotes collection.

      'We want to keep the information as is, but as text in the main document

      'instead of EndNote objects.

     

      'We will do this by replacing the EndNote reference in the main document

      '(usually a superscripted number) with a hardcoded text representation of

      'the number. Formatting is removed and the number places in brackets.

      'The text of the EndNotes that Word normally places at the end of the

      'document or section will be entered as plain text at the end of the

      'document. The EndNote objects can then be deleted.

     

      'NOTE: When there are multiple references to one EndNote, only one

      'occurrance in the main document is stored as an EndNote reference.

      'All the other references are stored as Bookmarks. After processing

      'the endnotes, we may be left with some Bookmarks which we'll have

      'to process seperately.

       

        'switch to main document

        If ActiveWindow.ActivePane.View.Type = wdPrintView Or ActiveWindow.ActivePane.View.Type = wdWebView Or ActiveWindow.ActivePane.View.Type = wdPrintPreview Then

            ActiveWindow.View.SeekView = wdSeekMainDocument

        Else

            ActiveWindow.Panes(2).Close

        End If

     

      Dim Note As Endnote

      Dim NoteReference As String

      Dim NoteText As String

       

      'Loop through all the EndNotes and

      '1. enter the reference number in brackets in the main document, e.g. "(2)"

      '2. enter the EndNote text at the bottom of the document.

      For Each Note In ActiveDocument.Endnotes

        With Note

          NoteText = .Range.Text

          NoteReference = .Index

          Call Selection.SetRange(.Reference.End, .Reference.End)

          Selection.Font.Superscript = True

          Selection.TypeText (NoteReference)

          Selection.Font.Superscript = False

          Call Selection.EndKey(wdStory)

          Call Selection.TypeText(NoteReference & ". " & NoteText) '"Type" the EndNote text at the end of the document.

          Selection.TypeParagraph

        End With

      Next Note

       

      'Delete all the endnotes.

      'The reason for doing 2 seperate loops is because Word will automatically

      'renumber the EndNotes as we delete them, so if we did the delete in the loop above,

      'each EndNote would have the same reference number, i.e. 1.

      Do While ActiveDocument.Endnotes.Count > 0

        Call ActiveDocument.Endnotes(1).Delete

      Loop

     

      'Now the document should have no EndNotes objects, only the EndNote text that

      'we "typed" at the bottom of the document in the first loop.

      'Also, all the EndNote references in the main document would have been removed,

      'leaving only the bracketed numbers we "typed" there.

     

      'What we are left with are Bookmarks that now refer to the non-existing EndNotes.

      'Interestingly enough, the removal of the EndNotes does not affect the Bookmarks.

      'Here we can simply search for superscripted text, remove the formatting and

      'place brackets around it.

     

      'Looking for...

      Selection.Find.ClearFormatting

      Selection.Find.Font.Superscript = True

      'Replacing with...

      Selection.Find.Replacement.ClearFormatting

      Selection.Find.Replacement.Font.Superscript = False

      'Some more Find/Replace settings

      With Selection.Find

        '.IgnoreSpace = True 'I cannot get this to work here, but it works from the Find/Replace dialog. Possibly not available in Word 2000.

        .Text = ""

        .Replacement.Text = " (^&)" 'The ^& here refers to the "found text", so if we found "abc" we will replace it with "(abc)"

        .Forward = True

        .Wrap = wdFindContinue

        .Format = True

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

      End With

      'Do it!

      Selection.Find.Execute Replace:=wdReplaceAll

       

      'For neatness, place the cursor at the top of the document

      Selection.HomeKey Unit:=wdStory

     

      'Issues:

      'ALL superscripted text will be affected, including white space and text that does not reference EndNotes.

      'The correct solution would be to loop through the Bookmarks collection instead of doing a find/replace.

     

    End Sub

     

    Monday, June 6, 2011 2:23 PM

All replies

  • There must be some other issue because I ran your code as a VBA subroutine against a document with end notes and it did what your comments explained it would do.

    No fields remained in the document.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, June 6, 2011 3:34 PM
  • Hi Harris,

     

    Thanks for your post.

     

    What’s your Word version, 32-bit or 64-bit?

    You might want to have a look at the documents below:

    Compatibility Between the 32-bit and 64-bit Versions of Office 2010

    Microsoft Office Code Compatibility Inspector user's guide

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 8, 2011 8:10 AM
    Moderator
  • hi Calvin,

     

    Thanks for your response. I have been away and so could not get back to this vital issue (for me vital!)

    I am using the 32 bit version of Word. I have run the Compatibility inspector but unfortunately do not know how to address the issues it raised. Are you able to assist please?

    Here is the result:

    Public Sub ConvertFootnotes()
     'This procedure converts all EndNotes in a document to normal text.
     'Word stores the EndNotes as EndNote objects in the EndNotes collection.
     'We want to keep the information as is, but as text in the main document
     'instead of EndNote objects.
     
     'We will do this by replacing the EndNote reference in the main document
     '(usually a superscripted number) with a hardcoded text representation of
     'the number. Formatting is removed and the number places in brackets.
     'The text of the EndNotes that Word normally places at the end of the
     'document or section will be entered as plain text at the end of the
     'document. The EndNote objects can then be deleted.
     
     'NOTE: When there are multiple references to one EndNote, only one
     'occurrance in the main document is stored as an EndNote reference.
     'All the other references are stored as Bookmarks. After processing
     'the endnotes, we may be left with some Bookmarks which we'll have
     'to process seperately.
      
      'switch to main document
      If ActiveWindow.ActivePane.View.Type = wdPrintView Or ActiveWindow.ActivePane.View.Type = wdWebView Or ActiveWindow.ActivePane.View.Type = wdPrintPreview Then
        ActiveWindow.View.SeekView = wdSeekMainDocument
      Else
        ActiveWindow.Panes(2).Close
      End If
    
     Dim Note As Endnote
     Dim NoteReference As String
     Dim NoteText As String
      
     'Loop through all the EndNotes and
     '1. enter the reference number in brackets in the main document, e.g. "(2)"
     '2. enter the EndNote text at the bottom of the document.
     For Each Note In ActiveDocument.Endnotes
      With Note
       '
       ' 
       '   Potentially contains removed items the object model
       '   [mso]Balloon.Text
       '   http://go.microsoft.com/?linkid=171788 /URL>
       ' 
       '
       NoteText = .Range.Text
       NoteReference = .Index
       Call Selection.SetRange(.Reference.End, .Reference.End)
       Selection.Font.Superscript = True
       Selection.TypeText (NoteReference)
       Selection.Font.Superscript = False
       Call Selection.EndKey(wdStory)
       Call Selection.TypeText(NoteReference & ". " & NoteText) '"Type" the EndNote text at the end of the document.
       Selection.TypeParagraph
      End With
     Next Note
      
     'Delete all the endnotes.
     'The reason for doing 2 seperate loops is because Word will automatically
     'renumber the EndNotes as we delete them, so if we did the delete in the loop above,
     'each EndNote would have the same reference number, i.e. 1.
     Do While ActiveDocument.Endnotes.Count > 0
      Call ActiveDocument.Endnotes(1).Delete
     Loop
     
     'Now the document should have no EndNotes objects, only the EndNote text that
     'we "typed" at the bottom of the document in the first loop.
     'Also, all the EndNote references in the main document would have been removed,
     'leaving only the bracketed numbers we "typed" there.
     
     'What we are left with are Bookmarks that now refer to the non-existing EndNotes.
     'Interestingly enough, the removal of the EndNotes does not affect the Bookmarks.
     'Here we can simply search for superscripted text, remove the formatting and
     'place brackets around it.
     
     'Looking for...
     Selection.Find.ClearFormatting
     Selection.Find.Font.Superscript = True
     'Replacing with...
     Selection.Find.Replacement.ClearFormatting
     Selection.Find.Replacement.Font.Superscript = False
     'Some more Find/Replace settings
     With Selection.Find
      '.IgnoreSpace = True 'I cannot get this to work here, but it works from the Find/Replace dialog. Possibly not available in Word 2000.
      .Text = ""
      .Replacement.Text = " (^&)" 'The ^& here refers to the "found text", so if we found "abc" we will replace it with "(abc)"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
     End With
     'Do it!
     '
     ' 
     '   Potentially contains changed items in the object model
     '   [wrd]Find.Execute
     '   http://go.microsoft.com/?linkid=9718424 
     ' 
     '
     Selection.Find.Execute Replace:=wdReplaceAll
      
     'For neatness, place the cursor at the top of the document
     Selection.HomeKey Unit:=wdStory
     
     'Issues:
     'ALL superscripted text will be affected, including white space and text that does not reference EndNotes.
     'The correct solution would be to loop through the Bookmarks collection instead of doing a find/replace.
     
    End Sub
    

      

    Regards,

    Harris

     

     

     

    Tuesday, July 12, 2011 9:40 AM