Calling Excel from c++

Hi. I want to be able access Excel (read, write, and format data) from
within C++.

I have been doing it the other way around (calling c++ dlls from VBA)
and it's just not quite as powerful as i would like.

Any pointers to a good website or something on how to do this?
0
Hamish.Dean (174)
12/8/2008 6:20:18 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
652 Views

Similar Articles

[PageSpeed] 46

"hamishd" <Hamish.Dean@gmail.com> ha scritto nel messaggio 
news:2cb9adfb-d00c-4e1c-9ab1-c5a968762c68@d36g2000prf.googlegroups.com...
> Hi. I want to be able access Excel (read, write, and format data) from
> within C++.
>
> I have been doing it the other way around (calling c++ dlls from VBA)
> and it's just not quite as powerful as i would like.
>
> Any pointers to a good website or something on how to do this?

The key point is that Excel exports an object model based on COM. It easy 
very easy to "automate" Excel from high-level languages, like classic VB, or 
VB.NET (because these tools do the boilerplate COM code for you, hiding COM 
management complexity); instead, with C++, you have to directly interact 
with COM at a lower level to automate Excel.

However, it is not particularly hard, thanks to some Microsoft extensions to 
standard C++, like the #import directive (that is used to incorporate 
information from a type library, in particular in this case from Excel type 
library, i.e. kind of Excel "object model"), and other Microsoft-specific 
C++ classes to support COM, like _bstr_t, _variant_t, etc.

You may want to practice your <favourite-search-engine> skills, and find 
some results, as the following:

"Programming Excel COM Objects in C++"
http://www.ddj.com/cpp/184401222?pgno=3

"Accessing Excel Spreadsheets via C++"
http://www.codeproject.com/KB/wtl/WTLExcel.aspx

Giovanni


0
12/8/2008 8:57:00 AM
On Dec 8, 1:20=A0am, hamishd <Hamish.D...@gmail.com> wrote:
> Hi. I want to be able access Excel (read, write, and format data) from
> within C++.
>
> I have been doing it the other way around (calling c++ dlls from VBA)
> and it's just not quite as powerful as i would like.
>
> Any pointers to a good website or something on how to do this?

Excel, like other Office products, has a very rich object model. You
can use automation to control excel, including an already running
instance. The key to all this is knowing COM and using it in C++.
Compared to VB and .net, COM in C++ is very complicated and tedious
but can be done. ATL provides lot of helpful classes for COM which
would be very useful for a C++ COM client.

--
Ajay
0
ajaykalra (6841)
12/8/2008 4:36:02 PM
Here is a good start. You can do almost anything you want with Excel using 
automation.

http://support.microsoft.com/kb/178781

AliR.

"hamishd" <Hamish.Dean@gmail.com> wrote in message 
news:2cb9adfb-d00c-4e1c-9ab1-c5a968762c68@d36g2000prf.googlegroups.com...
> Hi. I want to be able access Excel (read, write, and format data) from
> within C++.
>
> I have been doing it the other way around (calling c++ dlls from VBA)
> and it's just not quite as powerful as i would like.
>
> Any pointers to a good website or something on how to do this? 


0
AliR3470 (3235)
12/9/2008 3:59:03 PM
On Dec 10, 4:59=A0am, "AliR \(VC++ MVP\)" <A...@online.nospam> wrote:
> Here is a good start. You can do almost anything you want with Excel usin=
g
> automation.
>
> http://support.microsoft.com/kb/178781

Thanks to everyone for their advice.
0
Hamish.Dean (174)
12/9/2008 9:34:28 PM
All Office components export automation interfaces.  

The Good News is that these are pretty well documented.

The Bad News is that the documentation is mindlessly hard to use, because it was designed
by people who were clueless about the design of useful documentation.  So I can say that
the interfaces are well-documented only if you have the patience to track them down.  This
means you bring up Excel, bring up the VBA macro editor, go to its Help, and start down a
tedious (but actually complete) path.

I found the people over in the microsoft.public.office.developer.automation to be
remarkably helpful in giving me the key insights into how documents are organized and what
automation methods you need to use.  Once you have that information, you can "translate"
it to the COM methods of the interface.

