D
Dave
Apologies for the newbie question. I have created a vb.net program for
my company that is designed to work with Word Templates (about forty of
them that we commonly use) that are selected by the user and populated
(with info from an Access database) at run-time, then saved as Word
documents. The program I have coded works fine -- it does what I need
it to do. But it has two problems: (1) it runs very slowly, and (2) it
does not seem to "let go" of the Word documents after it has created
them (the documents can be opened and printed, but not deleted, for
example), at least not until the program is run again and it takes hold
of the new batch of documents. The backbone of the program is about
forty procedures that all look like this:
Public Sub PopulateLtrPayTaxBill()
Dim wrdApp As New Microsoft.Office.Interop.Word.Application
Dim doc12 As New Microsoft.Office.Interop.Word.Document
Try
doc12 = wrdApp.Documents.Add(Template:="C:\Documents and
Settings\David\Application Data\Microsoft\Templates\FRMltr CityTown pay
tax bill.dot")
doc12.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Ltr Pay Tax Word
document.")
End Try
'Then there are many lines of code that set bookmarks in the template
document to values that have either been entered by the user or are
extracted from the database. I am leaving these out to save space.
SaveTheDocument(doc12, "ltr " & ThisCollector.CollCity & " pay
tax bill.doc")
'This SaveTheDocument procedure just checks to see whether a new client
folder has to be created, creates one if necessary, then saves the
document created from the Word template as a Word document with the
specified name
wrdApp.Quit()
MessageBox.Show("Done. Ltr Pay Tax Bill Saved to ClientFiles\"
& foldername)
End Sub
I have two main questions. The first one is, am I wasting a ton of
processing capacity by creating a new instance of Word each time I need
to create a new document from a template? Ordinarily, a user of this
program is selecting 5-6 documents that they want to create at one time
(then terminating the program). Would I be better off creating a
single instance of Word.Application every time my program runs, and
simply having each procedure create a new Word.Document object? The
second question is, am I doing enough to "clean up" after this
procedure runs? Should I be setting wrdApp to Nothing? Should I be
running a more comprehensive cleanup procedure every time I "open" and
"close" a Word Document, or the Word Application? Any advice people
have as to how I could improve/streamline the structure of this program
would be very welcome.
Dave
my company that is designed to work with Word Templates (about forty of
them that we commonly use) that are selected by the user and populated
(with info from an Access database) at run-time, then saved as Word
documents. The program I have coded works fine -- it does what I need
it to do. But it has two problems: (1) it runs very slowly, and (2) it
does not seem to "let go" of the Word documents after it has created
them (the documents can be opened and printed, but not deleted, for
example), at least not until the program is run again and it takes hold
of the new batch of documents. The backbone of the program is about
forty procedures that all look like this:
Public Sub PopulateLtrPayTaxBill()
Dim wrdApp As New Microsoft.Office.Interop.Word.Application
Dim doc12 As New Microsoft.Office.Interop.Word.Document
Try
doc12 = wrdApp.Documents.Add(Template:="C:\Documents and
Settings\David\Application Data\Microsoft\Templates\FRMltr CityTown pay
tax bill.dot")
doc12.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Ltr Pay Tax Word
document.")
End Try
'Then there are many lines of code that set bookmarks in the template
document to values that have either been entered by the user or are
extracted from the database. I am leaving these out to save space.
SaveTheDocument(doc12, "ltr " & ThisCollector.CollCity & " pay
tax bill.doc")
'This SaveTheDocument procedure just checks to see whether a new client
folder has to be created, creates one if necessary, then saves the
document created from the Word template as a Word document with the
specified name
wrdApp.Quit()
MessageBox.Show("Done. Ltr Pay Tax Bill Saved to ClientFiles\"
& foldername)
End Sub
I have two main questions. The first one is, am I wasting a ton of
processing capacity by creating a new instance of Word each time I need
to create a new document from a template? Ordinarily, a user of this
program is selecting 5-6 documents that they want to create at one time
(then terminating the program). Would I be better off creating a
single instance of Word.Application every time my program runs, and
simply having each procedure create a new Word.Document object? The
second question is, am I doing enough to "clean up" after this
procedure runs? Should I be setting wrdApp to Nothing? Should I be
running a more comprehensive cleanup procedure every time I "open" and
"close" a Word Document, or the Word Application? Any advice people
have as to how I could improve/streamline the structure of this program
would be very welcome.
Dave