R
rachit.goyal
I am doing excel automation using C++ without MFC. I just want to open
Workbook->Worksheet and want to save that in CSV format using SaveAs
function. I got how to load excel application and open workbook and
worksheet, but I am not able to save it in CSc format. I am struggling
with VARIANT array values.
I am following one of the word automation example available in MSDN.
But MS word VARIANT array is not working in case of excel. Please
guide me.
This is code, I am using -
//Set up the DISPPARAMS for the SaveAs method (11 arguments)
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
BSTR bstrEmptyString;
bstrEmptyString = ::SysAllocString(OLESTR(""));
VARIANT vFalse;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
BSTR bstrTemp = ::SysAllocString(OLESTR("c:\\Excelexp.csv"));
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrTemp; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 6; //CSV FileFormat
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
//Invoke the SaveAs method
OLECHAR FAR* szFunction;
DISPID dispid_SaveAs;
szFunction = OLESTR("SaveAs");
hr = pWorkbook->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_SaveAs);
hr = pWorkbook->Invoke(dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpSaveAs, NULL, NULL, NULL);
Workbook->Worksheet and want to save that in CSV format using SaveAs
function. I got how to load excel application and open workbook and
worksheet, but I am not able to save it in CSc format. I am struggling
with VARIANT array values.
I am following one of the word automation example available in MSDN.
But MS word VARIANT array is not working in case of excel. Please
guide me.
This is code, I am using -
//Set up the DISPPARAMS for the SaveAs method (11 arguments)
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
BSTR bstrEmptyString;
bstrEmptyString = ::SysAllocString(OLESTR(""));
VARIANT vFalse;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
BSTR bstrTemp = ::SysAllocString(OLESTR("c:\\Excelexp.csv"));
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrTemp; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 6; //CSV FileFormat
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
//Invoke the SaveAs method
OLECHAR FAR* szFunction;
DISPID dispid_SaveAs;
szFunction = OLESTR("SaveAs");
hr = pWorkbook->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_SaveAs);
hr = pWorkbook->Invoke(dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpSaveAs, NULL, NULL, NULL);