none
Not able to specify a specific folder location while SaveAs with Excel RRS feed

  • Question

  • The link  https://social.msdn.microsoft.com/Forums/en-US/dcbea59b-a32f-4341-a7b0-24415f544320/excel-save-as-dialog-outputs-corrupted-pdf?forum=exceldev suggests to use the below code to avoid corruption of the PDF

    Excel.Dialog dialog =_excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
    
    dialog.Show();

    But the problem is I am not able to specify a folder for the dialog to open. FOr example I always want to open with ABC folder location by default for saving.

    So i try the below:

    Excel.Dialog dialog =_excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
    
    dialog.Show(folder path + file name);

    The above does not open the dialog in the folder path specified. Is this a bug in the inbuilt dialog ?

    Sunday, April 8, 2018 1:31 PM

Answers

  • Hello Shaik Jafer Ali,

    Since it could work if the workbook is not saved, as a workaround for saved workbook, we could create a new unsaved workbook based on the saved workbook and then call dialog to save PDF.

    Here is the simply code.

     Excel.Application _excelApp = Globals.ThisAddIn.Application;
                Excel.Dialog dialog = _excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
                Excel.Workbook originalWorkbook = _excelApp.ActiveWorkbook;
                string folder_path = @"C:\Users\Admin\Desktop\TestFolder";
                Excel.Workbook copiedWorkbook;
                if (originalWorkbook.Path == "")
                {
                    dialog.Show(folder_path);
                }
                else {
                    //save workbook
                    originalWorkbook.Save();
                    //close original workbook
                    string oringalFileFullName = originalWorkbook.FullName;
                    originalWorkbook.Close();
                    //open a new unsaved workbook based on original workbook
                    copiedWorkbook = _excelApp.Workbooks.Add(oringalFileFullName);
                    copiedWorkbook.Activate(); 
                    //call save as 
                    dialog.Show(folder_path);
                    //close unsaved workbook
                    copiedWorkbook.Close(false);
                    //back to open original workbook
                    _excelApp.Workbooks.Open(oringalFileFullName);
                }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 9, 2018 8:49 AM

All replies

  • What is folder path?

    Does "folder path" folder exists already?

    E.g. If folder_path is variable & folder_path="C:\temp" & folder temp exists on C:

    then dialog should open correctly in "C:\temp" i.e.

     Excel.Dialog dialog = _excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
     string folder_path = "C:\\temp\\";
     dialog.Show(folder_path);

    If Path "C:\temp" does not exists then dialog will open in Documents folder

    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.







    Sunday, April 8, 2018 1:58 PM
  • Possibly if workbook is saved then the dialog will open in saved folder. If you test on file which is not saved, then it will show the folder correctly

    Assume you need to allow user to choose and folder and save. the dialog box does both. But it is not allowing new folder for already saved workbook.

    Have you tried Application.FileDialog(msoFileDialogSaveAs)


    Best Regards, Asadulla Javed

    Monday, April 9, 2018 4:34 AM
    Answerer
  • I created a folder "D:\ABC" which has test.xlsx.  I opened test.xlsx. I need to save as in D:\Temp with same file name. When i do save as it still is on D:\ABC.

    My actual requirement is to default to a mapped drive. But as the above it self does not work I did not see the mapped drive this time

                Excel.Dialog dialog = excapp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
                System.Diagnostics.Debug.Write(driveName + excapp.ActiveWorkbook.Name);
    
                bool b = dialog.Show("D:\\Temp");

    Best Regards

    Jafer

    Monday, April 9, 2018 4:35 AM
  • I have tried Application.FileDialog(msoFileDialogSaveAs) and this works for saving to specific folder. But it has other issues like PDF and XPS Corruption after saving as indicated in https://social.msdn.microsoft.com/Forums/en-US/dcbea59b-a32f-4341-a7b0-24415f544320/excel-save-as-dialog-outputs-corrupted-pdf?forum=exceldev.

    So I dropped the approach using Application.FileDialog(msoFileDialogSaveAs).

    If there is a way to save to specific folder using  below code 

    Excel.Dialog dialog = _excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
     string folder_path = "C:\\temp\\";
     dialog.Show(folder_path);

    It would be helpful. But above does not work.

    Monday, April 9, 2018 6:07 AM
  • As mentioned by Bernie, PDF is not done by save as of excel file. It is exported (Range, SHeet, Filename)...

    and Saveas dialog box allows to define folder only if workbook is not saved earlier. It work on file which is creted by Ctrl+N or File Menu- >New

    For above limitation it is most logical that give exception for PDF type only in your code.


    Best Regards, Asadulla Javed

    Monday, April 9, 2018 6:42 AM
    Answerer
  • Hello Shaik Jafer Ali,

    Since it could work if the workbook is not saved, as a workaround for saved workbook, we could create a new unsaved workbook based on the saved workbook and then call dialog to save PDF.

    Here is the simply code.

     Excel.Application _excelApp = Globals.ThisAddIn.Application;
                Excel.Dialog dialog = _excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogSaveAs];
                Excel.Workbook originalWorkbook = _excelApp.ActiveWorkbook;
                string folder_path = @"C:\Users\Admin\Desktop\TestFolder";
                Excel.Workbook copiedWorkbook;
                if (originalWorkbook.Path == "")
                {
                    dialog.Show(folder_path);
                }
                else {
                    //save workbook
                    originalWorkbook.Save();
                    //close original workbook
                    string oringalFileFullName = originalWorkbook.FullName;
                    originalWorkbook.Close();
                    //open a new unsaved workbook based on original workbook
                    copiedWorkbook = _excelApp.Workbooks.Add(oringalFileFullName);
                    copiedWorkbook.Activate(); 
                    //call save as 
                    dialog.Show(folder_path);
                    //close unsaved workbook
                    copiedWorkbook.Close(false);
                    //back to open original workbook
                    _excelApp.Workbooks.Open(oringalFileFullName);
                }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 9, 2018 8:49 AM
  • Thank you, Terry.
    Wednesday, April 11, 2018 11:32 AM
  • Hello Shaik Jafer Ali,

    It seems that your original issue has been resolved. If so, I would suggest you mark helpful reply to close the thread. If not, please feel free to let us know the current state.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 12, 2018 6:12 AM