TrySaveWorkbookIfNeverSaved
(
Microsoft.Office.Interop.Excel.Workbook oWorkbook,
String sFolderToSaveWorkbookTo
)
{
Debug.Assert(oWorkbook != null);
// The Workbook.Path is an empty string until the workbook is saved.
if ( String.IsNullOrEmpty(oWorkbook.Path) )
{
if ( String.IsNullOrEmpty(sFolderToSaveWorkbookTo) )
{
sFolderToSaveWorkbookTo = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
}
String sFileNameNoExtension;
// Use a suggested file name if available; otherwise use a file
// name based on the current time.
if ( ( new PerWorkbookSettings(oWorkbook) ).GraphHistory
.TryGetValue(
GraphHistoryKeys.ImportSuggestedFileNameNoExtension,
out sFileNameNoExtension) )
{
if (sFileNameNoExtension.Length >
MaximumImportSuggestedFileNameNoExtension)
{
sFileNameNoExtension = sFileNameNoExtension.Substring(
0, MaximumImportSuggestedFileNameNoExtension);
}
sFileNameNoExtension = FileUtil.ReplaceIllegalFileNameChars(
sFileNameNoExtension, " ");
}
else
{
sFileNameNoExtension =
DateTimeUtil2.ToCultureInvariantFileName(DateTime.Now)
+ " NodeXL";
}
try
{
ExcelUtil.SaveWorkbookAs(oWorkbook,
Path.Combine(sFolderToSaveWorkbookTo,
sFileNameNoExtension) );
}
catch (COMException)
{
FormUtil.ShowWarning(
"The workbook can't be saved, probably because the folder"
+ " where the workbook should be saved does not exist. To"
+ " fix this, go to NodeXL, Graph, Automate and change the"
+ " Options for \"Save workbook to a new file if it has"
+ " never been saved\"."
);
return (false);
}
}
return (true);
}