Hello,
I am trying to write on excel 2010 cells.
Here is my code:
// excel automation console app.cpp : main project file. #include "stdafx.h" #include "stdafx.h" #import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath") #import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB" no_implementation #import "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" rename("DialogBox", "ExclDialogBox") rename("RGB", "ExclRGB") rename("CopyFile", "ExclCopyFile") rename("ReplaceText", "ExclReplaceText") #include <stdexcept> #include <iostream> #include <windows.h> #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <vcclr.h> using namespace std; using namespace Excel; using namespace Office; using namespace VBIDE; int main() { HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hr)) { // cout << "Failed to initialize COM library. Error code = 0x" // << hex << hr << endl; //return hr; } // Create Excel Application Object pointer Excel::_ApplicationPtr pXL; if ( FAILED( pXL.CreateInstance( "Excel.Application" ) ) ) { // cout << "Failed to initialize Excel::_Application!" << endl; //return 0; } ///////////////////////////////////////////////////////////////////////////// string path = "C:\\Users\\test"; string searchPattern = "*.xls"; string fullSearchPath = path + searchPattern; WIN32_FIND_DATA FindData; HANDLE hFind; hFind = FindFirstFile( fullSearchPath.c_str(), &FindData ); if( hFind == INVALID_HANDLE_VALUE ) { //cout << "Error searching directory\n"; // return -1; } do { string filePath = path + FindData.cFileName; pXL->Visible[0] = true; pXL->Visible[1] = true; // Open the Excel Workbook, but don't make it visible pXL->Workbooks->Open( filePath.c_str() ); // Access Excel Worksheet and return pointer to Worksheet cells Excel::_WorksheetPtr pWksheet = pXL->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[6][3] = "TEST"; pWksheet->SaveAs(filePath.c_str()); //ifstream in( filePath.c_str() ); //if( in ) //{ // do stuff with the file here //} //else { // cout << "Problem opening file " << FindData.cFileName << "\n"; } } while( FindNextFile(hFind, &FindData) > 0 ); if( GetLastError() != ERROR_NO_MORE_FILES ) { // cout << "Something went wrong during searching\n"; } pXL->Quit(); ///////////////////////////////////////////////////////////////////////////// return 0; }
Every time my program want to open excel it crash and give me this error:
HERE : pXL->Workbooks->Open( filePath.c_str() );
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception. at _com_issue_errorex(Int32 , IUnknown* , _GUID* ) at Excel.Workbooks.Open(Workbooks* , _com_ptr_t<_com_IIID<Excel::_Workbook\,\&_GUID_000208da_0000_0000_c000_000000000046> >* , _bstr_t* Filename, _variant_t* UpdateLinks, _variant_t* ReadOnly, _variant_t* Format, _variant_t* Password, _variant_t* WriteResPassword, _variant_t* IgnoreReadOnlyRecommended, _variant_t* Origin, _variant_t* Delimiter, _variant_t* Editable, _variant_t* Notify, _variant_t* Converter, _variant_t* AddToMru, _variant_t* Local, _variant_t* CorruptLoad, Int32 lcid)
The program seems to work great on Windows XP, but i did not find any solution on the internet from now for windows 7.
Tried to add on : C:\Windows\SysWOW64\config , a folder call Desktop, some people solved this problem doing that, but nothing for me.
Also tried to put all file attributes to normal in the folder before opening, still northing.
Is there a Excel protection , blocking the writing from an oher program?