I am creating an Excel Add-In with VSTO and C#, and I would like to set the directory where the Add-In resulting files are saved (their filenames are programmatically generated).
I tried using SaveFileDialog saveDlg = new SaveFileDialog();
in my C# file but the Save File As Dialog didn't enable me to select a directory…
Could you help me with the properties of saveDlg
that I must set?
Thank you?
Here are some options for you:
string fileName;
string folderName;
//Option 1
// FolderBrowserDialog - returns only path to the folder
FolderBrowserDialog fileFolderBrowserDialog = new FolderBrowserDialog();
fileFolderBrowserDialog.ShowDialog();
folderName = fileFolderBrowserDialog.SelectedPath;
fileName = string.Concat(fileSaveAsDialog.FileName, "\\", "your generated file name.xlsx");
//Option 2
//FileDialog - returns full path to the file
//TODO: Set up this dialog with proper file extensions
FileDialog fileSaveAsDialog = new SaveFileDialog();
fileSaveAsDialog.AddExtension = true;
fileSaveAsDialog.Filter = "Excel Workbooks (*.xls, *.xlsx)|*.xls;*.xlsx|Excel Macro-Enabled Workbooks | *.xlsm |All files *.*| *.*";
fileSaveAsDialog.ShowDialog();
fileName = fileSaveAsDialog.FileName;
//Option 3
// Excel SaveAs method
// if file full name is generated programatically -
// you may save it without prompting user
Excel.Application xlApp = Globals.ThisAddIn.Application;
Excel.Workbook xlWB = xlApp.ActiveWorkbook; //or new Excel.Workbook(); or .Workbooks["Workbook name"]
fileName = "C:\\Users\\username\\Desktop\\my Excel wb.xlsx";
xlWB.SaveAs(fileName);