Automating a Manual Process with a Macro

by Allen Wyatt
(last updated August 27, 2018)

1

Jason is a student editor of an academic law journal. As part of the publication process, he needs to check every sentence for plagiarism and accuracy and every citation to ensure proper formatting and support. This basically means every sentence must be supported by a citation placed in a footnote. Currently the staff manually copies and pastes every text sentence, footnote, and footnote number from the author's manuscript (a Word document) into a staff editor's Word document that is then used for checking. Jason wonders if there is a way to automate this process of copying over the "footnote number," "sentence from the article document" and "original footnote content" from the manuscript source document into the worksheet document.

Each of the tasks mentioned by Jason can be done programmatically, with some irritating exceptions. It is not that difficult to step through the footnote and endnote collections, in VBA, and extract information from them. This information can then be moved to a new document that could be used as the editor's worksheet. The irritating part is that the footnote and endnote numbers are dynamic, and therefore not that easy to access. A full discussion of this irritant can be found at this site:

http://www.vbaexpress.com/forum/showthread.php?31231

Exactly how you might go about making a macro to do the information transfer from one document to another depends, in large part, on the characteristics of the information in the author's document. For instance, does the author's document include one or two spaces after a sentence? Does it allow multiple footnotes per sentence? Does it allow endnotes in addition to footnotes? Does it include tables?

The point is that ther are any number of considerations that can affect the development of a macro. This means that any macros will need to be finely tuned to the source document you are working with—which means lots of testing. To give you a starting point, however, consider the following macros. They will copy sentences, footnotes, and endnotes (if any) from a source document to a new document.

Sub FootnotesEndnotes()
    Dim fNote As Footnote
    Dim eNote As Endnote
    Dim aRange As Range
    Dim sText As String
    Dim rText As String
    Dim eRef As String
    Dim newDoc As Document
    Dim oldDoc As Document

    Set oldDoc = ActiveDocument
    Set newDoc = Documents.Add

    sText = "---------------FOOTNOTES---------------" & vbCr
    newDoc.Content.InsertAfter sText

    oldDoc.Activate
    For Each fNote In ActiveDocument.Footnotes
        Set aRange = fNote.Reference
        aRange.MoveStart unit:=wdSentence, Count:=-1
        aRange.MoveEnd unit:=wdSentence
        sText = aRange.Text
        rText = fNote.Range.Text
        With fNote.Reference.Characters.First
            .Collapse
            .InsertCrossReference wdRefTypeFootnote, _
              wdFootnoteNumberFormatted, fNote.Index
            eRef = .Characters.First.Fields(1).Result
            Selection.Start = fNote.Reference.Start - Len(eRef)
            Selection.End = fNote.Reference.Start
            Selection.Delete
        End With
        Call WriteNewdoc(newDoc, sText, rText, eRef, "Footnote Text")
    Next fNote

    sText = "---------------ENDNOTES----------------" & vbCr
    newDoc.Content.InsertAfter vbCr & vbCr & sText

    For Each eNote In ActiveDocument.Endnotes
        Set aRange = eNote.Reference
        aRange.MoveStart unit:=wdSentence, Count:=-1
        aRange.MoveEnd unit:=wdSentence
        sText = aRange.Text
        rText = eNote.Range.Text
        With eNote.Reference.Characters.First
            .Collapse
            .InsertCrossReference wdRefTypeEndnote, _
              wdEndnoteNumberFormatted, eNote.Index
            eRef = .Characters.First.Fields(1).Result
            Selection.Start = eNote.Reference.Start - Len(eRef)
            Selection.End = eNote.Reference.Start
            Selection.Delete
        End With
        Call WriteNewdoc(newDoc, sText, rText, eRef, "Endnote Text")
    Next eNote

    newDoc.Activate
End Sub

Sub WriteNewdoc(newDoc As Document, sText As String, rText As String, _
       eRef As String, aStyle As String)
    Dim sText1 As String
    Dim sText2 As String
    Dim dRange As Range
    Dim k As Long
    Dim curDoc As Document

    Set curDoc = ActiveDocument
    newDoc.Activate
    k = InStr(sText, Chr(2))
    If k = 1 Then sText = Mid(sText, 2) 'in case previous sentence has note
    sText = Trim(sText)
    k = InStr(sText, Chr(2))
    If k = 0 Then
        sText = sText & Chr(2)
        k = Len(sText)
    End If
    If k > 1 Then
        sText1 = Left(sText, k - 1)
    Else
        sText1 = ""
    End If
    If k = Len(sText) Then
        sText2 = ""
    Else
        sText2 = Mid(sText, k + 1)
    End If
    If Len(sText2) > 0 Then
        If Mid(sText2, Len(sText2), 1) = Chr(13) Then
            sText2 = Left(sText2, Len(sText2) - 1)
        End If
    End If
    Set dRange = newDoc.Content
    dRange.Collapse Direction:=wdCollapseEnd
    dRange.Select
    With Selection
        .InsertAfter vbCr & sText1
        .Font.Superscript = False
        .Collapse Direction:=wdCollapseEnd
        .InsertAfter eRef
        .Font.Superscript = True
        .Collapse Direction:=wdCollapseEnd
        .InsertAfter " " & sText2 & vbCr
        .Font.Superscript = False
        .Collapse Direction:=wdCollapseEnd
        .InsertAfter eRef
        .Font.Superscript = True
        .Collapse Direction:=wdCollapseEnd
        .InsertAfter " " & rText & vbCr & vbCr
        .Font.Superscript = False
        .Style = aStyle
    End With
    curDoc.Activate
End Sub

This is (again) just a starting point. You will need to test and tweak the macros with your documents to make sure they do what you expect.

If you are looking for additional resources to aid in developing such a macro, you might try this book. It is a freebie, and it may have some macros (or examples) that you can adapt to your specific purposes:

http://www.archivepub.co.uk/book.html

Don't be surprised if your macro becomes quite complex over time. This is to be expected anytime you create a macro to perform tasks that humans can do with relatively little thinking.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12724) applies to Microsoft Word 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving Form Data for a Database

Use Word to create a form, and you can easily collect standardized data from a large number of users. When it comes time ...

Discover More

Deleting Blank Columns

Import data from another program, and you could end up with a lot of blank columns in your data. Here's the quickest way ...

Discover More

Specifying a Location To Save Automatic Backup Files

Word allows you make two types of automatic backups of your documents. One is a copy of the file before you start making ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Using Call to Run VBA Macros

An elegant way to run macros from within macros is to use the Call statement. In order to use it, you need to provide a ...

Discover More

Determining the Number of Fonts Available

When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the ...

Discover More

Moving Through a Table in a Macro

Do you need to step through a table, cell by cell, in a macro? It's easy to do using the Move method, as described in ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 3 + 4?

2013-11-16 07:30:13

Ken Endacott

Many citations refer to a web page and as part of the checking process it is necessary to determine that the referred web page exists and is relevant. If the reference contains a hyperlink then it is simply a matter of clicking on the hyperlink.

However, documents prepared for publication must have the urls in text form and checking these requires copying and pasting the url text into a browser.

The Fields module of the free macros at www.editordie.com.au/editorkae has the ability to find text urls in a document and with one click open a browser at the appropriate page.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.