Can't get Excel SaveAs dialog to show

M

mjoe

I'm trying to show the Excel SaveAs dialog from an ATL app.

In VB, the following works fine:

Application.Dialogs("xlDialogSaveAs").Show sFileName

But in ATL, I've had all sorts of trouble with getting this
to work. Here's what I have right now:

CComPtr<IDispatch> m_pExcel;
HRESULT hr = m_pExcel.CoCreateInstance(L"Excel.Application", NULL,
CLSCTX_SERVER);
CComDispatchDriver cddExcel = m_pExcel;

CComVariant vTrue(TRUE);
hr = cddExcel.PutPropertyByName(L"Visible",&vTrue);

CComVariant cvDialogs;
cddExcel.GetPropertyByName(L"Dialogs", &cvDialogs);
CComDispatchDriver cddDialogs = cvDialogs.pdispVal;

CComVariant cvCount;
hr = cddDialogs.GetPropertyByName(L"Count",&cvCount);

DISPID dwDispID;
hr = cddDialogs.GetIDOfName(L"Item", &dwDispID);

CComVariant cvIndex(5); //xlDialogSaveAs
DISPPARAMS dispparams = { (VARIANT*)&cvIndex, NULL, 1, 0 };

CComVariant cvDialog;
cddDialogs->Invoke(dwDispID, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD,
&dispparams, &cvDialog, NULL, NULL);

CComDispatchDriver cddDialog = cvDialog.pdispVal;
CComVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);

CComVariant cvResult;
VARIANT vArgsShow[30];
memset(vArgsShow, 0, sizeof(VARIANT) * 30);
for(int i=0;i<30;i++)
{
vArgsShow = vOpt;
}
CComVariant cvDocText("C:\\testing.xls");
vArgsShow[29] = cvDocText; //document_text
CComVariant cvTypeNum(1);
vArgsShow[28] = cvTypeNum; //type_num
CComVariant cvProtPwd("");
vArgsShow[27] = cvProtPwd; //prot_pwd
CComVariant cvBackup(FALSE);
vArgsShow[26] = cvBackup; //backup
CComVariant cvWriteResPwd("");
vArgsShow[25] = cvWriteResPwd; //write_res_pwd
CComVariant cvReadOnlyRec(FALSE);
vArgsShow[24] = cvReadOnlyRec; //read_only_rec
hr = cddDialog.InvokeN(L"Show", vArgsShow, 30, &cvResult);

return 0;

The excel instance starts up fine, I get the Dialogs object,
it has a count of over 700 dialogs, and I'm able to get the
dialog xlDialogSaveAs.

However, I can't get the invoke of the Show method to work.
I've tried all optional parms, just filename, parameters in
forward and backward order, all parameters. Nothing seems
to work.

Anybody have any ideas whats wrong here?

Thanks,

-M-
 
G

Guest

Hi M'

You need to use the name for the automation object before
the application call. Maybe like this:

Hr.Application.Dialogs("xlDialogSaveAs").Show sFileName
 
M

mjoe

The VB version works fine for me, even without the automation
object (I think its implied). Its the C++/ATL version that I can't
get to show the dialog.

-M-

Hi M'

You need to use the name for the automation object before
the application call. Maybe like this:

Hr.Application.Dialogs("xlDialogSaveAs").Show sFileName
-----Original Message-----
I'm trying to show the Excel SaveAs dialog from an ATL app.

In VB, the following works fine:

Application.Dialogs("xlDialogSaveAs").Show sFileName

But in ATL, I've had all sorts of trouble with getting this
to work. Here's what I have right now:

CComPtr<IDispatch> m_pExcel;
HRESULT hr = m_pExcel.CoCreateInstance (L"Excel.Application", NULL,
CLSCTX_SERVER);
CComDispatchDriver cddExcel = m_pExcel;

CComVariant vTrue(TRUE);
hr = cddExcel.PutPropertyByName(L"Visible",&vTrue);

CComVariant cvDialogs;
cddExcel.GetPropertyByName(L"Dialogs", &cvDialogs);
CComDispatchDriver cddDialogs = cvDialogs.pdispVal;

CComVariant cvCount;
hr = cddDialogs.GetPropertyByName(L"Count",&cvCount);

DISPID dwDispID;
hr = cddDialogs.GetIDOfName(L"Item", &dwDispID);

CComVariant cvIndex(5); //xlDialogSaveAs
DISPPARAMS dispparams = { (VARIANT*)&cvIndex, NULL, 1, 0 };

CComVariant cvDialog;
cddDialogs->Invoke(dwDispID, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD,
&dispparams, &cvDialog, NULL, NULL);

