EDN Admin
Well-known member
Dear all,
I have tried to write the COM model in VC++. I could successfully to open a blank new excel, however, I couldnt open my old excel file or even could not save the new blank excel file.
The code is as following:
<pre class="prettyprint #include <iostream>
#include <cmath>
#include <comutil.h>
// Office 2007 Objects
#import
"C
rogram Files (x86)Common Filesmicrosoft sharedOFFICE12mso.dll"
rename("DocumentProperties", "DocumentPropertiesXL")
rename("RGB", "RBGXL")
using namespace Office;
// VBA Objects
#import
"C
rogram Files (x86)Common Filesmicrosoft sharedVBAVBA6vbe6ext.olb"
using namespace VBIDE;
// Excel Application Objects
#import "C
rogram Files (x86)Microsoft OfficeOffice12EXCEL.exe"
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL")
rename("DocumentProperties", "DocumentPropertiesXL")
rename("ReplaceText", "ReplaceTextXL")
rename("CopyFile", "CopyFileXL")
exclude("IFont", "IPicture") no_dual_interfaces
using namespace std;
int main()
{
Excel::_ApplicationPtr XL;
CoInitialize(NULL);
// Start the Excel Application
// XL.CreateInstance(L"Excel.Application");
// Open the Excel Workbook, but dont make it visible
// pXL->Workbooks->Open( L"C:\dump\book.xls" );
XL->Visible = true;
/*
// If a communicatino error is thrown, catch it and complain
catch(_com_error &error)
{
cout << "COM error" << endl;
}
*/
// Access Excel Worksheet and return pointer to Worksheet cells
XL->Workbooks->Open("C:UsersWilsonDesktopBook1.xls");
Excel::_WorksheetPtr pWksheet = XL->ActiveSheet;
Excel:: RangePtr pRange = pWksheet->Cells;
// Read an Excel data cell. (Note Excel cells start from index = 1)
double value = pRange->Item[1][1];
// Write/modify Excel data cells + save. (reopen xls file to verify)
pRange->Item[1][1] = 5.4321;
pRange->Item[1][2] = 1.1211;
// pWksheet->SaveAs("C:a.xls");
// Exit the Excel Com object
XL->Quit();
// system("PAUSE");
return 0;
}
[/code]
<br/>
The purpose of doing this, is that I have designed a excel model with active x buttons already, which produce some table and curves. My main purpose is to write a windows form application to act as a switch to run the model and present the plot from the
excel file directly to the windows form application.
I know that this means I need to get access b/t excel and VC++, so is there other way that I could directly present the plot to c++ from my excel file?!
Thank you all, (My programming skills is not that good)
Best Wishes,
Wilson
View the full article
I have tried to write the COM model in VC++. I could successfully to open a blank new excel, however, I couldnt open my old excel file or even could not save the new blank excel file.
The code is as following:
<pre class="prettyprint #include <iostream>
#include <cmath>
#include <comutil.h>
// Office 2007 Objects
#import
"C

rename("DocumentProperties", "DocumentPropertiesXL")
rename("RGB", "RBGXL")
using namespace Office;
// VBA Objects
#import
"C

using namespace VBIDE;
// Excel Application Objects
#import "C

rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL")
rename("DocumentProperties", "DocumentPropertiesXL")
rename("ReplaceText", "ReplaceTextXL")
rename("CopyFile", "CopyFileXL")
exclude("IFont", "IPicture") no_dual_interfaces
using namespace std;
int main()
{
Excel::_ApplicationPtr XL;
CoInitialize(NULL);
// Start the Excel Application
// XL.CreateInstance(L"Excel.Application");
// Open the Excel Workbook, but dont make it visible
// pXL->Workbooks->Open( L"C:\dump\book.xls" );
XL->Visible = true;
/*
// If a communicatino error is thrown, catch it and complain
catch(_com_error &error)
{
cout << "COM error" << endl;
}
*/
// Access Excel Worksheet and return pointer to Worksheet cells
XL->Workbooks->Open("C:UsersWilsonDesktopBook1.xls");
Excel::_WorksheetPtr pWksheet = XL->ActiveSheet;
Excel:: RangePtr pRange = pWksheet->Cells;
// Read an Excel data cell. (Note Excel cells start from index = 1)
double value = pRange->Item[1][1];
// Write/modify Excel data cells + save. (reopen xls file to verify)
pRange->Item[1][1] = 5.4321;
pRange->Item[1][2] = 1.1211;
// pWksheet->SaveAs("C:a.xls");
// Exit the Excel Com object
XL->Quit();
// system("PAUSE");
return 0;
}
[/code]
<br/>
The purpose of doing this, is that I have designed a excel model with active x buttons already, which produce some table and curves. My main purpose is to write a windows form application to act as a switch to run the model and present the plot from the
excel file directly to the windows form application.
I know that this means I need to get access b/t excel and VC++, so is there other way that I could directly present the plot to c++ from my excel file?!
Thank you all, (My programming skills is not that good)
Best Wishes,
Wilson
View the full article