Click here to Skip to main content
15,887,083 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: void pointer? [modified] Pin
Joe Woodbury30-Jul-09 8:20
professionalJoe Woodbury30-Jul-09 8:20 
GeneralRe: void pointer? Pin
sam_psycho30-Jul-09 8:30
sam_psycho30-Jul-09 8:30 
GeneralRe: void pointer? Pin
Joe Woodbury30-Jul-09 8:35
professionalJoe Woodbury30-Jul-09 8:35 
GeneralRe: void pointer? Pin
sam_psycho30-Jul-09 9:01
sam_psycho30-Jul-09 9:01 
GeneralRe: void pointer? Pin
Joe Woodbury30-Jul-09 9:45
professionalJoe Woodbury30-Jul-09 9:45 
GeneralRe: void pointer? Pin
sam_psycho30-Jul-09 16:43
sam_psycho30-Jul-09 16:43 
GeneralRe: void pointer? Pin
Joe Woodbury30-Jul-09 17:04
professionalJoe Woodbury30-Jul-09 17:04 
QuestionC++ Excel Automation -- assign array to range Pin
MattPugsley30-Jul-09 5:45
MattPugsley30-Jul-09 5:45 
Hello,

I am trying to assign an array to a range in Excel. I am using Excel 2007, Visual C++, MS VC++2008 Express Edition on XP.

I have read a number of articles from MSDN and other places to get started, but I continue to get run-time errors.

The basic logic is:
1. Identify a range pointer (type RangePtr) defined for a specific range in an existing Excel spreadsheet.
2. Create a BSTR.
3. Put the BSTR in a one element SAFEARRAY (two dimensions, one row, one column).
4. Wrap the SAFEARRAY into a VARIANT.
5. Assign the VARIANT to the RangePtr.

