locked
Exception on thisworkbook RRS feed

  • Question

  • Hello Comunity

    i am trying to write one of my first excel AddIns.

    I would like to refference the workbook with thisworkbook. I allways get an error no matter how or where i am trying to use thisworkbook.

    For example in the thisaddin class

     Private Sub ThisAddIn_Startup() Handles Me.Startup
            Try
                MsgBox(Application.ThisWorkbook.Name)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    throws

    Exception from HRESULT: 0x800A03EC

    every single time.

    I am using Visual Studio 2010 and Office 2007 on windows 7. I read about some language problems, but they only seem to appear sporadicly.

    Greetings and thanks allot for the help

    Benedikt

    Tuesday, June 12, 2012 9:49 AM

Answers

  • Hi Benehsv,

    Welcome to the MSDN Forum.

    The error code 0x800A03EC (or -2146827284) means NAME_NOT_FOUND; in other words, you've asked for something, and Excel can't find it.

    For your case, I think it is because the AddIn loads immediately after you startup your Excel Application, and at that moment no workbook has been opened.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:30 AM
    Thursday, June 14, 2012 2:24 AM
  • Hello Khalique,

    no i specifficaly wanted to use ThisWorkbook, but it still does not work in any method!

    I probably missunderstood the meaning of ThisWorkbook!

    I thought each workbook loads a seperate workbook which i could than refference by thisworkbook.

    But it seems like the Application Excel loads one AddIn into a xlam file which is (ThisWorkbook). This Application can have multiple Workbooks. I only noticed that behaviour in excel 2010 but not in excel 2007 but i assume it's the same. 

    I found a way to refference the Workbooks I need with ActiveWorkbook.

    Are my assumptions correct?

    Thanks for your help anyway

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:31 AM
    Tuesday, June 19, 2012 11:35 AM
  • There may be more than one application level Add-In loaded in Excel at anyone time, each one posibbly implementing some of the same event handlers. And there may be more than one workbook open at anyone time and events raised by each one of them will cause the relevant event handler code to execute in each Add-In.

    That is why Excel application provides you a workbook object inside some events, like the one you used in the code exampe above. This specific workbook is the one that raised the event. It is your duty to check / verify if the workbook object provided to you as an argument to the event handler is actually the one you want to work with in your code.

    I feel like I made it more difficult to understand!

    kr

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:31 AM
    Wednesday, June 20, 2012 2:36 PM

All replies

  • Hi Benehsv,

    Welcome to the MSDN Forum.

    The error code 0x800A03EC (or -2146827284) means NAME_NOT_FOUND; in other words, you've asked for something, and Excel can't find it.

    For your case, I think it is because the AddIn loads immediately after you startup your Excel Application, and at that moment no workbook has been opened.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:30 AM
    Thursday, June 14, 2012 2:24 AM
  • Hi Quist

    thanks for your reply.

    I think there might be another problem. ActiveWorkbook works perfectly even at startup. ThisWorkbook doesn't work later on as well. 

    For example when I load this Windows Form "Druckeinstullngen" by pressing a button it throws the same exception. 

     Private Sub Druckeinstellung_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try                       
    
    MsgBox(Globals.ThisAddIn.Application.ThisWorkbook.Name) Catch ex As Exception
            MsgBox(ex.Message)
    End Try

    Regards,

    Benedikt 

    Thursday, June 14, 2012 9:02 AM
  • Benedikt,

    The second exception that you encounter inside your Windows Form has a different reason than the first one. Here, you are trying to access your workbook from a thread that is different than the one where your workbook was created. In other words, Windows Form runs on a different thread. You cannot access your workbook like that. If I am correct, you need to use Globals.Factory.GetVstoObject method for this purpose. Search the method name and you will find quite a few hint how to use it.

    kr

    Thursday, June 14, 2012 1:27 PM
  • hi

    maybe i am being stupid but it still doesn't work. 

    For example if I use thisworkbook in my thisaddin class well after the addin is loaded  it throws the same NAME_NOT_FOUND exception:

        Private Sub Application_WorkbookNewSheet(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook, ByVal Sh As Object) Handles Application.WorkbookNewSheet
    
    
            MsgBox(Application.ThisWorkbook.Name)
       
     End Sub

    getvstoObject throws the same exception in my form class.

    Private Sub UpdatePrints_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdatePrints.Click
            MsgBox(Globals.ThisAddIn.getIsInvoice())
            Dim mywb As excel.Workbook
            mywb = CType(Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ThisWorkbook), Global.Microsoft.Office.Interop.Excel.Workbook)
            MsgBox(mywb.Name)
       End Sub

    I could use activeworkbook but that is simply not the same.

    Thanks for your help!

    Greetings

    Benedikt 

    Thursday, June 14, 2012 1:41 PM
  • Instead of

     MsgBox(Application.ThisWorkbook.Name)

    you should use

     MsgBox(Wb.Name)

    because Excel is providing you Wb as a parameter to the method you are in.

    Remember, there may be several workbooks open in Excel at the same time and your target workbook may not be the one active.

    Regarding GetVstoObject, I have not personally used it so I cannot tell you the specifics how to use it. I have read about it and therefore I mentioned it as a possible solution.

    There another issue with using Windows Forms that is described here with a solution.

    http://www.codeproject.com/Articles/31971/Understanding-SynchronizationContext-Part-I

    Though it doesn't seem to be applicble as such in your code but it is good to know and it may provide you an alternate approach.

     

    • Marked as answer by benehsv Tuesday, June 19, 2012 11:29 AM
    • Unmarked as answer by benehsv Tuesday, June 19, 2012 11:30 AM
    Thursday, June 14, 2012 2:24 PM
  • Hello Khalique,

    no i specifficaly wanted to use ThisWorkbook, but it still does not work in any method!

    I probably missunderstood the meaning of ThisWorkbook!

    I thought each workbook loads a seperate workbook which i could than refference by thisworkbook.

    But it seems like the Application Excel loads one AddIn into a xlam file which is (ThisWorkbook). This Application can have multiple Workbooks. I only noticed that behaviour in excel 2010 but not in excel 2007 but i assume it's the same. 

    I found a way to refference the Workbooks I need with ActiveWorkbook.

    Are my assumptions correct?

    Thanks for your help anyway

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:31 AM
    Tuesday, June 19, 2012 11:35 AM
  • There may be more than one application level Add-In loaded in Excel at anyone time, each one posibbly implementing some of the same event handlers. And there may be more than one workbook open at anyone time and events raised by each one of them will cause the relevant event handler code to execute in each Add-In.

    That is why Excel application provides you a workbook object inside some events, like the one you used in the code exampe above. This specific workbook is the one that raised the event. It is your duty to check / verify if the workbook object provided to you as an argument to the event handler is actually the one you want to work with in your code.

    I feel like I made it more difficult to understand!

    kr

    • Marked as answer by Quist Zhang Monday, June 25, 2012 9:31 AM
    Wednesday, June 20, 2012 2:36 PM