C# - Snippets
Finalize
~SurroundingClass()
{
base.Finalize();
gApplicationExcel = null;
gErrorExcel = null;
gCmdBarExcel = null;
}
Message_CustomAlreadyExists
public static void CustomPropertyAlreadyExistsInformation(
string sPropertyName)
{
System.Windows.Forms.MessageBox.Show(
"The custom property '" + sPropertyName + "' already exists in this workbook.",
gsDIALOG_PREFIX_EXCEL,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information
);
}
Message_SelNoRangeSelected
public static void SelNoRangeSelectedInformation()
{
System.Windows.Forms.MessageBox.Show(
"No range is currently selected.",
gsDIALOG_PREFIX_EXCEL,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information
);
}
New
public clsExcel(
string sSolutionName,
string sDialogPrefix,
string sOfficeSolution,
Excel.Application objExcel,
string sVersion,
clsCmdBar objCmdBar)
{
try
{
gApplicationExcel = objExcel;
gsSOLUTION_NAME_EXCEL = sSolutionName;
gsDIALOG_PREFIX_EXCEL = sDialogPrefix;
gErrorExcel = new clsError(
gsSOLUTION_NAME_EXCEL,
gsDIALOG_PREFIX_EXCEL,
sOfficeSolution,
objExcel,
sVersion
);
gCmdBarExcel = objCmdBar;
gbDEBUG_EXCEL = clsError.DebugFlag("Debug Excel");
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"New",
"clsExcel",
"initialise the 'clsExcel' object.",
mobjCOMException,
mobjException
);
}
}
}
Property_BuiltInCount
public static int Property_BuiltInCount(
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return 0;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
PropertyBuiltInCount =
gApplicationExcel.Workbooks[sWbkName]
.BuiltinDocumentProperties
.Count;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyBuiltInCount",
"clsWbk",
"return the number of 'built-in' workbook properties.",
mobjCOMException,
mobjException
);
}
}
return PropertyBuiltInCount;
}
Property_BuiltInExists
public static bool Property_BuiltInExists(
string sPropertyName,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return false;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.BuiltinDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
PropertyBuiltInExists = true;
return PropertyBuiltInExists;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyBuiltInExists",
"clsWbk",
"determine if the 'built-in' workbook property '" +
sPropertyName + "' exists.",
mobjCOMException,
mobjException
);
}
}
return PropertyBuiltInExists;
}
Property_BuiltInGet
public static object Property_BuiltInGet(
string sPropertyName,
object objDefaultValue = null,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return null;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.BuiltinDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
PropertyBuiltInGet = props.Item(ipropertycount).Value;
return PropertyBuiltInGet;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyBuiltInGet",
"clsWbk",
"return the 'built-in' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
return PropertyBuiltInGet;
}
Property_BuiltInSet
public static void Property_BuiltInSet(
string sPropertyName,
object objPropertyValue,
string sPropertyType = "Text",
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.BuiltinDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
props.Item(ipropertycount).Value = objPropertyValue;
return;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyBuiltInSet",
"clsWbk",
"change the 'built-in' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
}
Property_CustomAdd
public static void Property_CustomAdd(
string sPropertyName,
object objPropertyValue,
string sPropertyType = "Text",
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
if (modWorkbook.PropertyCustomExists(sWbkName, sPropertyName) == false)
{
var props = gApplicationExcel
.Workbooks[sWbkName]
.CustomDocumentProperties;
props.Add(
Name: sPropertyName,
LinkToContent: Office.MsoTriState.msoFalse,
Value: objPropertyValue,
Type: clsWbk.PropertyTypeReturn(sPropertyType)
);
}
else
{
clszMessagesExcel.CustomPropertyAlreadyExistsInformation(
sPropertyName
);
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomAdd",
"clsWbk",
"add the 'custom' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
}
Property_CustomCount
public static int Property_CustomCount(
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return 0;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
PropertyCustomCount =
gApplicationExcel.Workbooks[sWbkName]
.CustomDocumentProperties
.Count;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomCount",
"clsWbk",
"return the number of 'custom' workbook properties.",
mobjCOMException,
mobjException
);
}
}
return PropertyCustomCount;
}
Property_CustomDelete
public static void Property_CustomDelete(
string sPropertyName,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.CustomDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
props.Item(ipropertycount).Delete();
return;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomDelete",
"clsWbk",
"delete the 'custom' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
}
Property_CustomExists
public static bool Property_CustomExists(
string sPropertyName,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return false;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.CustomDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
Property_CustomExists = true;
return Property_CustomExists;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomExists",
"clsWbk",
"determine if the 'custom' workbook property '" + sPropertyName + "' exists.",
mobjCOMException,
mobjException
);
}
}
return Property_CustomExists;
}
Property_CustomGet
public static object Property_CustomGet(
string sPropertyName,
object objDefaultValue = null,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return null;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.CustomDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
Property_CustomGet = props.Item(ipropertycount).Value;
return Property_CustomGet;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomGet",
"clsWbk",
"return the 'custom' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
return Property_CustomGet;
}
Property_CustomSet
public static void Property_CustomSet(
string sPropertyName,
object objPropertyValue,
string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
if (sWbkName == "")
{
sWbkName = gApplicationExcel.ActiveWorkbook.Name;
}
int ipropertycount;
string sitemname;
var props = gApplicationExcel
.Workbooks[sWbkName]
.CustomDocumentProperties;
for (ipropertycount = 1; ipropertycount <= props.Count; ipropertycount++)
{
sitemname = props.Item(ipropertycount).Name;
if (sitemname == sPropertyName)
{
props.Item(ipropertycount).Value = objPropertyValue;
return;
}
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"PropertyCustomSet",
"clsWbk",
"change the 'custom' workbook property '" + sPropertyName + "'.",
mobjCOMException,
mobjException
);
}
}
}
Selection_IsChartSelected
public static bool Selection_IsChartSelected(
string sSelectionType = "")
{
try
{
if (clsError.ErrorFlag() == true)
return false;
string stypename;
if (sSelectionType.Length == 0)
sSelectionType = clsWsh.ReturnSelection;
switch (sSelectionType)
{
case "ChartObject":
case "ChartArea":
case "Series":
case "ChartTitle":
case "PlotArea":
case "Axis":
case "AxisTitle":
case "Legend":
return true;
default:
return false;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ChartSelected",
"clsSel",
"determine if there is currently a chart selected.",
mobjCOMException,
mobjException
);
}
}
return false;
}
Selection_IsRangeSelected
public static bool Selection_IsRangeSelected(
bool bInformUser = false)
{
Excel.Range objSelection = null;
try
{
if (clsError.ErrorFlag() == true)
return false;
int icolumnfirst;
int icolumnlast;
int irowwfirst;
int irowwlast;
objSelection = (Excel.Range)gApplicationExcel.Selection;
icolumnfirst = objSelection.Column;
icolumnlast = objSelection.Column + objSelection.Columns.Count - 1;
irowwfirst = objSelection.Row;
irowwlast = irowwfirst + objSelection.Rows.Count - 1;
if (icolumnfirst == icolumnlast && irowwfirst == irowwlast)
{
if (bInformUser == true)
{
clszMessagesExcel.SelNoRangeSelectedInformation();
}
return false;
}
else
{
return true;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
objSelection = null;
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"RangeSelected",
"clsSel",
"determines if there is currently a range of cells selected.",
mobjCOMException,
mobjException
);
}
}
return false;
}
Selection_ReturnName
public static string SelectionNameReturn()
{
try
{
// an error occurs if there is nothing selected
return gApplicationExcel.Selection.Name;
}
catch (System.Runtime.InteropServices.COMException)
{
return "";
}
}
Selection_ReturnTypeName
public static string ReturnSelectionTypeName()
{
try
{
if (clsError.ErrorFlag() == true)
return "";
return Microsoft.VisualBasic.Information.TypeName(
gApplicationExcel.Selection
);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ReturnSelectionTypeName",
"clsSel",
"return the description of the current selection.",
mobjCOMException,
mobjException
);
}
}
return "";
}
Selection_ShapeRange
public static Excel.ShapeRange Selection_ShapeRange()
{
try
{
// an error occurs if there is nothing selected
return gApplicationExcel.Selection.ShapeRange;
}
catch (System.Runtime.InteropServices.COMException)
{
return null;
}
}
Selection_TryPaste
public static void SelectionPaste()
{
try
{
gApplicationExcel.Selection.Paste();
}
catch (System.Runtime.InteropServices.COMException)
{
}
}
Wbk_Active
public static bool Wbk_Active()
{
try
{
if (clsError.ErrorFlag() == true)
return;
string swbkname;
swbkname = gApplicationExcel.ActiveWorkbook.Name;
Active = true;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
Active = false;
}
}
Wbk_Close
public static void Wbk_Close(
bool bSave,
string sWbkName = "",
Windows.Forms.ListBox lstBoxName = null,
bool bInformUser = true)
{
Excel.Workbook objWorkbook;
try
{
if (clsError.ErrorFlag() == true)
{
return;
}
if (sWbkName != "")
{
objWorkbook = gApplicationExcel.Workbooks(sWbkName);
}
else
{
objWorkbook = gApplicationExcel.ActiveWorkbook();
}
if (gApplicationExcel.Workbooks.Count == 0)
{
return;
}
gApplicationExcel.StatusBar = "Closing the file : " + objWorkbook.Name + " ...";
if (!lstBoxName == null)
lstBoxName.Text = "Closing the file : " + objWorkbook.Name + " ...";
gApplicationExcel.ScreenUpdating = true;
gApplicationExcel.DisplayAlerts = false;
objWorkbook.Close(savechanges: bSave);
gApplicationExcel.DisplayAlerts = true;
gApplicationExcel.StatusBar = false;
if (!lstBoxName == null)
{
lstBoxName.Text = "";
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
objWorkbook = null/* TODO Change to default(_) if this is not a reference type */;
if (((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("CloseWbk", "clsWbk", "close the workbook '" + sWbkName + "'.", mobjCOMException, mobjException);
}
}
Wbk_ColourPaletteDefine
public static void Wbk_ColourPaletteDefine()
{
Excel.Workbook objWorkbook = null;
try
{
if (clsError.ErrorFlag() == true)
return;
objWorkbook = gApplicationExcel.ActiveWorkbook;
// chart fill colours
objWorkbook.Colors[17] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(240, 76, 71));
objWorkbook.Colors[18] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(248, 148, 81));
objWorkbook.Colors[19] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(117, 0, 71));
objWorkbook.Colors[20] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(0, 171, 215));
// chart line colours
objWorkbook.Colors[25] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(240, 76, 71));
objWorkbook.Colors[26] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(248, 148, 81));
objWorkbook.Colors[27] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(117, 0, 71));
objWorkbook.Colors[28] = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(0, 171, 215));
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
objWorkbook = null;
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ColourPaletteDefine",
"clsWbk",
"add the specific colours to the colour palette.",
mobjCOMException,
mobjException
);
}
}
}
Wbk_GetFullName
public static string Wbk_GetFullName()
{
try
{
if (clsError.ErrorFlag() == true)
return null;
FullNameGet = gApplicationExcel.ActiveWorkbook.FullName;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"FullNameGet",
"clsWbk",
"returns the fullname of the active presentation.",
mobjCOMException,
mobjException
);
}
}
return FullNameGet;
}
Wbk_IsSaved
public static bool Wbk_IsSaved(
Excel.Workbook workbook)
{
try
{
if (workbook.FullName.Contains(".xlsx"))
{
return true;
}
return false;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return false;
}
}
Wbk_Open
public static bool Wbk_Open(
string sFolderPath,
string sWbkName,
int iUpdateLinks = 0,
string sAdditional = "",
string sExtension = ".xlsx",
bool bReadOnly = false,
System.Windows.Forms.Label lblProgress = null,
bool bInformUser = true)
{
try
{
if (clsError.ErrorFlag() == true)
return false;
gApplicationExcel.StatusBar =
"Opening the file : " +
sFolderPath + sWbkName + sAdditional + sExtension + " ...";
if (lblProgress != null)
{
lblProgress.Text =
"Opening the file : " +
sFolderPath + sWbkName + sAdditional + sExtension + " ...";
}
gApplicationExcel.Workbooks.Open(
FileName: sFolderPath + sWbkName + sAdditional + sExtension,
UpdateLinks: iUpdateLinks,
ReadOnly: bReadOnly
).RunAutoMacros(Excel.XlRunAutoMacro.xlAutoOpen);
OpenWbk = true;
gApplicationExcel.StatusBar = false;
if (lblProgress != null)
{
lblProgress.Text = "";
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
OpenWbk = false;
if (mobjCOMException != null || mobjException != null)
{
clsError.Handle(
"OpenWbk",
"clsWbk",
"open the workbook called '" +
sWbkName + sAdditional + sExtension + "'" +
Environment.NewLine +
"from the folder location" + Environment.NewLine +
sFolderPath,
mobjCOMException,
mobjException
);
}
}
return OpenWbk;
}
Wbk_Save
public static void Wbk_Save(
Excel.Workbook workbook)
{
try
{
if (workbook.FullName.Contains(".xlsx"))
{
workbook.Save();
}
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex,
"Unable to save the workbook: " +
"'" + workbook.FullName + "'");
}
}
Wbk_SaveAs
public static bool Wbk_SaveAs(
string sFolderPath,
string sFileName,
string sExtension = ".xls",
bool bDeleteIfExists = false,
bool bInformUser = false,
string sAdditional = "")
{
try
{
bool breplace = false;
if (clsError.ErrorFlag() == true)
return false;
SaveAs = false;
if (clsFile.Exists(sFolderPath, sFileName, sExtension, false) == true)
{
if (bInformUser == true)
{
breplace = clsMessages.QuestionFileReplace(
sFolderPath,
sFileName
);
}
if (breplace == true)
{
if (bDeleteIfExists == true)
{
clsFile.Delete(
sFolderPath,
sFileName,
sExtension
);
}
gApplicationExcel.ActiveWorkbook.SaveAs(
FileName: sFolderPath +
sFileName +
sAdditional +
sExtension
);
SaveAs = true;
}
}
else
{
gApplicationExcel.ActiveWorkbook.SaveAs(
FileName: sFolderPath +
sFileName +
sAdditional +
sExtension
);
return true;
}
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
gobjCOMException = objCOMException;
}
catch (Exception objException)
{
gobjException = objException;
}
finally
{
if (gobjCOMException != null || gobjException != null)
{
clsError.Handle(
"SaveAs",
"clsWbk",
"save the file \"" +
sFileName + sExtension + " \"" +
"in the directory" + Environment.NewLine +
sFolderPath,
gobjCOMException,
gobjException
);
}
}
return SaveAs;
}
Wbk_SelectWbk
public static void Wbk_SelectWbk(
string sWbkName,
Excel.Workbook objWorkbook)
{
Excel.Workbook objWorkbook = null;
try
{
if (clsError.ErrorFlag() == true)
return;
objWorkbook = gApplicationExcel.Workbooks[sWbkName];
clszLateBindingExcel.clsWbkSelectWbk(objWorkbook);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
objWorkbook = null;
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"SelectWbk",
"clsWbk",
"",
mobjCOMException,
mobjException
);
}
}
}
Wbk_WshsAllToArray
public static string[] Wbk_WshsAllToArray(
string sWbkName = "",
string sSepearteChar = ";",
string sIgnoreList = "")
{
Excel.Workbook objWorkbook = null;
Excel.Worksheet objWorksheet = null;
try
{
if (clsError.ErrorFlag() == true)
return null;
string[] asworksheetnames;
int iworksheetcount = 0;
if (sWbkName.Length > 0)
{
objWorkbook = gApplicationExcel.Workbooks[sWbkName];
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}
// ReDim asworksheetnames(objWorkbook.Worksheets.Count - 1)
asworksheetnames = new string[objWorkbook.Worksheets.Count];
foreach (Excel.Worksheet ws in objWorkbook.Worksheets)
{
objWorksheet = ws;
if (Microsoft.VisualBasic.Strings.InStr(
1,
sIgnoreList.ToUpper(),
objWorksheet.Name.ToUpper()
) == 0)
{
asworksheetnames[iworksheetcount] = objWorksheet.Name;
iworksheetcount = iworksheetcount + 1;
}
}
// ReDim asworksheetnames(iworksheetcount)
Array.Resize(ref asworksheetnames, iworksheetcount);
WshsAllToArray = asworksheetnames;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"WshsAllToArray",
"clsWbk",
"return a concatenated string of all the worksheets in the active workbook",
mobjCOMException,
mobjException
);
}
}
return WshsAllToArray;
}
Wbk_WshsAllToString
public static string Wbk_WshsAllToString(
string sWbkName = "",
string sSepearteChar = ";",
string sIgnoreList = "")
{
Excel.Workbook objWorkbook = null;
Excel.Worksheet objWorksheet = null;
try
{
if (clsError.ErrorFlag() == true)
return null;
string sallwshs;
if (sWbkName.Length > 0)
{
objWorkbook = gApplicationExcel.Workbooks[sWbkName];
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}
sallwshs = "";
foreach (Excel.Worksheet ws in gApplicationExcel.ActiveWorkbook.Worksheets)
{
objWorksheet = ws;
if (Microsoft.VisualBasic.Strings.InStr(
1,
sIgnoreList.ToUpper(),
objWorksheet.Name.ToUpper()
) == 0)
{
sallwshs = sallwshs + sSepearteChar + objWorksheet.Name;
}
}
WshsAllToString = Microsoft.VisualBasic.Strings.Right(
sallwshs,
sallwshs.Length - 1
);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"WshsAllToString",
"clsWbk",
"return a concatenated string of all the worksheets in the active workbook",
mobjCOMException,
mobjException
);
}
}
return WshsAllToString;
}
Wbk_WshsSelectedToArray
public static string[] Wbk_WshsSelectedToArray()
{
Excel.Worksheet objworksheet = null;
try
{
if (clsError.ErrorFlag() == true)
return null;
string[] asworksheetnames;
int iworksheetcount = 0;
// ReDim asworksheetnames(gApplicationExcel.ActiveWindow.SelectedSheets.Count - 1)
asworksheetnames = new string[
gApplicationExcel.ActiveWindow.SelectedSheets.Count
];
foreach (Excel.Worksheet ws in gApplicationExcel.ActiveWindow.SelectedSheets)
{
objworksheet = ws;
asworksheetnames[iworksheetcount] = objworksheet.Name;
iworksheetcount = iworksheetcount + 1;
}
WshsSelectedToArray = asworksheetnames;
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
objworksheet = null;
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"WshsSelectedToArray",
"clsWsh",
"return an array containing the names of all the worksheets that are currently selected.",
mobjCOMException,
mobjException
);
}
}
return WshsSelectedToArray;
}
Wbk_WshsSelectedToString
public static string Wbk_WshsSelectedToString(
string sWbkName = "",
string sSepearteChar = ";")
{
Excel.Workbook objWorkbook = null;
Excel.Worksheet objWorksheet = null;
try
{
if (clsError.ErrorFlag() == true)
return null;
string sallwshs;
if (sWbkName.Length > 0)
{
objWorkbook = gApplicationExcel.Workbooks[sWbkName];
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}
sallwshs = "";
foreach (Excel.Worksheet ws in gApplicationExcel.ActiveWindow.SelectedSheets)
{
objWorksheet = ws;
sallwshs = sallwshs + sSepearteChar + objWorksheet.Name;
}
WshsSelectedToString = sallwshs.Substring(0, sallwshs.Length - 1);
}
catch (System.Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"WshsAllToString",
"clsWbk",
"return a concatenated string of all the worksheets in the active workbook",
mobjCOMException,
mobjException
);
}
}
return WshsSelectedToString;
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top