Re: Excel automation to an already open XLS
Goran wrote:
On Feb 26, 9:41 pm, phil oakleaf <n...@oakleafsoftware.co.uk> wrote:
I am using Automation to push data across to Excel and this works fine
if the Book is closed - I just open it and use it
However, I also need to get hold of the book if it is already open.
Can anyone provide an example of how to do this given just the name of
the open XLS file.
Normally, you should simply use CoGetObject API for that. That works
in both situations, when file is already open and when not
(corresponding server is started by the system then). E.g.
IInterfaceYouNeed* pItf = NULL;
BIND_OPTS bo;
ZeroMemory(&bo,sizeof(bo));
bo.cbStruct = sizeof(bo);
if (FAILED(CoGetObject(L"YOUR_FILENAME_HERE", &bo,
IID_IInterfaceYouNeed, (void**)&punk)))
GodDamnDidntWork();
// workworkwork
pItf->Release();
Goran.
Goran thanks but most of that when about 2' over my head. However, I
tried this
HRESULT hr;
CComPtr < IUnknown > pUnk;
CComPtr < IDispatch > pDispatch;
// The variables we are using for the Invoke method
CComBSTR bstrMethodName(_T("Get"));
DISPID dispid = NULL;
// First, we try to obtain a pointer to the object's IUnknown interface.
// This will tell us if the object we are looking for exists.
if (FAILED(hr = ::CoGetObject(CT2W("excel.xls"), NULL, IID_IUnknown,
void**)&pUnk)))
{
TRACE("It didnt work");
}
any although Excel is running and excel.xls is open 'it didnt work'
I have never used CoGetObject before so be gentle.
Phil