Excel automation naming conflict “_FilterDatabase” - different behavior MFC ole automation vs. C# .NET

夙愿已清 提交于 2021-01-28 14:08:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!