You can also see some of the code I wrote (including error detection and response) in a
program I wrote called the PowerPoint Indexer, which you can download free from my MVP
Tips site.  The core skeleton would be useful for interfacing to any office component, but
the details of the data structures I walk down are obviously PowerPoint rather than Excel
structures.  So you'll have to figure out how to find a file, a sheet, a cell, etc., but
once you have figured out the VBA method, you will be able to figure out the MFC classes
you have to import without too much more effort, and what methods to call in them. There's
a bit of a learning curve, but within a couple days you should be able to do something
useful (the hardest part for me was detecting and recovering from errors, if for no other
reason to figure out what I'd coded wrong and fix it, so you'll probably be able to reuse
some of my code for doing that.
					joe

On Sun, 7 Dec 2008 22:20:18 -0800 (PST), hamishd <Hamish.Dean@gmail.com> wrote:

>Hi. I want to be able access Excel (read, write, and format data) from
>within C++.
>
>I have been doing it the other way around (calling c++ dlls from VBA)
>and it's just not quite as powerful as i would like.
>
>Any pointers to a good website or something on how to do this?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
12/14/2008 8:58:38 AM
Reply:

Similar Artilces:

Excel 2003
Hi Guys: There is probably no way to beat this, but it's worth a try. I am downloading a list and if count this list with the following formula it gives me what I would expect. Column A = 285 names (contiguious) (Starts in row 2) Formula =COUNTA(A:A) gives me 285 Because this list includes a lot of repeats and I am interested in a count of unique names, I sort the list. Then in column B(B2) I insert the formula =IF(A1=A2,"",A2) and copy it down. This gives me a list with lots of holes where the repeat name occur. I use the formula: = COUNTA(B:B) and expected a number much les...

Call in?
Do new owners of Office X have to call up Microsoft to register their copy? If yes, what's the number? thanks. Ming <asdf@asdf.com> wrote: > Do new owners of Office X have to call up Microsoft to register their > copy? If yes, what's the number? thanks. Nope. We're not using Windoze :->>> This version does not require activation. Corentin -- - Mac:MS MVP (Francophone) - (MS) MVP: http://support.microsoft.com/default.aspx?ln=FR&scid=fh;FR;mvp Newsgroups produits MS: http://support.microsoft.com/newsgroups/?ln=FR ...

Excel: Can I extend the amount of characters in a text box?
I have several forms where I've merged several cells (say 3 rows of cells each). They're all formatted for text, but I appear to b limited to the number of characters regardless of the size of the box. Is that an Excel limit or is there a way to increase this amount? Thanks in advance for all your help. Chri -- Message posted from http://www.ExcelForum.com Hi the limit is 1024 characters. You can extend this limit with manual entering line breaks. Use ALT+ENTER to do this -- Regards Frank Kabel Frankfurt, Germany "Healingbear >" <<Healingbear.19t981@excelforu...

Covered Call Options
Hello all, I do I go about dealing with a covered call transaction in Microsoft Money 2007 Premium? Currently what I am doing is inputing the option symbol, and choosing the transaction "Sell to Open" then when I go to expire the option I try to do a Sell to Close. However it does not seem to work? Also what happens if instead of expired I has the position assigned? how would I account for that? Thank you. Shhhh In microsoft.public.money, Shhhh wrote: > >I do I go about dealing with a covered call transaction in Microsoft >Money 2007 Premium? Currently what I am doing is...

Formatting exponentials in Excel
I am trying to set up a spreadsheet that has a formula for the available space which is = total - used The thing is the space is GB or MB (1x10^9 or 1x10^6) I formatted the numbers to display 1000000 as 1.00E+06 but is there anyway of making this into 1MB so it will still calculate correctly? Hope that makes sense, Thanks, Niall. Custom format #,,\M\B OR #.#,,\M\B OR #.0,,\M\B OR 0.0,,\M\B -- HTH, RD ===================================================== Please keep all correspondence within the Group, so all may benefit! ===================================================== "N...

How do you do a strike out in an Excel cell?
How do you do a strike out in an Excel cell? Format | Cells... | Font | Effects = strikethrough Regards Trevor "dgalati" <dgalati@discussions.microsoft.com> wrote in message news:4364AE43-F016-45B3-A6ED-160A405B3291@microsoft.com... > How do you do a strike out in an Excel cell? ...

Call center tracking and reporting
Anyone use CRM 4.0 for call tracking? Our Sales/Marketing is set up. Now I need to decide how to most efficiently customize CRM 4.0 for our company's call tracking portion. Can someone send me a link to information? thanks ...

telephone calls
when placing a telephone call using outlook, everything works fine except I can't hear the person i am talking to. They can hear me find. When I use a telephone program not associated with outlook every thing works great. can someone help ...

Excel VBA Question
Ok, I am stuck trying to write a function to do the following, and it would be greatly appreciated if someone could help me. I have two columns, one of data, and another of dates. I need to write a function that filters by dates, and counts the total number of items For Example: My data (column 1) has Members, Non_Members, and Other. My Dates (Column 2) have, well, dates. I have a report sheet that I need to report the numbers. So in the report for each data type (Member, Non_Member, Other) I want to be able to use function (=blah(date I am filtering for less than or equal to)) an...

Closing Excel 2000 from VB (Unwanted process EXCEL.EXE)
I have an Access database - it uses the following code to open Excel Public Sub OpenXlSheet(strReportName As String) ' Check if excel is open, close it if it is If AppInUse("xlmain") <> 0 Then Dim objExcel As Excel.Application Set objExcel = GetObject(, "Excel.Application") objExcel.Quit Set objExcel = Nothing Do While AppInUse("xlmain") <> 0 Loop End If 'open a fresh excel application and load strReportName Set objExcel = CreateObject("Excel.Application") objExcel.Appl...

Insert spinning button with time format in Excel 2000?
I am making a form template in Excel 2000. Every time I try to enter a spinner button I can't get it to work with time format. I want the time to be between 00:00 and 24:00 with 15 minute increments. I've tried with and without the colons but nothing is working. I put a spinner from the Forms toolbar on a worksheet. I rightclicked on it and chose format control. On the control tab, I used: Minimum value: 0 maximum value: 96 Incremental change: 1 And cell link A1 Then in B1, I put: =a1/96 and formatted as time: hh:mm 96 is the number of 15 minute increments in a day (4*24...

very slow to open excel file
hello, my computer in office is using WIN 98 SE, i found that it's too slow to open an excel file recently. please help thanks a lot Hi Alex! Most probable causes are: Too much accumulated junk in: C:\Windows\Temp File Shut down your computer and re-start. Then delete all contents of the C:\Windows\Tem file Alternative might be a bloated Excel.xlb file. Locate the Excel.xlb or Excel10.xlb file using Start > Search > For Files and Folders. The xlb file should not be above around 30kb and is usually much smaller. If it is bloated, there's not a lot that can be done w...

Calling Macro
Hi All, I have macro like this sub unique() '''''''' '''''''' Counter_item = 0 For Each Item In nodups counter_item = counter_item + 1 bic_var = Item Call Ps_Match Next Item end sub I have another macro Sub Ps_Match() ''''' ''''' If counter_item <= 1 Then call another_macro() end if end sub In the first macro i am declaring counter_item equals to 1. In my second macro i need to call that number. At the to of the module put: Public Counter_I...

excel online
Hi, How can I put an excel xls on the webserver so that I can use IE to click and fill up the information and then click a button to send an email to a default sender? Please advise me the methodology. Thanks. ...

Excel construct single chart from 2 charts
Hello All, Anyone an idea how to construct such a chart in Excel? http://chart.yahoo.com/c/5y/p/pdli.gif I know you can build more then one chart into one chart sheet but what i really want to do is construct one chart or 2 chart objects assembled into 1 so they can be exported into 1 gif as per above looked at the charting samples at the John Peltier site but still got no idea how to construct this TIA Brian Brian - The way I've done this is by embedding two chart objects in a blank chart sheet. A little VBA to make sure the chart axes line up. Then the chart sheet is exported. -...

Workflow -Assembly Call
How does add subtract in the Workflow>> assembly call works. I do understand that ypu can write your own custom workflows, register it and then call them from here, but I am pretty confused with the existing functionality. workflows can call other workflows workflows can also call a method in a .net assembly. ie you could write a piece of code in c# or vb.net and then have the workflow call your code. ======================= John O'Donnell Microsoft CRM MVP http://codegallery.gotdotnet.com/crm "SAM" <SAM@discussions.microsoft.com> wrote in message news:DE...

Excel and Sorting
In Excel I have my checking information I would like Excel to take my information and sort it as to the catagory I have labled each transaction and put them into groups with totals. This is done it Quicken is this possible to do this in Excel? Thanks You got a response to your first version of this question. Justin wrote: > > In Excel I have my checking information I would like Excel to take my > information and sort it as to the catagory I have labled each transaction and > put them into groups with totals. This is done it Quicken is this possible to > do this in Excel?...

How do I create a calendar from data in excel?
I have two columns with dates in them and would like to display a calendar at the bottom of the page. Is this possible? Try a 3rd party application like WinCalendar... With this you can insert a Calendar into any excel sheet. You can also save daily appointments on a floating Calendar that later display on the inserted calendar. Refer to http://www.wincalendar.com/ for more info. TG On Jul 31, 12:00 pm, kare7272 <kare7...@discussions.microsoft.com> wrote: > I have two columns with dates in them and would like to display acalendarat > the bottom of the page. Is this possible...

Split Name Field in Excell
I have a single name field that has both the first and last name of some clients. How can I separate each name and create separate fields for each? Hi crystal the easiest place to start is by using data / text to columns ... insert a couple of spare columns to the right of the column you want to split select the column to split choose data / text to columns ensure delimited is selected - press next untick tab, tick space press finish let us know how you go Cheers JulieD "crystal" <anonymous@discussions.microsoft.com> wrote in message news:5cf901c474b7$77d4abc0$a...

Pasting HTML into excel
I have copy and pasted some HTML data (stock symbols) into excel but the VLOOKUP function doesn't seem to recognize these symbols even though the symbols appear to exist within the VLOOKUP search range. For example when searching for say IBM it does not find it. If I manually clear IBM and type IBM back into the same cell then VLOOKUP finds it. My guess is that there are some trailing non-visible HTML characters that VLOOKUP sees that are thwarting the match. Any ideas how I can delete these non-visible characters? I tried copy and paste special values, but it doesn't help. Th...

Exporting From US Excel and Importing in Euro Excel
I'm curious how this situation is handled. You have a worksheet that has some columns that represent U.S. monetary values that include decimals (e.g. 19.28). You export that to a .csv file (comma delimited file). You then have someone in Europe who wants to import this file, but they use commas as the separator in their monetary values. Is there a way they can import this, but also convert the decimals in the monetary values to commas? Thanks, Tim Not sure if it would work Tim but could you use Find/Replace on the range of "Values" changing "." to "," ...

Async calls to WebService in MFC
Hey guys, I use a webservice over the net which is scripted in VB.Net. This web service receives a username and a password and returns a string structure with multiples infos. If the string is empty, I had the wrong username and/or password. Pretty simple isn't? Ok now the problem is a bit more complex. I'm using an MFC client to connect to these web services, and since they're on the internet, there's a delay between sending the data and receiving an answer. I don't know really how I can tell me client to "wait for the string". The wrapper MFC created for the w...

Excel Oddity
Has anyone ever seen or know the solution for this Excel oddity? Thanks in advance. -When the user tries to navigate between cells, the function line at the top remains stuck on one value. However, if he selects the new cell with his mouse, then the value changes in the function line to the correct value. Tell the user to keep his/her sticky fingers off the Scroll Lock key! Bernard "sadsadsadaf" <jeffm@yahoo.com> wrote in message news:024301c352ce$b0f85070$a101280a@phx.gbl... > Has anyone ever seen or know the solution for this Excel > oddity? Thanks in advance. &...

Excel #40
Can any one help i want to count how many times a number appears in several locations on several spread sheets any ideas? Hi, you can try something like =COUNTIF(A:A,1)+COUNTIF(Sheet2!A:A,1) Change 1 for the number you are looking for if this helps please click yes, thanks "mart" wrote: > Can any one help i want to count how many times a number appears in several > locations on several spread sheets any ideas? ...

Borland C++ Builder
I use to use Borland C++ builder. What is the closest equivelent in the VC++.Net suite? I basically want drag-n-drop UI construction with automatic generation of code stubs for UI events callbacks Will using the resource editor with an MFC project get me close Thanks ...