CComDispatchDriver cddDialog = cvDialog.pdispVal;
CComVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);

CComVariant cvResult;
VARIANT vArgsShow[30];
memset(vArgsShow, 0, sizeof(VARIANT) * 30);
for(int i=0;i<30;i++)
{
vArgsShow = vOpt;
}
CComVariant cvDocText("C:\\testing.xls");
vArgsShow[29] = cvDocText; //document_text
CComVariant cvTypeNum(1);
vArgsShow[28] = cvTypeNum; //type_num
CComVariant cvProtPwd("");
vArgsShow[27] = cvProtPwd; //prot_pwd
CComVariant cvBackup(FALSE);
vArgsShow[26] = cvBackup; //backup
CComVariant cvWriteResPwd("");
vArgsShow[25] = cvWriteResPwd; //write_res_pwd
CComVariant cvReadOnlyRec(FALSE);
vArgsShow[24] = cvReadOnlyRec; //read_only_rec
hr = cddDialog.InvokeN(L"Show", vArgsShow, 30, &cvResult);

return 0;

The excel instance starts up fine, I get the Dialogs object,
it has a count of over 700 dialogs, and I'm able to get the
dialog xlDialogSaveAs.

However, I can't get the invoke of the Show method to work.
I've tried all optional parms, just filename, parameters in
forward and backward order, all parameters. Nothing seems
to work.

Anybody have any ideas whats wrong here?

Thanks,

-M-
.
 
P

Peter Huang

Hi Marion,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use dispatch
interface to show the SaveAs dialog in Excel.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think to show the SaveAs dialog, we need to open/create a workbook first.
Here goes the code, you may have a try.

CComPtr<IDispatch> m_pExcel;
HRESULT hr = m_pExcel.CoCreateInstance(L"Excel.Application", NULL,
CLSCTX_SERVER);
CComDispatchDriver cddExcel = m_pExcel;
CComVariant vTrue(TRUE);
hr = cddExcel.PutPropertyByName(L"Visible",&vTrue);

==========================Add a new workbook=======================
CComVariant cvWorkBooks;
cddExcel.GetPropertyByName(L"WorkBooks",&cvWorkBooks);
CComDispatchDriver ccdWorkBooks = cvWorkBooks.pdispVal;
ccdWorkBooks.Invoke0(L"Add");

CComVariant cvDialogs;
cddExcel.GetPropertyByName(L"Dialogs", &cvDialogs);
CComDispatchDriver cddDialogs = cvDialogs.pdispVal;
CComVariant cvCount;
hr = cddDialogs.GetPropertyByName(L"Count",&cvCount);
DISPID dwDispID;
hr = cddDialogs.GetIDOfName(L"Item", &dwDispID);
CComVariant cvIndex(5); //xlDialogSaveAs
DISPPARAMS dispparams = { (VARIANT*)&cvIndex, NULL, 1, 0 };
CComVariant cvDialog;
cddDialogs->Invoke(dwDispID, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD,
&dispparams, &cvDialog, NULL, NULL);
CComDispatchDriver cddDialog = cvDialog.pdispVal;
CComVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant cvResult;
VARIANT vArgsShow[30];
memset(vArgsShow, 0, sizeof(VARIANT) * 30);
for(int i=0;i<30;i++)
{
vArgsShow = vOpt;
}
CComVariant cvDocText("C:\\testing.xls");
vArgsShow[29] = cvDocText; //document_text
CComVariant cvTypeNum(1);
vArgsShow[28] = cvTypeNum; //type_num
CComVariant cvProtPwd("");
vArgsShow[27] = cvProtPwd; //prot_pwd
CComVariant cvBackup(FALSE);
vArgsShow[26] = cvBackup; //backup
CComVariant cvWriteResPwd("");
vArgsShow[25] = cvWriteResPwd; //write_res_pwd
CComVariant cvReadOnlyRec(FALSE);
vArgsShow[24] = cvReadOnlyRec; //read_only_rec
hr = cddDialog.InvokeN(L"Show", vArgsShow, 30, &cvResult);


Also base on my test,

Private Sub Command1_Click()
Dim exApp As New Excel.Application
exApp.Visible = True
'exApp.Workbooks.Add 'If we comment the line the Show method will fail too
in VB.
exApp.Dialogs(xlDialogSaveAs).Show
End Sub

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi Marion

Have you tried my suggestion? Did that works for you?

I would appreciate if you could provide an update by posting a message in
the Newsgroup thread letting us know whether the information provided in
the response was useful in resolving the issue. If you still have
questions, please reply to the thread to let us know you need further
assistance.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Top