none
Macro in Word stops partway through endnotes RRS feed

  • Question

  • I'm using macros in Word 2010 to modify journal article manuscripts in preparation for converting into HTML. One of the macros takes all of the italics in the manuscript and surrounds them with <em> tags. Initially, the macro wasn't processing italics in endnotes at all. I found a macro online that looks at the body text and the endnotes as distinct sections and this half works. My problem is that the macro will convert the italics in some, but not always all of the endnotes. In some manuscripts it will works on all of the endnotes, in some manuscripts it only works in about the first half of the endnotes, in some manuscripts in only works on the first few endnotes. 

    I'm still learning VBA and don't have the foggiest why this is happening. I've looked at the various documents to see if there is a variable between one footnote and another that might account for the macro stopping, but I haven't found anything. With my low level of experience, I haven't been able to determine if there's anything in the macro that would account for the problem either. I don't even know what to give you to help you help me! Any help would be greatly appreciated.

    The macro code:

    Sub html_italics()
    '
    '
    '
     Dim ThisRng As Range
        If Selection.Type = wdSelectionNormal Then GoTo SelectionOnly
    '
    'do body text
        
        Set ThisRng = ActiveDocument.StoryRanges(wdMainTextStory)
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ""
            .Replacement.Text = "<em>^&</em>"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = " </em>"
            .Replacement.Text = "</em> "
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
            With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "^p</em>"
            .Replacement.Text = "</em>^p"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ".</em>"
            .Replacement.Text = "</em>."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "<em></em>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
    '
    'do endnotes if they're there
        If existence_of_Endnotes = True Then
        Set ThisRng = ActiveDocument.StoryRanges(wdEndnotesStory)
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ""
            .Replacement.Text = "<em>^&</em>"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = " </em>"
            .Replacement.Text = "</em> "
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
            With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "^p</em>"
            .Replacement.Text = "</em>^p"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ".</em>"
            .Replacement.Text = "</em>."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        With ThisRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "<em></em>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        ThisRng.Find.Execute Replace:=wdReplaceAll
        End With
        End If
    '
    'this part does only the selected text
    SelectionOnly:
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ""
            .Replacement.Text = "<em>^&</em>"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            Selection.Find.Execute Replace:=wdReplaceAll
        End With
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = " </em>"
            .Replacement.Text = "</em> "
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Selection.Find.Execute Replace:=wdReplaceAll
        End With
            With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "^p</em>"
            .Replacement.Text = "</em>^p"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Selection.Find.Execute Replace:=wdReplaceAll
        End With
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = ".</em>"
            .Replacement.Text = "</em>."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Selection.Find.Execute Replace:=wdReplaceAll
        End With
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Font.Italic = True
            .Replacement.Font.Italic = False
            .Text = "<em></em>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Selection.Find.Execute Replace:=wdReplaceAll
        End With
        GoTo FinalCleanup
    FinalCleanup:
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
        End With
       End Sub

    Tuesday, July 19, 2016 5:52 PM

All replies

  • I haven't worked with end notes but I suspect some documents have many sections and you may not be looping through all sections.  I've had that problem reformatting footers.
    Tuesday, July 19, 2016 9:03 PM