-
July 5th, 2021, 11:36 PM
#1
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.
-
July 6th, 2021, 01:39 AM
#2
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
-
July 6th, 2021, 03:00 AM
#3
Re: How to get instance Excel2016?
 Originally Posted by Dang.D.Khanh
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);
}
 Originally Posted by Dang.D.Khanh
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
-
July 6th, 2021, 03:02 AM
#4
Re: How to get instance Excel2016?
 Originally Posted by Dang.D.Khanh
...
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
-
July 6th, 2021, 03:11 AM
#5
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
July 6th, 2021, 03:25 AM
#6
Re: How to get instance Excel2016?
Last edited by Dang.D.Khanh; July 6th, 2021 at 05:01 AM.
-
July 6th, 2021, 03:27 AM
#7
Re: How to get instance Excel2016?
 Originally Posted by 2kaud
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.
-
July 6th, 2021, 04:22 AM
#8
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
July 6th, 2021, 04:39 AM
#9
Re: How to get instance Excel2016?
 Originally Posted by Dang.D.Khanh
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
-
July 6th, 2021, 04:48 AM
#10
Re: How to get instance Excel2016?
 Originally Posted by 2kaud
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.
-
July 6th, 2021, 05:00 AM
#11
Re: How to get instance Excel2016?
 Originally Posted by VictorN
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.
-
July 6th, 2021, 05:47 AM
#12
Re: How to get instance Excel2016?
 Originally Posted by Dang.D.Khanh
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
-
July 6th, 2021, 08:07 AM
#13
Re: How to get instance Excel2016?
Last edited by Dang.D.Khanh; July 6th, 2021 at 08:26 AM.
-
July 6th, 2021, 08:29 AM
#14
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
-
July 6th, 2021, 10:13 AM
#15
Re: How to get instance Excel2016?
 Originally Posted by VictorN
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|