Tag Archives: VBA

VC + + COM programming error of 0xc0000005: access conflict when reading location 0xfeefef6

Calling COM+ gives you the desired result and returns an error after the last return 0 sentence.

error location in comip.h

void _Release() throw()

{

if (m_pInterface! = NULL) {

m_pInterface – & gt; Release(); // Error reported here

}

}
When I encounter this problem myself, I use m_pInterface->; Release(); Comment out the program no longer reports an error.
Some other ways online:
1.m_pInterface-> Release () into m_pInterface = NULL from http://topic.csdn.net/u/20091120/16/8a9c8d91-9a39-416b-8d8d-8c2cd2a71d3e.html
2. In the program implementation: PTR ->; Release(); To PTR. Release (); From http://topic.csdn.net/u/20091120/16/8a9c8d91-9a39-416b-8d8d-8c2cd2a71d3e.html
3. One statement is that it is a matter of scope. I don’t know much about it. See: http://stackoverflow.com/questions/2653797/why-does-couninitialize-cause-an-error-on-exit
This method has been tried, but it also works. See the following instructions for details.

I’m working on a C++ application to read some data from an Excel file. I’ve got it working, but I’m confused about one part. Here’s the code (simplified to read only the first cell).

//Mostly copied from http://www.codeproject.com/KB/wtl/WTLExcel.aspx 
 
#import "c:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" 
#import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" 
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" rename ("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") exclude("IFont", "IPicture") 
 
_variant_t varOption((long) DISP_E_PARAMNOTFOUND, VT_ERROR); 
 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DWORD dwCoInit = 0; 
    CoInitializeEx(NULL, dwCoInit); 
    Excel::_ApplicationPtr pExcel;     
    pExcel.CreateInstance(_T("Excel.Application")); 
    Excel::_WorkbookPtr pBook; 
    pBook = pExcel->Workbooks->Open("c:\\test.xls", varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption); 
    Excel::_WorksheetPtr pSheet = pBook->Sheets->Item[1]; 
    Excel::RangePtr pRange = pSheet->GetRange(_bstr_t(_T("A1"))); 
    _variant_t vItem = pRange->Value2; 
    printf(_bstr_t(vItem.bstrVal));     
    pBook->Close(VARIANT_FALSE); 
    pExcel->Quit(); 
    //CoUninitialize(); 
    return 0; 
} 

I had to comment out the call to CoUninitialize for the program to work. When CoUninitialize is uncommented, I get an access violation in the _Release function in comip.h on program exit.
Here’s the code from comip. h, for what it’s worth.

void _Release() throw() 
{ 
    if (m_pInterface != NULL) { 
        m_pInterface->Release(); 
    } 
} 

I’m not very experienced with COM programming, so there’s probably something obvious I’m missing.

    Why does the call to CoUninitialize cause an exception? What are the consequences of not calling CoUninitialize? Am I doing something completely wrong here?

ANSWER!

The problem you are having is one of scope. The short answer is to move the CoInit and CoUninit into an outer scope from the Ptrs. For example:

//Mostly copied from http://www.codeproject.com/KB/wtl/WTLExcel.aspx 
 
#import "c:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" 
#import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" 
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" rename ("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") exclude("IFont", "IPicture") 
 
_variant_t varOption((long) DISP_E_PARAMNOTFOUND, VT_ERROR); 
 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DWORD dwCoInit = 0; 
    CoInitializeEx(NULL, dwCoInit); 
    { 
        Excel::_ApplicationPtr pExcel;     
        pExcel.CreateInstance(_T("Excel.Application")); 
        Excel::_WorkbookPtr pBook; 
        pBook = pExcel->Workbooks->Open("c:\\test.xls", varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption); 
        Excel::_WorksheetPtr pSheet = pBook->Sheets->Item[1]; 
        Excel::RangePtr pRange = pSheet->GetRange(_bstr_t(_T("A1"))); 
        _variant_t vItem = pRange->Value2; 
        printf(_bstr_t(vItem.bstrVal));     
        pBook->Close(VARIANT_FALSE); 
        pExcel->Quit(); 
    } 
    CoUninitialize(); 
    return 0; 
} 

The longer answer is that the Ptrs destructors (which calls Release) are being called on exit from main. This is after CoUnit which, basically, shuts down the communication channel between your app and the COM object.
What are the consequences of not calling CoUnit?For short lived in-process COM servers, there really isn’t any negative consequence.

VBA returns “n / a” and other error information

When we use Excel’s built-in functions to return A Value, if the argument is wrong or cannot be found, we often return #N/A or #Value! And so on prompt message.
Can the VBA code we write in the module also return this information?
The answer is yes, because the code in the module itself can be called within the formula, so naturally this information can also be returned.
However, it is important to note that this information is not text, it is a built-in error value!
The following table shows some commonly used error values for reference:

Constant

Error Number

Cell Error value

xlErrDiv0

2007

0 # DIV /!
xlErrNA 2042 #N/A
xlErrName 2029 #NAME?
xlErrNull 2000 #NULL!
xlErrNum 2036 #NUM!
xlErrRef 2023 #REF!
xlErrValue 2015 #VALUE!

returns the value by calling CVErr(error constant), so what you see on the interface is the error value we want to display.
Such as:
CVErr(xlErrValue)

VBA error values in Excel

The cell error value can be inserted into the cell, or the CVErr function can be used to test whether the cell value is an error value. The cell error value can be one of the following XlCVError constants.

constant

error number

cell error values

xlErrDiv0

2007

