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.");
}
}