Class Method takes Forever to run.
I have a class(EncounterForm) that basicly is fed SQL patient Data and prints a Word Doc. with the patient Info on it.
the method(.FormFill) to do this works well enough, but it pauses the first time it is called, I mean like a 3 or 4 minute pause. then it runs fine for the rest of the records. What am I doing to cause this pause, and why does it work after the first call?
The code for the method is :
Public Function FormFill(ByVal docname) As String
Dim wordApp = New Word.Application
wordApp = CreateObject("Word.Application")
Dim wordDoc = New Word.Document
Try
Application.DoEvents()
With wordApp
.WindowState = Word.WdWindowState.wdWindowStateMinimize
.Visible = False
wordDoc = wordApp.Documents.Open(trim("Q:\" + docname), , True)
wordDoc.Activate()
.ActiveDocument.Bookmarks("PtName").Select()
.Selection.Text = mstrPatientName
.ActiveDocument.Bookmarks("Street1").Select()
.Selection.Text = mstrStreet1
.ActiveDocument.Bookmarks("Street2").Select()
.Selection.Text = mstrStreet2
.ActiveDocument.Bookmarks("City").Select()
.Selection.Text = mstrCity
.ActiveDocument.Bookmarks("State").Select()
.Selection.Text = mstrState
.ActiveDocument.Bookmarks("Zip").Select()
.Selection.Text = mstrZip
.ActiveDocument.Bookmarks("Phone").Select()
.Selection.Text = mstrPhone
.ActiveDocument.Bookmarks("InsName").Select()
.Selection.Text = mstrInsurance
.ActiveDocument.Bookmarks("Policy").Select()
.Selection.Text = mstrPolicy
.ActiveDocument.Bookmarks("CoPay").Select()
.Selection.Text = mstrCoPay
.ActiveDocument.Bookmarks("CoIns").Select()
.Selection.Text = mstrCoIns
'.ActiveDocument.Bookmarks("Attachments").Select()
'.Selection.Text = mstrAttachments
.ActiveDocument.Bookmarks("PtName2").Select()
.Selection.Text = mstrPatientName
'.ActiveDocument.Bookmarks("Acct").Select()
' .Selection.Text = mstrAcct
.ActiveDocument.Bookmarks("Mr").Select()
.Selection.Text = mstrMr
.ActiveDocument.Bookmarks("Dob").Select()
.Selection.Text = mstrDOB
.ActiveDocument.Bookmarks("Dt").Select()
.Selection.Text = mstrDte
.ActiveDocument.Bookmarks("Time").Select()
.Selection.Text = mstrTime
.ActiveDocument.Bookmarks("Service").Select()
.Selection.Text = mstrService
.ActiveDocument.Bookmarks("Category").Select()
.Selection.Text = mstrCategory
.ActiveDocument.PrintOut(background:=False) 'then print it out
.ActiveDocument.Close(False)
End With
wordApp.quit()
GoTo Cleanup
Catch ex As Exception
MessageBox.Show("Error accessing Word document.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
wordApp.quit()
GoTo Cleanup
End Try
Cleanup:
wordApp = Nothing
wordDoc = Nothing
GC.Collect() ' <-- *** Important! ***
GC.WaitForPendingFinalizers() ' <-- *** Important! ***
End Function
----------------------
I would appreciate any input on this problem
|