0 # DIV /!
xlErrNA 2042 #N/A
xlErrName 2029 #NAME?
xlErrNull 2000 #NULL!
xlErrNum 2036 #NUM!
xlErrRef 2023 #REF!
xlErrValue 2015 #VALUE!

Excel VBA tutorial: Cell error values · Examples
This example inserts seven cell error values into the A1:A7 cell area of Sheet1.


myArray = Array(xlErrDiv0, xlErrNA, xlErrName, xlErrNull, _
    xlErrNum, xlErrRef, xlErrValue)
For i = 1 To 7
    Worksheets("Sheet1").Cells(i, 1).Value = CVErr(myArray(i - 1))
Next i

This example checks whether the active cell in Sheet1 contains a cell error value and, if so, displays a message. This example serves as a model structure for error handlers for cell error values.


Worksheets("Sheet1").Activate
If IsError(ActiveCell.Value) Then
    errval = ActiveCell.Value
    Select Case errval
        Case CVErr(xlErrDiv0)
            MsgBox "#DIV/0! error"
        Case CVErr(xlErrNA)
            MsgBox "#N/A error"
        Case CVErr(xlErrName)
            MsgBox "#NAME?error"
        Case CVErr(xlErrNull)
            MsgBox "#NULL! error"
        Case CVErr(xlErrNum)
            MsgBox "#NUM! error"
        Case CVErr(xlErrRef)
            MsgBox "#REF! error"
        Case CVErr(xlErrValue)
            MsgBox "#VALUE! error"
        Case Else
            MsgBox "This should never happen!!"
    End Select
End If

Excel VBA: cell error value

The cell error value can be inserted into the cell, or the CVErr function can be used to test whether the cell value is an error value. The cell error value can be one of the following XlCVError constants.

constant

error number

cell error values

xlErrDiv0

2007

0 # DIV /!
xlErrNA 2042 #N/A
xlErrName 2029 #NAME?
xlErrNull 2000 #NULL!
xlErrNum 2036 #NUM!
xlErrRef 2023 #REF!
xlErrValue 2015 #VALUE!

Excel VBA tutorial: Cell error values · Examples
This example inserts seven cell error values into the A1:A7 cell area of Sheet1.


myArray = Array(xlErrDiv0, xlErrNA, xlErrName, xlErrNull, _
    xlErrNum, xlErrRef, xlErrValue)
For i = 1 To 7
    Worksheets("Sheet1").Cells(i, 1).Value = CVErr(myArray(i - 1))
Next i

This example checks whether the active cell in Sheet1 contains a cell error value and, if so, displays a message. This example serves as a model structure for error handlers for cell error values.


Worksheets("Sheet1").Activate
If IsError(ActiveCell.Value) Then
    errval = ActiveCell.Value
    Select Case errval
        Case CVErr(xlErrDiv0)
            MsgBox "#DIV/0! error"
        Case CVErr(xlErrNA)
            MsgBox "#N/A error"
        Case CVErr(xlErrName)
            MsgBox "#NAME?error"
        Case CVErr(xlErrNull)
            MsgBox "#NULL! error"
        Case CVErr(xlErrNum)
            MsgBox "#NUM! error"
        Case CVErr(xlErrRef)
            MsgBox "#REF! error"
        Case CVErr(xlErrValue)
            MsgBox "#VALUE! error"
        Case Else
            MsgBox "This should never happen!!"
    End Select
End If

VBA 400 error

Failure phenomenon: VBA small program, after running macro, sometimes can proceed smoothly, sometimes on the “wind”, 400 errors

Solution: There are a lot of things that could cause 400 errors, but my solution is strange, just activate the worksheet of the action. This object is probably caused by the use of thisWorkBook… This problem can be fixed by using thisWorkbook.sheets (“Sheet1”).activate.

Such as
Set objData = New DataObject
sHTML = Range(“E” & (I)). The Value
sHTML = Replace (sHTML, “& lt; br />” PutInClipboard
ActiveSheet.Paste Destination:=Range(“E” & (i))

As long as to
Set objData = New DataObject
SHTML = Range (” E “& amp; (I)). The Value
sHTML = Replace (sHTML, “& lt; br />” . PutInClipboard
thisworkbook.sheets (“Sheet1”).Activate
activesheet.paste Destination:=Range(“E” & (i))
I’m going to report 400 errors

Runtime Error 1004 Method ‘VBProject’ of object ‘_Workbook’ failed

症状

在Enterprise Performance Foundation Administrator中,当尝试使用位于“Configuration Rules”的“Create from Spreadsheet”按钮导入维度成员时;“维度”比;”成员”,以下错误发生时,您打开WebADI.xls

运行时错误’1004′:

方法’VBProject’的对象’_Workbook’失败

你在打开电子表格时选择了“启用宏”按钮。另外,在Excel 2003中,如果你打开“工具”>“宏”比;”安全…”,安全级别为”中等”.

解决方案

在Excel 2003中,转到“Tools”>“宏”比;“安全”,并选择“可信来源”选项卡。选中“信任访问Visual Basic项目”旁边的复选框。

(注意:还应选中“信任所有已安装的外接程序和模板”框。
在Excel 2007中,使用以下导航:

办公室李

    <>点击按钮在左上角李 <李>单击Excel选项按钮李 <>李在左边,点击信任中心李 <李>单击信任中心设置按钮李 <>李在左边,点击宏设置李 <李>单击“信任访问VBA项目对象模型”李

    转自:https://support.oracle.com/CSP/main/article?cmd=show&类型= NOT& id = 376013.1