COM Cleanup andFinally

I have the following code, should the portion starting with    'Cleanup be in a finally block, my concern is that if for instance it does not get to the document creation it will try and release it, ie it fails on creating a word application


                       

      Try
                                        File.WriteAllBytes(sTemporaryPath, mergeDocument.MergeDocument)

                                        Dim word As New Word.Application
                                        Dim document = word.Documents.Open(CType(sTemporaryPath, Object))


                                        'Cleanup
                                        GC.Collect()
                                        GC.WaitForPendingFinalizers()
                                        GC.Collect()
                                        GC.WaitForPendingFinalizers()

                                        document.Close()
                                        Marshal.FinalReleaseComObject(document)
                                        word.Quit()
                                        Marshal.FinalReleaseComObject(word)


                                    Catch ex As Exception
                                 
                                    End Try
rocky050371Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
You have to close first.

document.Close()
word.quit
word = nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Marshal.FinalReleaseComObject(document)
Marshal.FinalReleaseComObject(word)

hope this help
0
rocky050371Author Commented:
It was really should I be enclosing this within the finally block or leave within the executing code?
0
mastooCommented:
My syntax is a little rusty, but you could structure it as...
Dim word As Word.Application = null
Dim document As word.Document = null
Try
  File.WriteAllBytes(sTemporaryPath, mergeDocument.MergeDocument)
  word = New Word.Application
  document = word.Documents.Open(CType(sTemporaryPath, Object))
Catch ex As Exception
 error handling here
Finally

  'Cleanup
  GC.Collect()
  GC.WaitForPendingFinalizers()
  GC.Collect()
  GC.WaitForPendingFinalizers()
  document.Close()
  If document is not null then
    Marshal.FinalReleaseComObject(document)
  Endif
  If word is not null then
    word.Quit()
    Marshal.FinalReleaseComObject(word)
End Try

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
It depends if you want to keep de document open or not.

Or if you open multiple documents in the same application.
0
rocky050371Author Commented:
I will effectively be opening one document populating it then opening then next, I don't really want to start creating data sources.
0
Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
Ok,

  Then
 
   in the Class
    Dim word As Word.Application = Nothing
    Dim document As word.Document = Nothing

     in the load event

          word = New Word.Application

    While opening and closing documents

    Try
       document = word.Documents.Open(CType(sTemporaryPath, Object)) 'Open the document
       'Populate de document
       'Save the document
       document.Close()
       Document = nothing
    Catch ex As Exception
       error handling here
    End Try
 
At the end of the process

     'Cleanup
     GC.Collect() 'Dont know why you do a garbage colecction
     GC.WaitForPendingFinalizers()
     word.Quit()

The Word application can still opened because what you want is open diferents documents but the application object is the same and there is no reason to open more than one.

Hope this help
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.