Pregunta

I'm trying to call the 5th sheet in an open workbook. When I open the workbook from the program I seem to be able to do it:

Dim CurrentRun As New Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet


Private Sub GeneralButtonOpener_Click(sender As Object, e As EventArgs) Handles GeneralButtonOpener.Click

    CurrentRun.Visible = True
    CurrentBook = CurrentRun.Workbooks.Add(MainTemplatePath)
    CurrentSheet = CurrentBook.Worksheets(4)
    CurrentSheet.Activate()

End Sub

But all my attempts at calling the sheet if the file is already open have failed:

    Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
    Dim CurrentBook As Microsoft.Office.Interop.Excel.Workbook
    Dim CurrentSheet As Microsoft.Office.Interop.Excel.Worksheet

    CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    CurrentBook = CurrentRun.Workbooks
    CurrentSheet = CurrentBook.Sheets(4)
    CurrentSheet.Activate()

or

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
    Dim CurrentBook As Excel.Workbook
    Dim CurrentSheet As Excel.Worksheet

    CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    CurrentBook = CurrentRun.ActiveWorkbook
    CurrentSheet = CurrentBook.Sheets(4)
    CurrentSheet.Activate()
End Sub

I've looked at several examples, but I can't figure out where I'm going wrong. Which surprises me as there seem to be a lot of questions on the subject. Ether a pointer to where this is solved/addressed or what I'm specifically doing wrong would be appreciated.

Thanks!

¿Fue útil?

Solución

Much to my surprise, I found that I in fact had dozens of instances of Excel running in the background. When I am debugging, and launch the COM the first instance of Excel is started. The second is started when I open the windows form (the main part of the add-in).

What I didn't know, was that when an exception was thrown, and I stop things from within Visual Studio, only the first instance of Excel was closed. I have code that tries and clean up open applications of Excel, but of course it was not reached because an exception was thrown.

And here I had been thinking I would put Error handling a bit down the road when I had things a little more developed. Clearly I need to address some basic error handling much earlier in my build process. I'm entirely self taught, and somehow made it three years without that being an issue.

Hopefully someone else who wasn't been taught that can see this before pulling their hair out for 14 hours.

Solution Close all other instances of Excel and the above code works. Address cleanup in error handling and earlier as addressed here: http://support.microsoft.com/kb/317109 Maybe also call the GC, though that seems controversial.

Final code:

....
Imports System.Runtime.InteropServices
....
Dim CurrentRun As New Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
....
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    On Error GoTo VortexInYourSoul
    CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    CurrentBook = CurrentRun.Workbooks(1)
    CurrentRun.Visible = True
    CurrentSheet = CurrentBook.Sheets(8)
    CurrentSheet.Activate()
    CurrentBook.ActiveSheet.name = "LLAMA LALA LLAMALMALMAL"
    ....
  Exit Sub
VortexInYourSoul:

     CurrentSheet = Nothing
     CurrentBook.Close(False)
     CurrentBook = Nothing
     CurrentRun.Quit()
     CurrentRun = Nothing
     MsgBox("Error: " & Err.Description)

  End Sub
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top