Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB + Word Performance Problems

Status
Not open for further replies.

Lemox

Programmer
Dec 13, 2003
67
GB
Hi,

I produce reports from vb 6 into MS Word 2k.

I wrote a loop that inserts the data in the word document :
The more it loops, the more its getting slow.

Initally, this loop is executed twice / sec... and in the end (looped about 200 times), it takes 11 seconds to do it once !!!

In this loop, I'm sure that it's always the same statements that are executed.

If i have a look at my MS Task Manager while my loop is being executed, I can see that CPU usage is always at 100%.

Any idea ?

Could it be linked to the growing size of the word document ?
Or the 100% CPU usage ?

If someone feels like he/she can help but needs further information, I can post my code.

Thanx
Lemox
 
Can you post your code? I'm sure it has to do with the looping functionality. You may be unintentionally nesting loops, or something similar.

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Ok : here's the loop (simplified but always 'Ifs' like following):

--------
With oWord 'Dim oWord As clsWordDocument

While Not Me.EOF 'Me = UserDefined Recordset
If .InsertBookmark("PersonLastName") Then
.ReplaceField "fldLastName", Me.LastName
End If
.DeleteBookmarkIfStillPresent "PersonLastName"

If .InsertBookmark("PersonFirstName") Then
.ReplaceField "fldFirstName", Me.FirstName
End If
.DeleteBookmarkIfStillPresent "PersonFirstName"

If .InsertBookmark("PersonCompany") Then
.ReplaceField "fldCompany", Me.Firm, False
End If
.DeleteBookmarkIfStillPresent "PersonCompany"
Me.MoveNext
Wend
End With

---------------------------
---------------------------
' class clsWordDocument

---------------
'Insert Bookmark from Template into Doc
Public Function InsertBookmark(strBookmark As String) As Boolean

On Error GoTo InsertBookmark_Error

oWordTemplate.Activate
oWordTemplate.Bookmarks(strBookmark).Select
Me.oWordApplication.Selection.Copy
oWordDocument.Activate
Me.oWordApplication.Selection.EndKey wdStory
Me.oWordApplication.Selection.Paste
oWordDocument.Bookmarks(strBookmark).Select
InsertBookmark = True
Exit Function

InsertBookmark_Error:

InsertBookmark = False

End Function

----------------
'Delete Bookmark
Public Sub DeleteBookmarkIfStillPresent(strBookmark As String)

With oWord.oWordDocument
.Bookmarks(strBookmark).Delete
End With

Exit Sub

End Sub


-----------------
'Replace Field : [fldLastName], [fldCompany]... by value
Public Sub ReplaceField(strFieldName As String, strValue As String)

With Me.oWordApplication.Selection.Find
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute findtext:="[" & strFieldName & "]"
End With

If Me.oWordApplication.Selection.Find.Found Then
If LenB(strValue) <> 0 Then
Me.oWordApplication.Selection.TypeText strValue
Else
Me.oWordApplication.Selection.Delete
End If
Endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top