L
liu_jz
I wrote a program in VBA. It took about 4s. I wrote this program in VB.
It took 60s. Is VB slower than VBA? Why? How to improve the performance
in VB?
The source code as follows:
Dim gwdApp As Word.Application
Dim gwdDoc As Word.Document
Dim wdRange() As Word.Range
Dim wdField As Word.Field
Dim i As Long
Dim j As Integer
Dim lngRecordCnt As Long
Dim lngFieldCnt As Long
Dim lngRangeStart As Long
Dim lngStep As Long
Dim strFieldName() As String
Dim vntFieldValue() As Variant
.......
For i = 0 To lngRecordCnt - 1
......
wdRange.Copy
Selection.SetRange lngRangeStart, lngRangeStart
'VB gwdApp.Selection.SetRange lngRangeStart, lngRangeStart
Selection.Paste
'VB gwdApp.Selection.Paste
gwdDoc.Bookmarks(strBookmarkName).Delete
Call gwdDoc.Bookmarks.Add(strBookmarkName, gwdDoc.Range(lngRangeStart,
lngStep + lngRangeStart))
For j = 0 To lngFieldCnt - 1
If blnGetFieldByName(gwdDoc.Bookmarks(strBookmarkName).Range,
strFieldName(j), wdField) Then
wdField.Result.Text = vntFieldValue(j)
wdField.Unlink
End If
Next j
......
Next i
It took 60s. Is VB slower than VBA? Why? How to improve the performance
in VB?
The source code as follows:
Dim gwdApp As Word.Application
Dim gwdDoc As Word.Document
Dim wdRange() As Word.Range
Dim wdField As Word.Field
Dim i As Long
Dim j As Integer
Dim lngRecordCnt As Long
Dim lngFieldCnt As Long
Dim lngRangeStart As Long
Dim lngStep As Long
Dim strFieldName() As String
Dim vntFieldValue() As Variant
.......
For i = 0 To lngRecordCnt - 1
......
wdRange.Copy
Selection.SetRange lngRangeStart, lngRangeStart
'VB gwdApp.Selection.SetRange lngRangeStart, lngRangeStart
Selection.Paste
'VB gwdApp.Selection.Paste
gwdDoc.Bookmarks(strBookmarkName).Delete
Call gwdDoc.Bookmarks.Add(strBookmarkName, gwdDoc.Range(lngRangeStart,
lngStep + lngRangeStart))
For j = 0 To lngFieldCnt - 1
If blnGetFieldByName(gwdDoc.Bookmarks(strBookmarkName).Range,
strFieldName(j), wdField) Then
wdField.Result.Text = vntFieldValue(j)
wdField.Unlink
End If
Next j
......
Next i