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

C / C++ / MFC

 
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 
GeneralRe: C++ Excel Automation -- assign array to range -- mostly working now Pin
MattPugsley30-Jul-09 10:12
MattPugsley30-Jul-09 10:12 
I've gotten the code to assign an array to an Excel range, as desired, but now I'm getting errors regarding the native COM capabilities built into VC++2008. They regard releasing some interface pointers. I expect it's because I've pulled my code from many different sources: some using MFC, some not; some using #import directives, some using explicit references to IDispatch.

I'll just keep grinding on it.

Thanks,
Matt

Here's the current code in case anyone else is working on a similar project and wants an example that *almost* works.

// code for initially connecting to Excel
// adapted from "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
	
	VARIANT arr;
	arr.vt = VT_ARRAY | VT_BSTR;
	arr.parray = 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(arr.parray,index,bstr1);
	
	// modify second element <0,1>
	// index[0] = 0;
	// index[1] = 1;
	// hr = SafeArrayPutElement(mySafeArrayPtr,index,bstr2);
	
	// assign safearray to range
	pTotalRange->PutValue2(&arr);
		
	// cleanup
	VariantClear(&arr);
	SysFreeString(bstr1);
	SysFreeString(bstr2);
	CoUninitialize();
	
	
	return 0;
}

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 
AnswerRe: Read and Write to Microsoft Excel which support .xlsx format. Pin
Adam Roderick J29-Jul-09 22:53
Adam Roderick J29-Jul-09 22:53 

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.