问题
Opening an Excel document by C++ MFC OLE automation shows a naming conflict ("_FilterDatabase") while the similar approach in C# .NET via Microsoft.Office.Interop.Excel does not.
In my C++ MFC OLE Automation project I have imported the type library for excel.exe and selected the interfaces _Applicatin (CExcelApplication), _Workbook(CWorkbook) and Workbooks(CWorkbooks).
In my C# .NET project I have added the COM reference "Microsoft Excel 16.0 Object Library".
This is the code I'm using in the C++ MFC OLE automation project:
void OpenExcelDocument(const std::wstring& strDocument)
{
// Create Excel object and bring Excel to the screen
CExcelApplication appExcel;
appExcel.CreateDispatch(L"Excel.Application");
appExcel.put_Visible(TRUE);
// obtain the Workbooks interface
LPDISPATCH pDispWorkbooks = appExcel.get_Workbooks();
if (pDispWorkbooks == NULL)
return;
CExcelWorkbooks workbooks;
workbooks.AttachDispatch(pDispWorkbooks);
// setup the default parameters
VARIANT varDefault;
memset(&varDefault, 0, sizeof(varDefault));
varDefault.vt = VT_ERROR;
varDefault.scode = DISP_E_PARAMNOTFOUND;
// open the document
workbooks.Open(strDocument.c_str(),
varDefault, varDefault, varDefault, varDefault, varDefault, varDefault, varDefault, varDefault,
varDefault, varDefault, varDefault, varDefault, varDefault, varDefault);
}
The following code shows the similar approach via C# .NET
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadExcelDoc
{
class Program
{
static void OpenExcelDocument(String strDocument)
{
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(strDocument);
}
static void Main(string[] args)
{
String xlsFile = @"...";
OpenExcelDocument(xlsFile);
}
}
}
My questions are:
- Is this really internally the same approach for the Excel COM objects?
- Is there a way to avoid Excel prompting to resolve the naming conflict in my C++ MFC OLE automation approach despite altering the Excel document manually?
- Are my VARIANT default arguments in C++ specified correctly?
来源:https://stackoverflow.com/questions/54751924/excel-automation-naming-conflict-filterdatabase-different-behavior-mfc-ole