Mở tờ khai excel lỗi exception from hresult 0x800a01a8 năm 2024

[{"Line of Business":{"code":"LOB10","label":"Data and AI"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSDLC4","label":"IBM Planning Analytics Advanced"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"204"}]

On an example of a simple scenario, this article demonstrates what causes 0x800A01A8 and how to avoid it in C#, VB.NET, VBA, Delphi. Exception from HRESULT: 0x800A01A8 is also known as OLE error 800A01A8 and also known as Object Required.

Program. Here’s a method reproducing the issue. The method is an event handler of the Click event of a Ribbon button shown by a PowerPoint add-in. In the method, I get a shape object, retrieve the object’s Parent property, and delete the shape.

C#:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {

PowerPoint.Shape titleShape = (PowerPointApp.ActiveWindow.View.Slide as PowerPoint.Slide).Shapes.Title;
object parent = null;
try {
    titleParent = titleShape.Parent;
} catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
if (titleParent != null) {
    // use titleParent
}
titleShape.Delete();
}

Code samples in VB.NET, VBA, Delphi

Scenario. Now run the method, click the Undo command in the PowerPoint UI, run the method again and you see the exception.

In this scenario the following properties of the PowerPoint.Shape object produce the same exception:

PowerPoint.Shape properties producing 0x800A01A8 in the given scenario

Solution. To solve the issue in the example above, release the shape object after you delete it. In C# and VB.NET, you do this by calling System.Runtime.InteropServices.Marshal.ReleaseComObject(titleShape). In VBA and Delphi, just set titleShape to Nothing (VBA) and nil (Delphi).

Conclusion. What you saw is just another manifestation of all those problems that occur if you don’t release COM objects in Office solutions. Releasing COM objects immediately after use is the best practice. See also When to release COM objects in Office add-ins developed in .NET.

Good luck!

Updated on 1-Jul-2014. I gladly accept the suggestion from Fabske. Below are the modified VB.NET and C# versions of the code. These versions demonstrate releasing all COM objects. The suggestion to set the just released variable to null (Nothing in VB.NET) is given in When to release COM objects in Office add-ins developed in .NET

I did some research and it seems to do with late binding, code that is giving me the exception follows:

Code:

Dim ZZ As String = "J" Dim AJ As Excel.Range = CType(wSheet.Columns("A" & ZZ), Range)

    With AJ  
        For g = 1 To 9  
            .Select()  
            .ClearContents()  
            ZZ = Chr(Asc(ZZ) + 1)  
        Next  
    End With
I read on msdn if AJ in this case starts out as nothing then vb will throw this exception and to use early binding. I cant bind a column if it hasn't been created yet, so i played around with the placement of the declaration of AJ to no avail, what is the right Direction i should take to fix this issue?


  • Apr 17th, 2009, 10:42 AM

    Re: Exception from HRESULT: 0x800A01A8 Early binding, in this case, is mostly useful for debugging, and has a different meaning than when talking about binding to a control. Aren't Excel exceptions really excellent and informative? They tell you all you need to know...well, ok, maybe not. In fact, I have never found a more useless error message than those.

    I'm no expert with Excel, so perhaps ranges can work that way, but are you sure that a column can be converted to a range? After looking through what little Excel I have used, I can't find any example that indicates one way or the other.

    Second, you appear to be clearing 9 columns. Is the number of rows unknown? If not, you can put together a range that is the whole chunk, since they are contiguous rows. While that may not solve your problem, the very paucity of Excel error message information means that trying something simpler, such as getting rid of the loop, would be a direction to try. My usual boring signature: Nothing

    Mở tờ khai excel lỗi exception from hresult 0x800a01a8 năm 2024
    Mở tờ khai excel lỗi exception from hresult 0x800a01a8 năm 2024

    -
    • Apr 17th, 2009, 11:04 AM

      Mở tờ khai excel lỗi exception from hresult 0x800a01a8 năm 2024
      Thread Starter Member
      Mở tờ khai excel lỗi exception from hresult 0x800a01a8 năm 2024

      -

      Re: Exception from HRESULT: 0x800A01A8

      Was trying to find a way around not using a loop as to not bloat the code too much, but i commented the loop out.

    Code:

    Dim AJ As Excel.Range = CType(wSheet.Columns("AJ"), Range) With AJ > 'For g = 1 To 9 > .Select() > .EntireColumn.ClearContents() > 'ZZ = Chr(Asc(ZZ) + 1) > 'Next > End With

    I no longer receive the exception but the column does not clear.

    I tried this simple code:

    Code:

    wSheet.Range("AJ").Select() > wSheet.Range("AJ").ClearContents()

    throws this Exception from HRESULT: 0x800A03EC

    The VB help file states: To work around this behavior, use early-bound objects or pass a variable instead of a property of the object.

    Is this saying that i need to specify what "AJ" is before the excel code even executes? Or, should i dim a string variable of "AJ" then use the variable instead of actually typing "AJ"?


    • Apr 17th, 2009, 12:02 PM

      Re: Exception from HRESULT: 0x800A01A8

      I would say that the problem has to do with the "AJ argument. If you don't get the error with the loop code commented out, is it enough just to do something like: AJ.Select AJ.ClearContents After all, you appear to be correctly creating the range in the first snippet. Actually, try without the Select statement, as well. That's probably not necessary, as you already have a range, and you don't really care whether or not it is selected, just that it is cleared.