How To Reset/Set "Save As dialog" Path via Interop?

Y

Yazeed

Hello All,

I was wondering if there is a way to reset the "Save As dialog" initial path
when the user clicks on Save as from the menu.

As an example for excel, if you opened a workbook via interop (C#, VB,.....)
and made it visible to the user, then manually opened the excel menu and
clicked save as, the path displayed is the same one where the workbook got
opened, is there a way to reset that path or set it differently??

Thanks.
 
Y

Yazeed

Sorry i forgot to mention that im using office 2007 and

Microsoft.Office.Interop.X.dll version 12 -where X is the application name
(Excel,Word,PowerPoint,....).
 
J

JMiguez

Sorry i forgot to mention that im using office 2007 and

Microsoft.Office.Interop.X.dll version 12 -where X is the application name
(Excel,Word,PowerPoint,....).



Here is a small code and comprehensive to keep you with the save as
dialog for Excel purposes.
This is should be what you are looking for and more. Hopefully we
clear other problems with
the users... right? For me this code works well, but try it and let me
know.


/// <summary>
/// Saves the data in a user-specified file.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butSave_Click(object sender, EventArgs e)
{
//New instance of the save file dialog
SaveFileDialog SaveFile = new SaveFileDialog();
//Apply a filter to provide user with different options.
Remember that whether it says XLS we are still writing the data in
XML.
SaveFile.Filter = "Excel (*.xls)|*.xls|CSV (*.csv)|*.csv|
XML (*.xml)|*.xml|All Known Formats (*.xls *.xml, *.csv)|*.xml; *.xls;
*.csv";
//Everyone knows the XLS extension at work, so I decided
to place the XLS extension. Up to you.
SaveFile.DefaultExt = ".xls";
//This is the title that you can place for the dialog.
SaveFile.Title = "Select filename and location";
//This is the save dialog initial path, basically what you
wanted.
SaveFile.InitialDirectory = Application.StartupPath;
//Show the dialog.
SaveFile.ShowDialog(this);
try
{
//I didn't really feel like doing the CSV for them
because wanted to do everything in XML, so CSV will be somewhat
ambiguous.
switch (SaveFile.FileName.ToUpper().Substring
(SaveFile.FileName.Length - 4, 4))
{
case ".XLS":
axSpreadsheet1.Export(SaveFile.FileName,
Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone,
Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);
break;
case ".XML":
axSpreadsheet1.Export(SaveFile.FileName,
Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone,
Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);
break;
case ".CSV":
axSpreadsheet1.Export(SaveFile.FileName,
Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone,
Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);
break;
default:
axSpreadsheet1.Export(SaveFile.FileName,
Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone,
Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);
break;
}
}
catch
{
MessageBox.Show("There are problems saving to the
file. File not saved.");
}
}
 
Y

Yazeed

Thanks JMiguez for your reply, but this is not what i want to do.

i think you thought that i need to reset the path in .NET SaveFileDialog
Class, on the other hand, i need to reset the path in "EXCEL Save File
Dialog". as when you open excel workbook, click on the menu and then save as,
notice the initial path displayed, its the same one where the workbook got
opened, what i need is to reset that path using Interop.Excel V12.

I tried to get the Excel SaveAsDialog but no luck to reset the path.

Here is a code snippet on what i need to do:

Application excelApplication = new Application();
excelApplication.Workbooks.Open(Path.Combine
(WORKBOOK_PATH, WORKBOOK_NAME), 0, false, 5, "", "", false,
XlPlatform.xlWindows, "", true, false, 0, true, false, false)

excelApplication.Visible = true;

Now after executing this code snippet, excel application will open along
with the specified workbook, now, click on menu-->Save As.

you will notice the path set there is the same as "WORKBOOK_PATH", i need to
reset this path.

Thanks again.
 
Y

Yazeed

Ok i cracked it and found a workaround as following in case of someone is
searching for this:


1. you have to capture the BeforeClose Event.
2. in the event Handler do the following:

Cancel = false;

FileDialog saveAsDialog =
ExcelApplication.get_FileDialog(MsoFileDialogType.msoFileDialogSaveAs);

saveAsDialog.InitialFileName = @"C:\"; //this is the path you want
to set

if (saveAsDialog.Show() != 0)
{
saveAsDialog.Execute();
}


The idea is to cancel the SaveAsDialog opened by excel and show your own, so
basically when the user clicks on save as, the default dialog is canceled and
a new one is opened with your customization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top