• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

OLE access violation with Excel

Hi

I have an application in which I automate excel, its been a while since I
looked at it and I am having the problem that when the app is in debug
mode an access violation is thrown when I reach the following line

XL::_Application xlApp;

if(!xlApp.CreateDispatch("Excel.Application"))                               /// Access violation is here
      AfxMessageBox("Unable to create dispatch for Excel");


In release mode every thing is working fine, does anyone have any ideas
what might be happening here?

Thanks in advance

LittlePerson
0
LittlePerson
Asked:
LittlePerson
  • 4
  • 3
  • 2
1 Solution
 
AndyAinscowCommented:
Is the following of use?

      CLSID clsid;

      ::CLSIDFromProgID(L"Excel.Application", &clsid); // from registry


            if(!m_pExcelApp->CreateDispatch(clsid))
            {
                  TRACE("Attatching after create\n");
                  if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
                  {
                        TRACE("attaching after create - with the prog\n");
                        VERIFY(pUnk->QueryInterface(IID_IDispatch, (void**) &pDisp) == S_OK);
                        m_pExcelApp->AttachDispatch(pDisp, false);      //I want to keep Excel running so I don't auto-detatch
                        pUnk->Release();
                  }
0
 
AndyAinscowCommented:
You have had a few questions recently with things not working in debug mode but working in release mode.  Maybe you ought to consider de-installing, rebooting and installing VC++.
0
 
LittlePersonAuthor Commented:
That had crossed my mind, thanks.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
AndyAinscowCommented:
Forgot the following for the code snippet
      LPDISPATCH pDisp;
      LPUNKNOWN pUnk;
0
 
Duy PhamFreelance IT ConsultantCommented:
Try this :

CreateDispatch("ExcelApplication", NULL);
0
 
LittlePersonAuthor Commented:
Try this :

CreateDispatch("ExcelApplication", NULL);

Did not work
0
 
AndyAinscowCommented:
Have you tried it via the clsid yet?
0
 
LittlePersonAuthor Commented:
No, sorry I haven;t yet. I'll try soon. I've been tracking down the installation disks!

Thanks
0
 
Duy PhamFreelance IT ConsultantCommented:
Did you try to call CoInitialize(NULL) for enabling COM component in your app ?
If you did, could you paste your code up for us to see it clearly ?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now