The code compiles without errors, but I get run-time errors when using the last step. I get no run-time errors if I omit the last step (step #5).

The call stack that results when running the program:

 	kernel32.dll!7c812aeb() 	
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	kernel32.dll!7c812aeb() 	
 	kernel32.dll!7c809ac6() 	
 	InterfaceWithExcel.exe!_com_raise_error(HRESULT hr=-2147352568, IErrorInfo * perrinfo=0x00000000)  Line 19	C++
 	InterfaceWithExcel.exe!_com_issue_error(HRESULT hr=-2147352568)  Line 39 + 0xe bytes	C++
>	InterfaceWithExcel.exe!_com_util::CheckError(HRESULT hr=-2147352568)  Line 99	C++
 	InterfaceWithExcel.exe!_variant_t::_variant_t(const tagVARIANT & varSrc=safearray of BSTR = [1,1](0x001a4bf4 "C"))  Line 1136 + 0x1d bytes	C++
 	InterfaceWithExcel.exe!main()  Line 98 + 0x12 bytes	C++
 	InterfaceWithExcel.exe!__tmainCRTStartup()  Line 582 + 0x19 bytes	C
 	InterfaceWithExcel.exe!mainCRTStartup()  Line 399	C
 	kernel32.dll!7c817067() 	


Googling has told me that hr=-2147352568 means "bad variable type." But I don't know where the bad variable type is in the code below. You can see above how the code does see my one element SAFEARRAY with the BSTR value of "C". (This is bstr1, defined in the code below.)

I have been successfuly in assigning values to individual cells. So the code is not entirely on the wrong track. For example, myRangePtr->Item[1][1] = 1; will successfully assign the number 1 to cell A1. But I'm trying to dump an entire table, and so I would like to be able to assign arrays to ranges rather than cell values one at a time.

The code follows below.

Thank you for your time!
--Matt Pugsley

// code adapted from provided in "A brief introduction to C++ and Interfacing with Excel"
// by Andrew L. Hazel, School of Mathematics, University of Manchester

// Microsoft Office Objects
#import \
 "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSO.DLL" \
rename("DocumentProperties","DocumentPropertiesXL") \
rename("RGB", "RGBXL")
// I changed OFFICE11 to OFFICE12, since I am using Excel 2007

// Microsoft VBA Objects
#import \
 "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

// Excel Application Objects
#import "C:\Program Files\Microsoft Office\OFFICE12\EXCEL.EXE" \
rename("DialogBox", "DialogBoxXL") rename("RGB", "RGBXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText","ReplaceTextXL") \
rename("CopyFile","CopyFileXL") \
exclude("IFont","IPicture") no_dual_interfaces

#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>



int main()
{
	Excel::_ApplicationPtr XL;
	HRESULT hr;
	
	try
	{
		CoInitialize(NULL);
	
		XL.CreateInstance(L"Excel.Application");
		
		XL->Visible = true;
	}
	catch(_com_error &error)
	{
		cout << "COM error " << endl;
	}

	// create workbook & worksheet
	// identify range to which array will be assigned
	XL->Workbooks->Add(Excel::xlWorksheet);
	Excel::_WorksheetPtr pSheet = XL->ActiveSheet;
	Excel::RangePtr pRange = pSheet->Cells;
	Excel::RangePtr pBeginRange = pRange->Item[1][1];
	Excel::RangePtr pEndRange = pRange->Item[1][1];
	Excel::RangePtr pTotalRange = pSheet->Range[(Excel::Range*)pBeginRange][(Excel::Range*)pEndRange];
	
	
	// create some BSTRs
	wchar_t wsz1[] = L"C";
	wchar_t wsz2[] = L"D";
	BSTR bstr1;
	bstr1 = ::SysAllocString(wsz1);
	BSTR bstr2;
	bstr2 = ::SysAllocString(wsz2);
	
	// create safearray
	SAFEARRAYBOUND rgsabound[2] = { 0 };
	rgsabound[0].cElements = 1; // num rows
	rgsabound[0].lLbound = 0; // lower bound
	rgsabound[1].cElements = 1; // num cols
	rgsabound[1].lLbound = 0; // lower bound
	SAFEARRAY* mySafeArrayPtr = SafeArrayCreate(VT_BSTR,2,rgsabound);

	// identify index we want to change
	long index[2];
	
	// modify first element <0,0>
	index[0]	= 0;
	index[1] = 0;
	hr = SafeArrayPutElement(mySafeArrayPtr,index,bstr1);
	
	// modify second element <0,1>
	// index[0] = 0;
	// index[1] = 1;
	// hr = SafeArrayPutElement(mySafeArrayPtr,index,bstr2);
	
	// wrap safearray in variant
	VARIANT var;
	VariantInit(&var);
	var.vt = VT_ARRAY ;
	var.parray = mySafeArrayPtr;
	
	// assign safearray to range
	pTotalRange->Value = var; // this causes error
	
	// cleanup
	SysFreeString(bstr1);
	SysFreeString(bstr2);
	hr = SafeArrayDestroy(mySafeArrayPtr);
	
	return 0;
}

GeneralRe: C++ Excel Automation -- assign array to range -- mostly working now Pin
MattPugsley30-Jul-09 10:12
MattPugsley30-Jul-09 10:12 
GeneralRe: C++ Excel Automation -- assign array to range -- mostly working now Pin
Le@rner31-Aug-10 23:32
Le@rner31-Aug-10 23:32 
QuestionCan we change pointer type in the run time? Pin
THAQCD30-Jul-09 3:45
THAQCD30-Jul-09 3:45 
AnswerRe: Can we change pointer type in the run time? Pin
Code-o-mat30-Jul-09 4:04
Code-o-mat30-Jul-09 4:04 
GeneralRe: Can we change pointer type in the run time? Pin
THAQCD30-Jul-09 8:21
THAQCD30-Jul-09 8:21 
GeneralRe: Can we change pointer type in the run time? Pin
Code-o-mat30-Jul-09 8:28
Code-o-mat30-Jul-09 8:28 
AnswerRe: Can we change pointer type in the run time? Pin
MattPugsley30-Jul-09 7:51
MattPugsley30-Jul-09 7:51 
GeneralRe: Can we change pointer type in the run time? Pin
THAQCD30-Jul-09 8:32
THAQCD30-Jul-09 8:32 
QuestionHow to avoid a Global Variable Pin
Madhu_Rani30-Jul-09 3:36
Madhu_Rani30-Jul-09 3:36 
AnswerRe: How to avoid a Global Variable Pin
Chris Losinger30-Jul-09 5:12
professionalChris Losinger30-Jul-09 5:12 
AnswerRe: How to avoid a Global Variable Pin
Rick York30-Jul-09 6:07
mveRick York30-Jul-09 6:07 
GeneralRe: How to avoid a Global Variable Pin
Madhu_Rani30-Jul-09 20:04
Madhu_Rani30-Jul-09 20:04 
GeneralRe: How to avoid a Global Variable Pin
Madhu_Rani30-Jul-09 20:36
Madhu_Rani30-Jul-09 20:36 
Questionfwrite for unicode characters... Pin
Rakesh530-Jul-09 1:57
Rakesh530-Jul-09 1:57 
QuestionRe: fwrite for unicode characters... Pin
Rajesh R Subramanian30-Jul-09 2:35
professionalRajesh R Subramanian30-Jul-09 2:35 
QuestionCMFCPropertyGridCtrl column width PinPopular
eight30-Jul-09 1:32
eight30-Jul-09 1:32 
QuestionRead and Write to Microsoft Excel which support .xlsx format. Pin
Le@rner29-Jul-09 21:41
Le@rner29-Jul-09 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.