Prev: stcpy
Next: VS 2008
From: phil oakleaf on
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.


Any help will be much appreciated

Thanks

Phil


From: Joseph M. Newcomer on
Go look at my PowerPoint Indexer. I not only can interface to an active document, but I
set it up so that if I have to open the document, I close it when I'm done, but if it was
already open, I *don't* close it when I'm done. It would work the same for PowerPoint or
Excel or Word.

You can download the complete source from my MVP Tips page.

http://www.flounder.com/mvp_tips.htm#PowerPoint%20Indexing%20Tool
joe
On Fri, 26 Feb 2010 20:41:28 +0000, phil oakleaf <news(a)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.
>
>
>Any help will be much appreciated
>
>Thanks
>
>Phil
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: David Scambler on
Yes, this works the same in Excel. However, be aware that if the already
open Excel is showing a modal dialog or is in edit mode your connection
will fail.

Joseph M. Newcomer <newcomer(a)flounder.com> wrote in
news:69ogo5pdkohh2b0ss8vv3onjf4cocvcmvs(a)4ax.com:

> Go look at my PowerPoint Indexer. I not only can interface to an
> active document, but I set it up so that if I have to open the
> document, I close it when I'm done, but if it was already open, I
> *don't* close it when I'm done. It would work the same for PowerPoint
> or Excel or Word.
>
> You can download the complete source from my MVP Tips page.
>
> http://www.flounder.com/mvp_tips.htm#PowerPoint%20Indexing%20Tool
> joe
> On Fri, 26 Feb 2010 20:41:28 +0000, phil oakleaf
> <news(a)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.
>>
>>
>>Any help will be much appreciated
>>
>>Thanks
>>
>>Phil
>>
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm

From: Goran on
On Feb 26, 9:41 pm, phil oakleaf <n...(a)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.
From: phil oakleaf on
Goran wrote:
> On Feb 26, 9:41 pm, phil oakleaf <n...(a)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
 | 
Pages: 1
Prev: stcpy
Next: VS 2008