Excel Automation Using C++ #import
-
Hi All, Very new to Excel Automation but have done a lot with Word Automation. Using VC++ V6 and Excel 2000. I simply need to get a range object that corresponds to the current user selection. This is what I thought would work: Excel::IRangePtr spRange = msp_excel->GetSelection(); There is an active sheet and a selection of cells but spRange (the smart pointer) is always returned as null??? I am also puzzled by the fact that there doesn't seem to be a Selection object, per se, something that could be returned and worked with via ISelectionPtr or the like??? Any help greatly appreciated! Thanks, Doug
Doug Knudson
-
Hi All, Very new to Excel Automation but have done a lot with Word Automation. Using VC++ V6 and Excel 2000. I simply need to get a range object that corresponds to the current user selection. This is what I thought would work: Excel::IRangePtr spRange = msp_excel->GetSelection(); There is an active sheet and a selection of cells but spRange (the smart pointer) is always returned as null??? I am also puzzled by the fact that there doesn't seem to be a Selection object, per se, something that could be returned and worked with via ISelectionPtr or the like??? Any help greatly appreciated! Thanks, Doug
Doug Knudson
Make sure you have an interface pointer to the sheet you think you're using. Try and do something else such as selecting "A1" and write something to it. The application object contains a collection of workbooks that contains a collection of worksheets and so on. You need to get the correct sheet. Is msp_excel really an interface opinter to a worksheet? The name that implies a opinter to the application object makes me wonder...;) It's been a while since I automated Excel, but I remember a lot of calls to functions such as GetActiveWorkbook(), GetActiveSheet(). A good thing is to explicitly make the application visible to the user; you might have a second instance of Excel running. -- Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998
"...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above -
Make sure you have an interface pointer to the sheet you think you're using. Try and do something else such as selecting "A1" and write something to it. The application object contains a collection of workbooks that contains a collection of worksheets and so on. You need to get the correct sheet. Is msp_excel really an interface opinter to a worksheet? The name that implies a opinter to the application object makes me wonder...;) It's been a while since I automated Excel, but I remember a lot of calls to functions such as GetActiveWorkbook(), GetActiveSheet(). A good thing is to explicitly make the application visible to the user; you might have a second instance of Excel running. -- Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998
"...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying aboveHi Roger, Thanks very much for the response. In answer to your questions... msp_excel is a pointer to the "Application Object" defined as _ApplicationPtr and yes, though strange and at least in the wrapper files (.tlh and .tli) generated by the #import, the Selection is associated with the Application Object, not the sheet. Another strange thing to me is that a Selection does not seem to be an object at all when using #import (no ISelectionPtr or equivalent) whereas in in the documentation (99.99% for VB, as I'm sure you know :)) it appears to be an object with VB syntax like: appObject.Selection.Font.Style = "bold" I would expect to be able to use something like the following to get a range that corresponded to the selection: msp_excel->GetSelection()->GetRange(); Since it seems that you need a range for nearly everything that you do in Excel, getting a range for the current selection seems like such a common and trivial thing to do, I can't help but feel I am really missing something fundamental here... :-( Thanks, Doug
Doug Knudson
-
Hi Roger, Thanks very much for the response. In answer to your questions... msp_excel is a pointer to the "Application Object" defined as _ApplicationPtr and yes, though strange and at least in the wrapper files (.tlh and .tli) generated by the #import, the Selection is associated with the Application Object, not the sheet. Another strange thing to me is that a Selection does not seem to be an object at all when using #import (no ISelectionPtr or equivalent) whereas in in the documentation (99.99% for VB, as I'm sure you know :)) it appears to be an object with VB syntax like: appObject.Selection.Font.Style = "bold" I would expect to be able to use something like the following to get a range that corresponded to the selection: msp_excel->GetSelection()->GetRange(); Since it seems that you need a range for nearly everything that you do in Excel, getting a range for the current selection seems like such a common and trivial thing to do, I can't help but feel I am really missing something fundamental here... :-( Thanks, Doug
Doug Knudson
I suggest you read some about how to automate Excel. Have a look in the references section in this article[^] and see if you can find anything useful for you. I don't think the article itself is of any use for you, but the links may very well be. Hope this helps -- Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998
"...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above