CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Automate Excel

  1. #1
    Join Date
    Apr 2017
    Posts
    6

    Automate Excel

    Hello. I new am these forum. Please help provide if can.

    I want automate Excel from c++. I confidant am with c++. I have example code open Excel spreadsheet read/write cells. This work good. I find not documentation for spreadsheet functions available. Most for c#, VB not c++. Where is com model Excel c++ documentation please.

    Is com best Excel automate - other ways better?

    Thank you for help.

  2. #2
    Join Date
    Jan 2009
    Posts
    399

    Re: Automate Excel


  3. #3
    Join Date
    Apr 2017
    Posts
    6

    Re: Automate Excel

    Thank for the reply. That is MFC that I not use. I use ordinary c++. It not code I try to find but documentation Excel com model c++. If have pointer to worksheet I get, what methods available can be used please? How set range for use? For Windows API Microsoft has documentation internet describe functions for c++. Where similar Excel com methods c++ documented please.

  4. #4
    Join Date
    Jan 2009
    Posts
    399

    Re: Automate Excel


  5. #5
    Join Date
    Apr 2017
    Posts
    6

    Re: Automate Excel

    Thank for reply. Yes I aware of that. But same problem. Where documentation is Excel model? Examples used ActiveSheet, Add, Range, Value, Saved. Where documentation these and all available c++ please.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automate Excel

    https://powerspreadsheets.com/excel-vba-object-model/
    https://www.google.de/search?q=com+m...m+object+model

    BTW, why don't you want to use MFC?
    It is much easier to automate excel using MFC!
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2017
    Posts
    6

    Re: Automate Excel

    Thank for reply. I know not MFC as taught c++ only. I know not VBA why want documentation for c++.

    If pointer to worksheet how insert row after required row? Where documented for c++ - not VBA. How to change cell colour How to set bold. Where is object model documentation for c++ - not VBA.

    Thank you.
    Last edited by auto2000; April 18th, 2017 at 02:04 PM. Reason: Spell

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automate Excel

    Quote Originally Posted by auto2000
    Where is object model documentation for c++ - not VBA.
    I guess there is no any "c++" object model documentation, only VBA one.
    However there is a lot of c++ wrapper classes to use this object model using MFC. I did it about a decade back. And I guess it still does work.
    Last edited by 2kaud; April 19th, 2017 at 04:31 AM.
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2017
    Posts
    6

    Re: Automate Excel

    This my test code
    Code:
    #include "imports.h"
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace Excel;
    using namespace std;
    
    const string sname = "c:\\My files\\admin\\test.xls";
    
    int main()
    {
    	HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    	if (FAILED(hr))
    	{
    		cout << "Failed to initialize COM library. Error code = 0x" << hex << hr << endl;
    		return 1;
    	}
    
    	_ApplicationPtr pXL;
    
    	if (FAILED(pXL.CreateInstance("Excel.Application")))
    	{
    		cout << "Failed to initialize Excel::_Application!" << endl;
    		return 2;
    	}
    
    	_WorkbookPtr pBook;
    
    	try {
    		pBook = pXL->Workbooks->Open(sname.c_str());
    	}
    	catch (...) {
    		cout << "Cannot open file" << endl;
    		pXL->Quit();
    		return 0;
    	}
    
    	pXL->PutVisible(0, FALSE);
    
    	_WorksheetPtr pWksheet = pXL->ActiveSheet;
    	//_WorksheetPtr pSheet = pBook->Sheets->Item[1];
    
    	RangePtr pRange = pWksheet->Cells;
    
    	for (int c = 5; (double)pRange->Item[c][3] != 0; ++c)
    		if ((double) pRange->Item[c][4] != 0)
    			cout << setprecision(0) << (double)pRange->Item[c][3] << " " << fixed << setprecision(2) << (double)pRange->Item[c][4] << endl;
    
    	/*
    	//pWksheet->Name = L"Sheet1";
    
    	// Read an Excel data cell. (Note Excel cells start from index = 1)
    	double value1 = pRange->Item[1][1];
    
    	cout << "Value in CELL [1][1] = " << value1 << endl;
    
    	double value2 = pRange->Item[1][2];
    	cout << "Value in CELL [1][2] = " << value2 << endl;
    
    	// Write/modify Excel data cells + save. (reopen xls file to verify)
    	cout << endl << "Modifying Excel cell values..." << endl << endl;
    
    	pRange->Item[1][1] = 10;
    	pRange->Item[1][2] = 11;
    
    	// Output new value
    	value1 = pRange->Item[1][1];
    	cout << "New value in CELL [1][1] = " << value1 << endl;
    
    	value2 = pRange->Item[1][2];
    	cout << "New value in CELL [1][2] = " << value2 << endl;
    
    	// Switch off alert prompting to save as 
    	pXL->PutDisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_FALSE);
    
    	// Save the values in book.xml and release resources
    	pWksheet->SaveAs(sname.c_str());
    
    	// And switch back on again...
    	pXL->PutDisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_TRUE);
    	*/
    
    	pBook->Release();
    	pWksheet->Release();
    	pXL->Quit();
    }
    For pointer pWksheet what methods available - what are params? and for pRange? How to insert row? How to format a cell - range of cell?

    Please help.
    Last edited by auto2000; April 19th, 2017 at 02:53 AM. Reason: Spell

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

  11. #11
    Join Date
    Apr 2017
    Posts
    6

    Re: Automate Excel

    Thank. That was looking to find.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured