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 7 6195
Dave,
I've a .Net app does reporting to word documents. I haven't fixed all of
the nuisances with slow operation but what I've found works for cleaning up
is this (sorry if I'm including too much code):
Dim inVal As Object
inVal = New System.Runtime.InteropServices.DispatchWrapper(Not hing)
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdTbl As Word.Table
Dim wdPara As Word.Paragraph
Dim wdRng As Word.Range
oTemplt = "your template name"
'Create Report Name for this month
oRept = "your report name" & ".DOC"
'Open the report template
wdApp = CType(CreateObject("Word.Application"), Word.Application)
wdApp.Visible = False
wdDoc = wdApp.Documents.Open(oTemplt, , True)
wdRng = wdDoc.Bookmarks.Item("DocHeader").Range
wdRng.Text = "Open Escalations"
wdRng = wdDoc.Bookmarks.Item("DocDate").Range
wdRng.Text = Format(Today(), "MMM dd, yyyy")
wdDoc.SaveAs(oRept) 'Save with the new
report name
.... document generation ....
'Save & Close the Word Document
With wdDoc
.Save()
.Close()
End With
wdApp.Visible = True
wdApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp)
wdApp = Nothing
wdDoc = Nothing
wdPara = Nothing
wdRng = Nothing
wdTbl = Nothing
Hope this helps a bit
"Dave" wrote: 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
Thanks Taras! I think this may be the code I'm missing -- particularly
"System.Runtime.InteropServices.Marshal.ReleaseCom Object(wdApp)"
Dave
Thanks Taras! That's definitely more closing-out and cleaning-up than
I've been doing. I will look into this line in particular, which looks
on the face of it like it may solve the problem I've been having with
the program not "letting go" --
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp)
Dave
Hi Taras
I have found that the following works well, and closes Word afterwards. It
also speeds operations up quite a bit over the default.
<snip>
Dim m_WordApp As Word.Application
Dim m_Document As Word.Document
Dim m_TotalParagraphs As Integer
m_WordApp = New Word.Application
Try
m_Document = m_WordApp.Documents.Open(CType("YourFile.doc", Object),
ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
' Set options to improve speed
With m_Document
.AcceptAllRevisions()
.ShowGrammaticalErrors = False
.ShowSpellingErrors = False
.SpellingChecked = True
.GrammarChecked = True
With .Application
.ScreenUpdating = False
.Options.Pagination = False
End With
End With
' Reformat the document
m_Document.Repaginate()
' Cache the number of paragraphs here, if needed
m_TotalParagraphs = m_Document.Paragraphs.Count
' Do stuff here
' ...
Catch ex As Exception
' Any error at this stage we have to give up
Try
If Not m_Document Is Nothing Then
m_Document.Close(SaveChanges:=False)
End If
Catch ex2 As Exception
End Try
Finally
' Done
For Each doc As Word.Document In m_WordApp.Documents
doc.Close()
Next
m_WordApp.Quit()
End Try
</snip>
HTH
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com... Dave, I've a .Net app does reporting to word documents. I haven't fixed all of the nuisances with slow operation but what I've found works for cleaning up is this (sorry if I'm including too much code):
Dim inVal As Object inVal = New System.Runtime.InteropServices.DispatchWrapper(Not hing)
Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim wdTbl As Word.Table Dim wdPara As Word.Paragraph Dim wdRng As Word.Range
oTemplt = "your template name"
'Create Report Name for this month oRept = "your report name" & ".DOC"
'Open the report template wdApp = CType(CreateObject("Word.Application"), Word.Application) wdApp.Visible = False wdDoc = wdApp.Documents.Open(oTemplt, , True) wdRng = wdDoc.Bookmarks.Item("DocHeader").Range wdRng.Text = "Open Escalations" wdRng = wdDoc.Bookmarks.Item("DocDate").Range wdRng.Text = Format(Today(), "MMM dd, yyyy") wdDoc.SaveAs(oRept) 'Save with the new report name
.... document generation ....
'Save & Close the Word Document With wdDoc .Save() .Close() End With wdApp.Visible = True wdApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp)
wdApp = Nothing wdDoc = Nothing wdPara = Nothing wdRng = Nothing wdTbl = Nothing
Hope this helps a bit
"Dave" wrote:
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
Charles,
You have some interesting options that I'll incorporate. Couldn't find
references where I could do those things before. What source did you get
some of the information from? The one piece that I find annoying is that
when I'm generating the report (which can take a few minutes) CPU usage is
close to 100%. Do you know of any way of spinning off a thread or something
that reduces usage?
(Dave, sorry for trying to solve some of my issues in your post)
"Charles Law" wrote: Hi Taras
I have found that the following works well, and closes Word afterwards. It also speeds operations up quite a bit over the default.
<snip> Dim m_WordApp As Word.Application Dim m_Document As Word.Document Dim m_TotalParagraphs As Integer
m_WordApp = New Word.Application
Try m_Document = m_WordApp.Documents.Open(CType("YourFile.doc", Object), ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
' Set options to improve speed With m_Document .AcceptAllRevisions() .ShowGrammaticalErrors = False .ShowSpellingErrors = False .SpellingChecked = True .GrammarChecked = True
With .Application .ScreenUpdating = False .Options.Pagination = False End With End With
' Reformat the document m_Document.Repaginate()
' Cache the number of paragraphs here, if needed m_TotalParagraphs = m_Document.Paragraphs.Count
' Do stuff here ' ...
Catch ex As Exception ' Any error at this stage we have to give up Try If Not m_Document Is Nothing Then m_Document.Close(SaveChanges:=False) End If
Catch ex2 As Exception
End Try
Finally ' Done For Each doc As Word.Document In m_WordApp.Documents doc.Close() Next
m_WordApp.Quit()
End Try
</snip>
HTH
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message news:87**********************************@microsof t.com... Dave, I've a .Net app does reporting to word documents. I haven't fixed all of the nuisances with slow operation but what I've found works for cleaning up is this (sorry if I'm including too much code):
Dim inVal As Object inVal = New System.Runtime.InteropServices.DispatchWrapper(Not hing)
Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim wdTbl As Word.Table Dim wdPara As Word.Paragraph Dim wdRng As Word.Range
oTemplt = "your template name"
'Create Report Name for this month oRept = "your report name" & ".DOC"
'Open the report template wdApp = CType(CreateObject("Word.Application"), Word.Application) wdApp.Visible = False wdDoc = wdApp.Documents.Open(oTemplt, , True) wdRng = wdDoc.Bookmarks.Item("DocHeader").Range wdRng.Text = "Open Escalations" wdRng = wdDoc.Bookmarks.Item("DocDate").Range wdRng.Text = Format(Today(), "MMM dd, yyyy") wdDoc.SaveAs(oRept) 'Save with the new report name
.... document generation ....
'Save & Close the Word Document With wdDoc .Save() .Close() End With wdApp.Visible = True wdApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp)
wdApp = Nothing wdDoc = Nothing wdPara = Nothing wdRng = Nothing wdTbl = Nothing
Hope this helps a bit
"Dave" wrote:
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
Much of it was trial and error. I used the intellisense and object browser
to come up with likely looking candidates and then tried them out,
one-by-one.
The documents I process are up to 15 Mb in size and so take some to load in
a visible copy of Word. I noticed that Word would always perform a
repagination and spelling check when I opened a document (you don't notice
it on small documents because it is so quick), which gave me the idea to
turn these features off when automating Word.
Another thing that can affect speed of processing is the version of Word
used to save the document and the version used for automation. My client
uses Word 2000 and I use Word 2003. If I take a Word 2000 document directly
and automate it, it runs quite slowly. If I load it and save it using Word
2003 first, and then automate it with Word 2003 then it is quite a bit
quicker, possibly halving the time taken to process it.
With regard to the cpu percentage, I think you are probably stuck with this,
although the 'Save and Automate' technique might help slightly. I run my
processing on a different thread and it still uses 50%+ cpu. The most likely
culprit is the marshalling backwards and forwards between managed an
unmanaged code. There is quite a lot of work to do with each COM call, which
can seriously affect performance and increase cpu percentage.
HTH
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com... Charles, You have some interesting options that I'll incorporate. Couldn't find references where I could do those things before. What source did you get some of the information from? The one piece that I find annoying is that when I'm generating the report (which can take a few minutes) CPU usage is close to 100%. Do you know of any way of spinning off a thread or something that reduces usage?
(Dave, sorry for trying to solve some of my issues in your post)
"Charles Law" wrote:
Hi Taras
I have found that the following works well, and closes Word afterwards. It also speeds operations up quite a bit over the default.
<snip> Dim m_WordApp As Word.Application Dim m_Document As Word.Document Dim m_TotalParagraphs As Integer
m_WordApp = New Word.Application
Try m_Document = m_WordApp.Documents.Open(CType("YourFile.doc", Object), ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
' Set options to improve speed With m_Document .AcceptAllRevisions() .ShowGrammaticalErrors = False .ShowSpellingErrors = False .SpellingChecked = True .GrammarChecked = True
With .Application .ScreenUpdating = False .Options.Pagination = False End With End With
' Reformat the document m_Document.Repaginate()
' Cache the number of paragraphs here, if needed m_TotalParagraphs = m_Document.Paragraphs.Count
' Do stuff here ' ...
Catch ex As Exception ' Any error at this stage we have to give up Try If Not m_Document Is Nothing Then m_Document.Close(SaveChanges:=False) End If
Catch ex2 As Exception
End Try
Finally ' Done For Each doc As Word.Document In m_WordApp.Documents doc.Close() Next
m_WordApp.Quit()
End Try
</snip>
HTH
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message news:87**********************************@microsof t.com... > Dave, > I've a .Net app does reporting to word documents. I haven't fixed all > of > the nuisances with slow operation but what I've found works for > cleaning > up > is this (sorry if I'm including too much code): > > Dim inVal As Object > inVal = New > System.Runtime.InteropServices.DispatchWrapper(Not hing) > > Dim wdApp As Word.Application > Dim wdDoc As Word.Document > Dim wdTbl As Word.Table > Dim wdPara As Word.Paragraph > Dim wdRng As Word.Range > > oTemplt = "your template name" > > 'Create Report Name for this month > oRept = "your report name" & ".DOC" > > 'Open the report template > wdApp = CType(CreateObject("Word.Application"), > Word.Application) > wdApp.Visible = False > wdDoc = wdApp.Documents.Open(oTemplt, , True) > wdRng = wdDoc.Bookmarks.Item("DocHeader").Range > wdRng.Text = "Open Escalations" > wdRng = wdDoc.Bookmarks.Item("DocDate").Range > wdRng.Text = Format(Today(), "MMM dd, yyyy") > wdDoc.SaveAs(oRept) 'Save with the new > report name > > .... document generation .... > > > 'Save & Close the Word Document > With wdDoc > .Save() > .Close() > End With > wdApp.Visible = True > wdApp.Quit() > System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp) > > wdApp = Nothing > wdDoc = Nothing > wdPara = Nothing > wdRng = Nothing > wdTbl = Nothing > > Hope this helps a bit > > "Dave" wrote: > >> 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 >> >>
One other thing to add: there are pros and cons to the CreateObject() and
New methods of starting Word.
By using late-binding, the former makes it more likely that your application
will work with different versions of Word on the host machine. However, I
have found that if you use the latter method and build against the lowest
version that you want to support, e.g. Word 2000, then the same solution
should also work with Word 2003. The two versions use different managed
wrappers, and Word 2000 does not work with a wrapper created for Word 2003,
but Word 2003 can work with a wrapper created for Word 2000.
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com... Charles, You have some interesting options that I'll incorporate. Couldn't find references where I could do those things before. What source did you get some of the information from? The one piece that I find annoying is that when I'm generating the report (which can take a few minutes) CPU usage is close to 100%. Do you know of any way of spinning off a thread or something that reduces usage?
(Dave, sorry for trying to solve some of my issues in your post)
"Charles Law" wrote:
Hi Taras
I have found that the following works well, and closes Word afterwards. It also speeds operations up quite a bit over the default.
<snip> Dim m_WordApp As Word.Application Dim m_Document As Word.Document Dim m_TotalParagraphs As Integer
m_WordApp = New Word.Application
Try m_Document = m_WordApp.Documents.Open(CType("YourFile.doc", Object), ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
' Set options to improve speed With m_Document .AcceptAllRevisions() .ShowGrammaticalErrors = False .ShowSpellingErrors = False .SpellingChecked = True .GrammarChecked = True
With .Application .ScreenUpdating = False .Options.Pagination = False End With End With
' Reformat the document m_Document.Repaginate()
' Cache the number of paragraphs here, if needed m_TotalParagraphs = m_Document.Paragraphs.Count
' Do stuff here ' ...
Catch ex As Exception ' Any error at this stage we have to give up Try If Not m_Document Is Nothing Then m_Document.Close(SaveChanges:=False) End If
Catch ex2 As Exception
End Try
Finally ' Done For Each doc As Word.Document In m_WordApp.Documents doc.Close() Next
m_WordApp.Quit()
End Try
</snip>
HTH
Charles
"Taras" <Ta***@discussions.microsoft.com> wrote in message news:87**********************************@microsof t.com... > Dave, > I've a .Net app does reporting to word documents. I haven't fixed all > of > the nuisances with slow operation but what I've found works for > cleaning > up > is this (sorry if I'm including too much code): > > Dim inVal As Object > inVal = New > System.Runtime.InteropServices.DispatchWrapper(Not hing) > > Dim wdApp As Word.Application > Dim wdDoc As Word.Document > Dim wdTbl As Word.Table > Dim wdPara As Word.Paragraph > Dim wdRng As Word.Range > > oTemplt = "your template name" > > 'Create Report Name for this month > oRept = "your report name" & ".DOC" > > 'Open the report template > wdApp = CType(CreateObject("Word.Application"), > Word.Application) > wdApp.Visible = False > wdDoc = wdApp.Documents.Open(oTemplt, , True) > wdRng = wdDoc.Bookmarks.Item("DocHeader").Range > wdRng.Text = "Open Escalations" > wdRng = wdDoc.Bookmarks.Item("DocDate").Range > wdRng.Text = Format(Today(), "MMM dd, yyyy") > wdDoc.SaveAs(oRept) 'Save with the new > report name > > .... document generation .... > > > 'Save & Close the Word Document > With wdDoc > .Save() > .Close() > End With > wdApp.Visible = True > wdApp.Quit() > System.Runtime.InteropServices.Marshal.ReleaseComO bject(wdApp) > > wdApp = Nothing > wdDoc = Nothing > wdPara = Nothing > wdRng = Nothing > wdTbl = Nothing > > Hope this helps a bit > > "Dave" wrote: > >> 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 >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Duncan Smith |
last post by:
Hello,
I'm having a little trouble trying to automate Word (2002). I'm
hoping to be able to create and process documents relatively independently
of any existing open word documents. e.g. When...
|
by: SpamProof |
last post by:
I'd like to be able to print multiple documents are once using vb.net.
What is the best way to do this with out having to print each document
one at a time via a loop .
Can't I send them all to...
|
by: philipl |
last post by:
hi,
I have a program which waits for batches documents to be dropped into
a folder, once this happens, a class which activates the word object
is instaniated per batchg of docs & a number of...
|
by: gizmo |
last post by:
We have a requirement to initiate more than one instance of an
application using the filenames. (the example below will start two
instances of MS Word).
The problem is that we need to close each...
|
by: Helen Trim |
last post by:
I have an application with three forms that are msde
visible and activated when needed. It uses Word to open
documents and one of the forms is opened as the Word
document is closed in the...
|
by: Bob Harrison |
last post by:
After installing the Media SDK and looking at the vb sample I was able to
create a control that includes the media player plus other form controls. I
want to add multiple instances of this control...
|
by: Bob Alston |
last post by:
Looking for someone with experience building apps with multiple
instances of forms open. I am building an app for a nonprofit
organizations case workers. They provide services to the elderly.
...
|
by: LegalIT |
last post by:
Hello,
I have a VB.net application that creates reports from database information. I am able to create the reports fine. The problem is each time I create a report my application starts a new...
|
by: Pittaman |
last post by:
Hello,
We're developing an application (.NET 2.0) that let's users drop documents in the application and allows users to show some document types "embedded". For example, a textfile would appear...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |