CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jun 2021
    Posts
    51

    How to get instance Excel2016?

    Hi,
    My computer is installing 2 versions of excel10 and excel16.
    I use the following code to get the running excel instance but it only recognizes excel10.
    Code:
        LPWSTR pszExcelPath = 
    	  L"C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE";
    
        CreateProcess(pszExcelPath, NULL, 0, 0, 1,
    	  NORMAL_PRIORITY_CLASS, 0, NULL, &Start, &ProcInfo);
    I don't know what is the purpose of
    Code:
    for (int j = 1; j <= 5; j++)
    Although when I run the code, I always get pDisp!=nullptr but I can't query to workbooks from m_XLApp,...
    Code:
    m_XLApp.Attach(static_cast<Interface*> pDisp,true );
    if (pUnk)
    	pUnk->Release();
    wstring s{ m_XLApp->Caption }; <<--vs crash then out debugging.
    How I can fix this error?

    Edited: There aren't multiple instances running at the same time, I just want to get at least one running instance.

    Many thanks!
    Last edited by Dang.D.Khanh; July 6th, 2021 at 03:53 AM.

  2. #2
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    hi, am i using it the wrong way? ( this code runs normally if i open excel2010 instead of excel2016.)
    I think this must work.

    Code:
    Excel::_ApplicationPtr app(pDisp);
    Excel::WorkbooksPtr ws = app->Workbooks;
    what i mean is app will get instance of openning excel application and then i can access it properties.
    however it always crash!
    Last edited by VictorN; July 6th, 2021 at 03:03 AM. Reason: QUOTE tags replaced with the CODE ones

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,118

    Re: How to get instance Excel2016?

    Quote Originally Posted by Dang.D.Khanh View Post
    I use the following code to get the running excel instance ...
    I don't know what is the purpose of
    Code:
    for (int j = 1; j <= 5; j++)
    It is very clear states in the comments to the code:
    Code:
    for(int i=1;i<=5;i++) //try attaching for up to 5 attempts
    {
       HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
       if(SUCCEEDED(hr)) 
       {
           hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
           break;
       }
       ::Sleep(1000);
    }
    Quote Originally Posted by Dang.D.Khanh View Post
    Although when I run the code, I always get pDisp!=nullptr but I can't query to workbooks from m_XLApp,...
    Code:
    m_XLApp.Attach(static_cast<Interface*> pDisp,true );
    if (pUnk)
    	pUnk->Release();
    wstring s{ m_XLApp->Caption }; <<--vs crash then out debugging.
    How I can fix this error?
    Please, define ""crash".
    Do you use try/catch blocks?
    Is the m_XLApp pointer valid?
    Victor Nijegorodov

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,118

    Re: How to get instance Excel2016?

    Quote Originally Posted by Dang.D.Khanh View Post
    ...
    what i mean is app will get instance of openning excel application and then i can access it properties.
    however it always crash!
    Please, define ""crash".
    Do you use try/catch blocks?
    Is the pDisp pointer valid? Is the app pointer valid?
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,580

    Re: How to get instance Excel2016?

    The path for excel10 and excel16 will be different. You need to find the installed paths for your system and use them as appropriate.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.5)

  6. #6
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    Hi Sir,

    Code:
    	HRESULT hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);
    		
    		if (SUCCEEDED(hr)) {
    		    Excel::_ApplicationPtr app(pDisp);
    		    Excel::_WorkbookPtr wb = app->ActiveWorkbook;  <<-- I get error at this code
    		    CString s = wstring{ wb->Name }.c_str();
    		    pUnk->Release();
    		    ...
    		}
    in excel.tli:
    Update Image:

    -----------------------------------------------------------------------------------_my app value

    Name:  app.png
Views: 254
Size:  8.3 KB

    -----------------------------------------------------------------------------------_result value

    Name:  result.png
Views: 263
Size:  10.3 KB

    -----------------------------------------------------------------------------------_then I get error

    Name:  Screenshodft_1.jpg
Views: 279
Size:  34.2 KB
    Last edited by Dang.D.Khanh; July 6th, 2021 at 05:01 AM.

  7. #7
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    Quote Originally Posted by 2kaud View Post
    The path for excel10 and excel16 will be different. You need to find the installed paths for your system and use them as appropriate.
    Hi Sir,
    Can you explain more clearly? let's say if I run on another computer(client) it will probably be difficult for me.
    Can I get the instance without specifying this?
    Last edited by Dang.D.Khanh; July 6th, 2021 at 03:30 AM.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,580

    Re: How to get instance Excel2016?

    The installed path for Excel can be obtained from the registry:

    HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\Excel.exe\path
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.5)

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,118

    Re: How to get instance Excel2016?

    Quote Originally Posted by Dang.D.Khanh View Post
    Hi Sir,

    HTML Code:
    	HRESULT hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);
    		
    		if (SUCCEEDED(hr)) {
    		    Excel::_ApplicationPtr app(pDisp);
    		    Excel::_WorkbookPtr wb = app->ActiveWorkbook;  <<-- I get error at this code
    		    CString s = wstring{ wb->Name }.c_str();
    		    pUnk->Release();
    		    ...
    		}
    in excel.tli:


    Attachment 35975
    1. Your attachment doesn't open.
    2. Please, use CODE tags rather than HTML ones.
    3. What is the value of app?
    Victor Nijegorodov

  10. #10
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    Quote Originally Posted by 2kaud View Post
    The installed path for Excel can be obtained from the registry:

    HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\Excel.exe\path
    Thanks sir,
    Now this is clearer to me.
    I remember that from this post I was able to access running excel entities and get the names of workbooks into a combobox (but it's in .Net).

    Is there another approach that is simpler or has been explored in c++? because translating it is still beyond my ability.

    Update: looks like I made a mistake. I tried to check again and it failed.
    Last edited by Dang.D.Khanh; July 6th, 2021 at 05:24 AM.

  11. #11
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    Quote Originally Posted by VictorN View Post
    1. Your attachment doesn't open.
    2. Please, use CODE tags rather than HTML ones.
    3. What is the value of app?
    Hi Sir, I have updated that comment.
    The error I get from this code:

    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));

    The code works on excel 2010, but I can't continue on excel2016.
    Last edited by Dang.D.Khanh; July 6th, 2021 at 05:06 AM.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,118

    Re: How to get instance Excel2016?

    Quote Originally Posted by Dang.D.Khanh View Post
    Hi Sir, I have updated that comment.
    The error I get from this code:

    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));

    The code works on excel 2010, but I can't continue on excel2016.
    You must catch the _com_error exception to obtain the reason of the problem!
    See example in this thread.
    Victor Nijegorodov

  13. #13
    Join Date
    Jun 2021
    Posts
    51

    Re: How to get instance Excel2016?

    Quote Originally Posted by VictorN View Post
    You must catch the _com_error exception to obtain the reason of the problem!
    Hi Sir,
    I followed your post and after run code, I get these error:

    Exception thrown at 0x00007FF8AB5A9689 (KernelBase.dll) in sap.exe: 0x80029C4A: Error loading type library/DLL.

    Name:  errror.jpg
Views: 256
Size:  8.7 KB


    Name:  comrer.jpg
Views: 256
Size:  11.5 KB


    Name:  comerr3.jpg
Views: 259
Size:  6.9 KB
    Last edited by Dang.D.Khanh; July 6th, 2021 at 08:26 AM.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,118

    Re: How to get instance Excel2016?

    You received the failure with hresult 0x80029c4a which means "Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)".
    Possible reasons and workarounds are here: https://www.google.com/search?q=hres...hrome&ie=UTF-8
    Victor Nijegorodov

  15. #15
    Join Date
    Jun 2021
    Posts
    51

    Smile Re: How to get instance Excel2016?

    Quote Originally Posted by VictorN View Post
    You received the failure with hresult 0x80029c4a which means "Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)".
    Possible reasons and workarounds are here: https://www.google.com/search?q=hres...hrome&ie=UTF-8
    Hi Sir,
    Maybe the problem occurs because I regularly clean my computer causing them to fail .
    After reinstall my offices ,now it works fine .
    This is exactly what I expected.
    Thanks you!
    Last edited by Dang.D.Khanh; July 6th, 2021 at 11:32 AM.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured