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. HansenProgrammer AnalystAsked:
Who is Participating?
 
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
 
David L. HansenProgrammer AnalystAuthor 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. HansenProgrammer AnalystAuthor Commented:
Much appreciated.
0
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.

All Courses

From novice to tech pro — start learning today.