Populating Array slow.

andyww14

New member
Joined
Oct 10, 2010
Messages
2
Programming Experience
Beginner
Hi,

I have some code that is extracting tracked changes from a document. The trouble is its very slow, taking around 60 seconds to capture each tracked change.

I originally thought that pagination was slowing down the code (it’s quite a large word document). However I have turned that off but it’s made no difference to the speed of the function.

Can anybody suggest what the bottleneck is? I think it maybe the population of the array, as if I remove this code it executes very quickly.
Any suggestions as to what I could change to improve performance appreciated.

Many thanks.

VB.NET:
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop


Public Class AddTrackChanges

    ' The purpose of this function is to extract the comments and return them within an array.
    Public Function ExportTrackChanges(ByVal newWord As Word.Document, ByVal UpdateProgressChanges As ProgressBar, ByVal lblProgressChanges As Label) As Array

        Dim nCount As Integer
        Dim oRevision As Revision
        Dim oRange As Range
        Dim strText As String
        Dim n As Long
        Dim i As Long
        nCount = newWord.Revisions.Count
        UpdateProgressChanges.Maximum = nCount
        UpdateProgressChanges.Value = 0

        'newWord.Application.Options.Pagination = False

        Dim progressChangesCounter As Integer
        progressChangesCounter = 0


        ' Define the size of the array
        Dim saChanges(nCount, 5) As String

        'Get info from each tracked change (insertion/deletion) from oDoc and insert in table
        For Each oRevision In newWord.Revisions
            newWord.UndoClear()
            Select Case oRevision.Type
                'Only include insertions and deletions
                Case WdRevisionType.wdRevisionInsert, WdRevisionType.wdRevisionDelete
                    newWord.Application.Options.Pagination = False
                    With oRevision
                        'Get the changed text
                        strText = .Range.Text

                        oRange = .Range
                        Do While InStr(1, oRange.Text, Chr(2)) > 0
                            'Find each Chr(2) in strText and replace by appropriate text
                            i = InStr(1, strText, Chr(2))
                            If oRange.Footnotes.Count = 1 Then
                                strText = Replace(Expression:=strText, Find:=Chr(2), Replacement:="[footnote reference]", Start:=1, Count:=1)
                                'To keep track of replace, adjust oRange to start after i
                                oRange.Start = oRange.Start + i
                            ElseIf oRange.Endnotes.Count = 1 Then
                                strText = Replace(Expression:=strText, Find:=Chr(2), Replacement:="[endnote reference]", Start:=1, Count:=1)
                                'To keep track of replace, adjust oRange to start after i
                                oRange.Start = oRange.Start + i
                            End If
                        Loop
                    End With
                    'Add 1 to counter
                    n = n + 1
                    'Add row to table

                    ' Add items to the array
                    saChanges(n, 0) = oRevision.Range.Information(WdInformation.wdActiveEndPageNumber)
                    saChanges(n, 1) = oRevision.Range.Information(WdInformation.wdFirstCharacterLineNumber)
                    'Type of revision
                    If oRevision.Type = WdRevisionType.wdRevisionInsert Then
                        saChanges(n, 2) = "Inserted"
                    Else
                        saChanges(n, 2) = "Deleted"
                    End If
                    saChanges(n, 3) = strText
                    saChanges(n, 4) = oRevision.Author
                    saChanges(n, 5) = oRevision.Date
                    UpdateProgressChanges.Value += 1
                    newWord.UndoClear()
                    progressChangesCounter += 1
                    lblProgressChanges.Text = progressChangesCounter
                    System.Windows.Forms.Application.DoEvents()
            End Select
        Next oRevision

        Return saChanges
        newWord.Application.Quit()


    End Function

End Class
 
Back
Top