none
Word VB script causes Word2013 to crash RRS feed

  • Question

  • <tt>Hi Script Guys,
    I have a VB macro-script that adds line numbering to footnotes in Word 2013. The same script will remove the line number from the line-numbered footnotes. This the problem I am having with the with the script.
    </tt><tt>When I run it, it take an extremely long time to run and causes Word to crash and go into a "Not Responding" state. Can you please provide feedback as to what might be causing this problem or what needs to be corrected?  Thanks for your help. The macro is listed below.</tt>

    Public Sub LineNumberFootnotes()
        Application.ScreenUpdating = False

        Selection.HomeKey unit:=wdStory
        If (Selection.PageSetup.LineNumbering.Active = True) Then
            ActiveDocument.Bookmarks("\page").Range.Select
            Selection.Collapse direction:=wdCollapseEnd
            Selection.MoveLeft unit:=wdCharacter, Count:=1
           
            Do While Selection.Range.Information(wdFirstCharacterLineNumber) <> -1
                Selection.MoveDown unit:=wdLine, Count:=1
            Loop
            Selection.HomeKey unit:=wdLine
            Selection.MoveRight unit:=wdCharacter, Count:=2, Extend:=2
            firstchar = Selection
            If (Selection.Font.Superscript = False And firstchar = "~1") Then
            Else
                Call footNoteOn
            End If
            'Selection.PageSetup.LineNumbering.Active = False
        Else
            ActiveDocument.Bookmarks("\page").Range.Select
            Selection.Collapse direction:=wdCollapseEnd
            Selection.MoveLeft unit:=wdCharacter, Count:=1
           
            Do While Selection.Range.Information(wdFirstCharacterLineNumber) <> -1
                Selection.MoveDown unit:=wdLine, Count:=1
            Loop
            Selection.HomeKey unit:=wdLine
            Selection.MoveRight unit:=wdCharacter, Count:=2, Extend:=2
            firstchar = Selection
            If (Selection.Font.Superscript = False And firstchar = "~1") Then
                Call footNoteOff
            End If
            'Selection.PageSetup.LineNumbering.Active = True
        End If
       
        Selection.HomeKey unit:=wdStory
       
        Application.ScreenUpdating = True
    End Sub
    Private Sub footNoteOn()
        'find end of document
        'go to the last line
        Selection.EndKey unit:=wdStory
        'get last line
        intLastLine = Selection.Range.Information(wdFirstCharacterLineNumber)
        'get last page
        intLastPage = Selection.Range.Information(wdActiveEndPageNumber)
       
        'start of document
        Selection.HomeKey unit:=wdStory
       
        loopstopper = False
        j = 1
        notecount = 1
        Do While loopstopper = False
            'what page are we on?
            pagenum = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
           
            If (Selection.Range.Information(wdFirstCharacterLineNumber) <> -1) Then
                ActiveDocument.Bookmarks("\page").Range.Select
                Selection.Collapse direction:=wdCollapseEnd
                Selection.MoveLeft unit:=wdCharacter, Count:=1
            End If
           
            Do While Selection.Range.Information(wdInFootnote) = False
                Selection.MoveDown unit:=wdLine, Count:=1
            Loop
            Selection.HomeKey unit:=wdLine
            i = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

            Count = 0
            Do While i = pagenum And j > 0
                'get text of footnote that matches the count
                Dim aFN As footnote
                For Each aFN In ActiveDocument.Footnotes
                    If (aFN.Index = notecount) Then
                        noteindex = aFN.Index
                        notetxt = Trim(aFN.Range.Text)
                    End If
                Next aFN
               
                'find text of line you are on
                Selection.MoveRight unit:=wdCharacter, Count:=2
                Selection.EndKey unit:=wdLine, Extend:=wdExtend
                'just cleaning up some stuff
                linetxt = Selection
                linetxt = Left(linetxt, Len(linetxt) - 1)
                linetxt = Trim(linetxt)
                linetxt = Replace(linetxt, vbNewLine, "")
                linetxt = Application.CleanString(linetxt)
               
                Selection.HomeKey unit:=wdLine
               
                If (InStr(1, notetxt, linetxt, vbTextCompare) > 0) Then
                    Count = Count + 1
                Else
                    Count = 1
                    notecount = notecount + 1
                End If
               
           
                Selection.TypeText Text:="~" & Count & "~"
                Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
                Selection.Font.ColorIndex = wdWhite
                Selection.MoveLeft unit:=wdCharacter, Count:=2
                Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
                Selection.Font.ColorIndex = wdWhite
                Selection.MoveRight unit:=wdCharacter, Count:=3
                j = Selection.MoveDown(wdLine, 1)
                Selection.HomeKey unit:=wdLine
                i = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
            Loop
           
            If (j = 0) Then
                loopstopper = True
            End If
        Loop
    End Sub
    Private Sub footNoteOff()
        'find end of document
        'go to the last line
        Selection.EndKey unit:=wdStory
        'get last line
        intLastLine = Selection.Range.Information(wdFirstCharacterLineNumber)
        'get last page
        intLastPage = Selection.Range.Information(wdActiveEndPageNumber)
       
        'start of document
        Selection.HomeKey unit:=wdStory
       
        loopstopper = False
        j = 1
        Do While loopstopper = False
            Count = 1
            'what page are we on?
            pagenum = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
            i = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
            ActiveDocument.Bookmarks("\page").Range.Select
            Selection.Collapse direction:=wdCollapseEnd
            Selection.MoveUp unit:=wdLine, Count:=1
           
            Selection.HomeKey unit:=wdLine
           
            'special code for last page
            If (intLastPage = Selection.Range.Information(wdActiveEndPageNumber)) Then
                Do While j <> 0
                    j = Selection.MoveDown(wdLine, 1)
                Loop
                loopstopper = True
            End If
           
            Do While i = pagenum And Selection.Range.Information(wdInFootnote) = True
                Selection.Delete unit:=wdCharacter, Count:=3
                Selection.MoveUp unit:=wdLine, Count:=1
                Selection.HomeKey unit:=wdLine
                i = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
            Loop
            Selection.EndKey unit:=wdLine
            Selection.MoveRight unit:=wdCharacter, Count:=1
        Loop
    End Sub

    <tt> 
    </tt>

    • Moved by Bill_Stewart Thursday, January 22, 2015 4:23 PM Move to more appropriate forum
    Friday, December 5, 2014 4:31 AM

All replies

  • Sorry - we cannot debug youur script for you.  It is likely caused by a bad design with an infinite loop caused by an indeterminant loop control variable.

    If you can creaate a script that demnstrates your issue that is only a few lines we might be able to help.

    Performance of external automation with Office components is very slow.  For any highly iterative process you sshould use VBA which can e triggered by an external script.

    Post in the Word VBA forum for instructions on how to use VBA to automate maodification of external documents.  I think you will find it is much faster, easier and more powerful than VBScript and it has a great deebugger to help you.


    ¯\_(ツ)_/¯

    Friday, December 5, 2014 9:54 AM