Click here to Skip to main content
15,851,514 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

n 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).

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".

What I have tried:

This is the code I'm using in the C++ MFC OLE automation project:

C++
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

C#
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?
Posted
Comments
Rick York 19-Feb-19 10:33am    
I don't have a solution for you but have you looked at the samples that came with VS2010? There is a section on OLE in the MFC directory that might have something useful. That's a good sample pack to have around regardless.
achimschoen 20-Feb-19 8:00am    
Thanks for the reply. I'm aware of the examples. Unfortunately, they don't answer my question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900