How to auto save Excel file

HI, i am developing a program where i have to auto save the excel file. i try putting some integer data's in excel and auto save the file it works perfect. while i try to auto save the file with some string value in it. it shows me error.
AutoWrap()-IDispatch::GetIDsOfNames("Saved")failed w/err 0x80020006 - this is the error what i am getting right now. please help me to auto save the excel file while it have string value in it.
i have to auto save the excel file.
Which Excel file?

Your question isn't clear at all. I don't know how your program is related to Excel or how either is used. Can you please describe your setup in more detail please.

Thanks.
Hi, string code
//Save the work book.
{
VARIANT fname;
fname.vt = VT_BSTR;
fname.bstrVal =::SysAllocString(L"D:\\output3.xls");
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlSheet, L"Saved", 1, fname);
}

if i run this program i am getting error message. AutoWrap()-IDispatch::GetIDsOfNames("Saved")failed w/err 0x80020006.
actually i m trying to put some strings in excel and saving the file in particular drive. i try with putting integer value in excel and save it. it works perfect

integer code
//Save the work book.
{
VARIANT x;
x.vt = VT_I4;
x.bstrVal = ::SysAllocString(L"D:\\output.xls");
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlBook, L"Saved", 1, x);
}
this code works perfect but the string code is not working. plz help
You shouldn't be using IDispatch with C++. Have you looked at any ATL examples?
//this is my full code

#undef _UNICODE
#undef UNICODE
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string.h>


// AutoWrap() - Automation helper function...
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) {
// Begin variable-argument list...
va_list marker;
va_start(marker, cArgs);

if(!pDisp) {
MessageBox(NULL, "NULL IDispatch passed to AutoWrap()", "Error", 0x10010);
_exit(0);
}

// Variables used...
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
char buf[200];
char szName[200];


// Convert down to ANSI
WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);

// Get DISPID for name passed...
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx", szName, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}

// Allocate memory for arguments...
VARIANT *pArgs = new VARIANT[cArgs+1];
// Extract arguments...
for(int i=0; i<cArgs; i++) {
pArgs[i] = va_arg(marker, VARIANT);
}

// Build DISPPARAMS
dp.cArgs = cArgs;
dp.rgvarg = pArgs;

// Handle special-case for property-puts!
if(autoType & DISPATCH_PROPERTYPUT) {
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}

// Make the call!
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL);
if(FAILED(hr)) {
sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szName, dispID, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}
// End variable-argument section...
va_end(marker);

delete [] pArgs;

return hr;
}

int main(void)
{
// COM object is being intialized -- Haroon
// Initialize COM for this thread...
CoInitialize(NULL);

//get class ID of Excel -- Haroon
// Get CLSID for our server...
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);

if(FAILED(hr)) {

::MessageBox(NULL, "CLSIDFromProgID() failed", "Error", 0x10010);
return -1;
}

// get IDispatch -- Haroon
// Start server and get IDispatch...
IDispatch *pXlApp;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pXlApp);
if(FAILED(hr)) {
::MessageBox(NULL, "Excel not registered properly", "Error", 0x10010);
return -2;
}


// Excel visible -- Haroon
// Make it visible (i.e. app.visible = 1)
{

VARIANT x;
x.vt = VT_I4;
x.lVal = 1;
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlApp, L"Visible", 1, x);
}

// workbook collection
// Get Workbooks collection
IDispatch *pXlBooks;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"Workbooks", 0);
pXlBooks = result.pdispVal;
}

// new workbook create -- Haroon
// Call Workbooks.Add() to get a new workbook...
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlBooks, L"Add", 0);
pXlBook = result.pdispVal;
}

// creating array -- Haroon
// Create a safearray of variants...
VARIANT arr;
arr.vt = VT_ARRAY | VT_VARIANT;
{
SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = 10;
sab[1].lLbound = 1; sab[1].cElements = 1;
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
}

for(int j=1; j<=3; j++)
{
BSTR b;
VARIANT parm1;
b = SysAllocString(L"Enter the Command Values:");
parm1.vt = VT_BSTR;
parm1.bstrVal = b;
// Add to safearray...
long indices[] = {1,1};
SafeArrayPutElement(arr.parray, indices, &parm1);
}


int k=2;
int i=0;
WCHAR *hrs[]={L"Native Class",L"Caption",L"Height",L"Width",L"Window ID",L"Window Style"};
for(int j=1; j<=6; j++) {
// Create entry value for (i,j)
BSTR b;
VARIANT parm1;
b = SysAllocString(hrs[i]);
i++;
parm1.vt = VT_BSTR;
parm1.bstrVal = b;
// Add to safearray...
long indices[] = {k,1};
k++;
SafeArrayPutElement(arr.parray, indices, &parm1);
}

// Get current sheet -- Haroon
// Get ActiveSheet object
IDispatch *pXlSheet;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"ActiveSheet", 0);
pXlSheet = result.pdispVal;
}

// Get Range object for the Range A1:O15...
IDispatch *pXlRange;
{
VARIANT parm;
parm.vt = VT_BSTR;
parm.bstrVal = ::SysAllocString(L"A1:A8");
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
//VariantClear(&parm);
pXlRange = result.pdispVal;
}

// Set range with our safearray...
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlRange, L"Value", 1, arr);

//Save the work book.
{
VARIANT fname;
fname.vt = VT_BSTR;
fname.bstrVal =::SysAllocString(L"D:\\output3.xls");
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlSheet, L"Saved", 1, fname);
}

// Quit Excel
AutoWrap(DISPATCH_METHOD, NULL, pXlApp, L"Quit", 0);

// Release references...
pXlRange->Release();
pXlSheet->Release();
pXlBook->Release();
pXlBooks->Release();
pXlApp->Release();
VariantClear(&arr);

// Uninitialize COM for this thread...
CoUninitialize();
}

currently i am facing problem in saving the excel file. i can able to write and read but not able to auto-save the excel file. please help me.
Topic archived. No new replies allowed.