How can I prevent unintended excel instances from being launched?

Using the code below (which works), I execute an Excel template and then customize the resulting document.  Everything works just fine.  The only bug is that everytime the code runs I get two instances of Excel running instead of just the one.  Not at all sure why;  I really want to prevent this extra instance from being launched.  Any ideas?
Private Sub makeReport()
        Dim myExcel As Excel.Application = New Excel.Application
        Dim myWorkbook As Excel.Workbook = myExcel.Workbooks.Add
        Dim dateChose As Date = Me.DateTimePicker1.Value
        myWorkbook = myExcel.Workbooks.Open(PathToDocumentTemplate, 0, False, 5, "", "", False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows)
        Dim myWorksheet As Excel.Worksheet = myWorkbook.Worksheets(1)


        ' Edit the worksheet (prep for user)'
        writeToCellInXL(myWorksheet, 3, 2, Me.cmbLocation.Text, "")
        writeToCellInXL(myWorksheet, 4, 2, dateChose.ToLongDateString.ToString, "")
        writeToCellInXL(myWorksheet, 25, 2, Me.cmbLocation.Text, "")
        writeToCellInXL(myWorksheet, 26, 2, dateChose.ToLongDateString.ToString, "")

        ' Set Excel visible so that the user can work with it'
        myExcel.Visible = True
       
        ' Cleanup - release the references'
        Try
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myWorksheet)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myWorkbook)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myWorkbook)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myExcel)

        Catch ex As Exception
            myWorksheet = Nothing
            myWorkbook = Nothing
            myExcel = Nothing
        End Try

    End Sub

Open in new window

LVL 15
David L. HansenCEOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

omegaomegaDeveloperCommented:
Hello, sl8rz,

I can't see anything in the code that you have shown that would cause two instances of Excel to be created.  

However, you do create/Open two workbooks within Excel.  The first is created by the statement:

     Dim myWorkbook As Excel.Workbook = myExcel.Workbooks.Add

This workbook does not appear to be used, but the reference to it is immediately replaced by the statement:

     myWorkbook = myExcel.Workbooks.Open(PathToDocumentTemplate, ...

Perhaps you are seeing these two workbooks and thinking that you have two instances of Excel?

BTW, I also notice that you have a redundant line:

     System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myWorkbook)

in your Try block.

Cheers,
Randy
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
David L. HansenCEOAuthor Commented:
That was it!  All I had to do was change:
Dim myWorkbook As Excel.Workbook = myExcel.Workbooks.Add

To:
Dim myWorkbook As Excel.Workbook

Works great!

Thanks a lot.
0
David L. HansenCEOAuthor Commented:
Much appreciated.
0
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.