Accessing an embedded Excel Sheet in Word from C++

A

Alex Müller

I access a word document with an embedded excel object from C++.
I need to query the excel object for the values of the cells, which I don't achieve.

I call on the Word object:
Selection.WholeStory
Selection.InlineShapes(1).OLEFormat.DoVerb

When I continue examining the object that I receive with Selection.InlineShapes(1).OLEFormat, I can call ProgID and am given "Excel.Sheet.8".
But I cannot call commands on that object that I would call on an Excel.Worksheet object, like "Range" for example (theres no DISPID for "Range").

Usually when I try finding out automation commands, I record a macro; but this time it is impossible, because the embedded object isn't enabled during macro recording.

So how can I access the cell values ?

The OLEFormat.Object has a Sheets collection which I can aquire. Sheets.Count yields 1.

But neither Sheets(0), Sheets(1), Sheets.Item(0), Sheets.Item(1) gives any object to me, I always reveive error -2147352573 / 0x80020003 (member not found).
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?QWxleCBNw7xsbGVy?=,
I access a word document with an embedded excel object from C++.
I need to query the excel object for the values of the cells, which I don't achieve.

I call on the Word object:
Selection.WholeStory
Selection.InlineShapes(1).OLEFormat.DoVerb

When I continue examining the object that I receive with
Selection.InlineShapes(1).OLEFormat, I can call ProgID and am given "Excel.Sheet.8".
But I cannot call commands on that object that I would call on an Excel.Worksheet
object, like "Range" for example (theres no DISPID for "Range").I can only tell you this in "VBA-speak", but I hope you'll be able to "translate" it:

Dim oWB As Excel.Workbook
Dim olef As Word.OLEFormat

Set olef = ActiveDocument.InlineShapes(1).OLEFormat
olef.DoVerb wdOLEVerbOpen
Set oWB = olef.Object
Debug.Print oWB.ActiveSheet.Name
oWB.Application.Quit
Set oWB = Nothing

1. The embedded object is an Excel Workbook

2. You have to first activate the OLEFormat, then set the Workbook object variable to the
OLEFormat's Object property

3. From here on, you can work with the Workbook exactly as you would in Excel

4. Note that I open the workbook in a separate window, rather than in-place. I do this
because I've never been able to find a reliable way to take the focus out of a workbook
activated in-place in Word. MS Graph, yes; Excel, I'm still looking :) What can work, if
it has time to cycle through, is a SendKeys for the ESC key, but this can interfere with
any processing that follows the automation of the embedded object. In addition, if you'd
be automating a series of objects in the same application, there are issues with COM
clearing itself out of memory properly before the next object is loaded. Loading into a
separate window usually avoids those kinds of problems, as well.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 
A

Alex Müller

Thank you very much for the reply, Cindy. But the problem is not solved; I will also write down in pseudo-VB-Code what I have at the moment:

Dim oWB As Excel.Workbook
Dim olef As Word.OLEFormat
Dim oRange As Excel.Range

Set olef = ActiveDocument.InlineShapes(1).OLEFormat
olef.DoVerb wdOLEVerbOpen
Set oWB = olef.Object
Debug.Print oWB.ActiveSheet.Name
-> Set oRange = oWB.Range("A1")

To be exact, all this is programmed in C++, but this way the code is more compact.

I receive the Sheet name properly, so the oWB seems kind of valid.

But when calling the Range command, I always receive an exception and the program blows up. The internal error that I can view on the exception's call stack is 0x80020003 / "member not found"

I also tried "oWB.Cells(...)" but with the same effect.

I experimented with calling "oWB.Activate" before that (which seems useless cause it is the ActiveSheet), but with no success. Calling "oWB.Select" before: also the same.

So I still can't access the cell values ....
 
A

Alex Müller

Sorry, instead of

-> Set oRange = oWB.Range("A1")

it is of course

-> Set oRange = oWB.ActiveSheet.Range("A1")
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?QWxleCBNw7xsbGVy?=,
I receive the Sheet name properly, so the oWB seems kind of valid.

But when calling the Range command, I always receive an exception and the program blows up. The internal error that I can view on the exception's call stack is 0x80020003 / "member not found"
When I try this

Set oRange = oWB.ActiveSheet.Range("A1")
oRange.Value = "Hi"

it works fine in the VBA environment.

If you automate an Excel workbook directly (instead of through Word), can you get the same code to work?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top