C# Snippets


Finalize

~SurroundingClass()
{
base.Finalize();

gApplicationExcel = null;
gErrorExcel = null;
gCmdBarExcel = null;
}
Protected Overrides Sub Finalize()
MyBase.Finalize()

gApplicationExcel = Nothing
gErrorExcel = Nothing
gCmdBarExcel = Nothing

End Sub

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, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information);
}
Public Shared Sub CustomPropertyAlreadyExistsInformation(ByVal sPropertyName As String)

Call System.Windows.Forms.MessageBox.Show( _
"The custom property '" & sPropertyName & "' already exists in this workbook.", _
gsDIALOG_PREFIX_EXCEL, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

End Sub

Message_SelNoRangeSelected

public static void SelNoRangeSelectedInformation()
{
System.Windows.Forms.MessageBox.Show("No range is currently selected.", gsDIALOG_PREFIX_EXCEL, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information);
}
Public Shared Sub SelNoRangeSelectedInformation()

Call System.Windows.Forms.MessageBox.Show( _
"No range is currently selected.", _
gsDIALOG_PREFIX_EXCEL, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

End Sub

New

public SurroundingClass(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 (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("New", "clsExcel", "initialise the 'clsExcel' object.", mobjCOMException, mobjException);
}
}
Public Sub New(ByVal sSolutionName As String, _
ByVal sDialogPrefix As String, _
ByVal sOfficeSolution As String, _
ByVal objExcel As Excel.Application, _
ByVal sVersion As String, _
ByVal objCmdBar As clsCmdBar)

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 objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("New", "clsExcel", _
"initialise the 'clsExcel' object.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Property_BuiltInCount

public static int Property_BuiltInCount(string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
// Office.DocumentProperties)
// PropertyBuiltInCount = objDocumentProperties.Count

if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

PropertyBuiltInCount = gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties.Count;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyBuiltInCount", "clsWbk", "return the number of 'built-in' workbook properties.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_BuiltInCount(Optional ByVal sWbkName As String = "") _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyBuiltInCount = objDocumentProperties.Count

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

PropertyBuiltInCount = gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties.Count

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInCount", "clsWbk", _
"return the number of 'built-in' workbook properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Property_BuiltInExists

public static bool Property_BuiltInExists(string sPropertyName, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim ipropertycount As Integer
// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
// Office.DocumentProperties)
// For ipropertycount = 1 To objDocumentProperties.Count
// If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
// PropertyBuiltInExists = True
// Exit Function
// End If
// Next ipropertycount

if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

int ipropertycount;
string sitemname;

{
var withBlock = gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
PropertyBuiltInExists = true;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyBuiltInExists", "clsWbk", "determine if the 'built-in' workbook property '" + sPropertyName + "' exists.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_BuiltInExists(ByVal sPropertyName As String, _
Optional ByVal sWbkName As String = "") _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim ipropertycount As Integer
'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'For ipropertycount = 1 To objDocumentProperties.Count
' If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
' PropertyBuiltInExists = True
' Exit Function
' End If
'Next ipropertycount

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
PropertyBuiltInExists = True
Exit Function
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInExists", "clsWbk", _
"determine if the 'built-in' workbook property '" & sPropertyName & "' exists.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Property_BuiltInGet

public static object Property_BuiltInGet(string sPropertyName, object objDefaultValue = null, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
// Office.DocumentProperties)
// PropertyBuiltInGet = objDocumentProperties.Item(sPropertyName).Value


if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

int ipropertycount;
string sitemname;

{
var withBlock = gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
PropertyBuiltInGet = withBlock.Item(ipropertycount).Value;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyBuiltInGet", "clsWbk", "return the 'built-in' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_BuiltInGet(ByVal sPropertyName As String, _
Optional ByVal objDefaultValue As Object = Nothing, _
Optional ByVal sWbkName As String = "") _
As Object

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyBuiltInGet = objDocumentProperties.Item(sPropertyName).Value


If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
PropertyBuiltInGet = .Item(ipropertycount).Value
Exit Function
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInGet", "clsWbk", _
"return the 'built-in' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

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 withBlock = gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
withBlock.Item(ipropertycount).Value = objPropertyValue;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyBuiltInSet", "clsWbk", "change the 'built-in' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub Property_BuiltInSet(ByVal sPropertyName As String, _
ByVal objPropertyValue As Object, _
Optional ByVal sPropertyType As String = "Text", _
Optional ByVal sWbkName As String = "")

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).BuiltinDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Value = objPropertyValue
Exit Sub
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInSet", "clsWbk", _
"change the 'built-in' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Property_CustomAdd

public static void Property_CustomAdd(string sPropertyName, object objPropertyValue, string sPropertyType = "Text", string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplication.Presentations(sPresentationName).CustomDocumentProperties, _
// Microsoft.Office.Core.DocumentProperties)

// objDocumentProperties.Add(Name:=sPropertyName, _
// LinkToContent:=Office.MsoTriState.msoFalse, _
// Value:=objPropertyValue, _
// Type:=Presentation_PropertyType(sPropertyType))

if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

if (modWorkbook.PropertyCustomExists(sWbkName, sPropertyName) == false)
{
{
var withBlock = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties;
withBlock.Add(Name: sPropertyName, LinkToContent: Office.MsoTriState.msoFalse, Value: objPropertyValue, Type: clsWbk.PropertyTypeReturn(sPropertyType));
}
}
else
clszMessagesExcel.CustomPropertyAlreadyExistsInformation(sPropertyName);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomAdd", "clsWbk", "add the 'custom' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub Property_CustomAdd(ByVal sPropertyName As String, _
ByVal objPropertyValue As Object, _
Optional ByVal sPropertyType As String = "Text", _
Optional ByVal sWbkName As String = "")

Try
If clsError.ErrorFlag() = True Then Exit Sub

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplication.Presentations(sPresentationName).CustomDocumentProperties, _
' Microsoft.Office.Core.DocumentProperties)

'objDocumentProperties.Add(Name:=sPropertyName, _
' LinkToContent:=Office.MsoTriState.msoFalse, _
' Value:=objPropertyValue, _
' Type:=Presentation_PropertyType(sPropertyType))

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

If modWorkbook.PropertyCustomExists(sWbkName, sPropertyName) = False Then

With gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties
.Add(Name:=sPropertyName, _
LinkToContent:=Office.MsoTriState.msoFalse, _
Value:=objPropertyValue, _
Type:=clsWbk.PropertyTypeReturn(sPropertyType))
End With

Else
Call clszMessagesExcel.CustomPropertyAlreadyExistsInformation(sPropertyName)
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomAdd", "clsWbk", _
"add the 'custom' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Property_CustomCount

public static int Property_CustomCount(string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
// Office.DocumentProperties)
// PropertyCustomCount = objDocumentProperties.Count

if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

PropertyCustomCount = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties.Count;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomCount", "clsWbk", "return the number of 'custom' workbook properties.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_CustomCount(Optional ByVal sWbkName As String = "") _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyCustomCount = objDocumentProperties.Count

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

PropertyCustomCount = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties.Count

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomCount", "clsWbk", _
"return the number of 'custom' workbook properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Property_CustomDelete

public static void Property_CustomDelete(string sPropertyName, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;


int ipropertycount;
string sitemname;

{
var withBlock = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
withBlock.Item(ipropertycount).Delete();
return;
}
}
}
}

catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomDelete", "clsWbk", "delete the 'custom' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub Property_CustomDelete(ByVal sPropertyName As String, _
Optional ByVal sWbkName As String = "")

Try
If clsError.ErrorFlag() = True Then Exit Sub


Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Delete()
Exit Sub
End If
Next ipropertycount
End With


Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomDelete", "clsWbk", _
"delete the 'custom' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Property_CustomExists

public static bool Property_CustomExists(string sPropertyName, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim ipropertycount As Integer
// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).CustomInDocumentProperties, _
// Office.DocumentProperties)
// For ipropertycount = 1 To objDocumentProperties.Count
// If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
// PropertyBuiltInExists = True
// Exit Function
// End If
// Next ipropertycount


if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

int ipropertycount;
string sitemname;

{
var withBlock = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
Property_CustomExists = true;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomExists", "clsWbk", "determine if the 'custom' workbook property '" + sPropertyName + "' exists.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_CustomExists(ByVal sPropertyName As String, _
Optional ByVal sWbkName As String = "") As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim ipropertycount As Integer
'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).CustomInDocumentProperties, _
' Office.DocumentProperties)
'For ipropertycount = 1 To objDocumentProperties.Count
' If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
' PropertyBuiltInExists = True
' Exit Function
' End If
'Next ipropertycount


If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
Property_CustomExists = True
Exit Function
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomExists", "clsWbk", _
"determine if the 'custom' workbook property '" & sPropertyName & "' exists.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Property_CustomGet

public static object Property_CustomGet(string sPropertyName, object objDefaultValue = null, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

// Dim objDocumentProperties As Office.DocumentProperties
// objDocumentProperties = _
// CType(gApplicationPowerPoint.Presentations(sPresentationName).CustomDocumentProperties, _
// Office.DocumentProperties)
// If clsPresentation.PropertyCustomExists(sPropertyName, sPresentationName) = True Then
// PropertyCustomGet = objDocumentProperties.Item(sPropertyName).Value
// Else
// PropertyCustomGet = objDefaultValue
// End If

if (sWbkName == "")
sWbkName = gApplicationExcel.ActiveWorkbook.Name;

int ipropertycount;
string sitemname;

{
var withBlock = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
Property_CustomGet = withBlock.Item(ipropertycount).Value;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomGet", "clsWbk", "return the 'custom' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Function Property_CustomGet(ByVal sPropertyName As String, _
Optional ByVal objDefaultValue As Object = Nothing, _
Optional ByVal sWbkName As String = "") _
As Object

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).CustomDocumentProperties, _
' Office.DocumentProperties)
'If clsPresentation.PropertyCustomExists(sPropertyName, sPresentationName) = True Then
' PropertyCustomGet = objDocumentProperties.Item(sPropertyName).Value
'Else
' PropertyCustomGet = objDefaultValue
'End If

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
Property_CustomGet = .Item(ipropertycount).Value
Exit Function
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomGet", "clsWbk", _
"return the 'custom' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

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 withBlock = gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties;
for (var ipropertycount = 1; ipropertycount <= withBlock.Count; ipropertycount++)
{
sitemname = withBlock.Item(ipropertycount).Name;

if (sitemname == sPropertyName)
{
withBlock.Item(ipropertycount).Value = objPropertyValue;
return;
}
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("PropertyCustomSet", "clsWbk", "change the 'custom' workbook property '" + sPropertyName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub Property_CustomSet(ByVal sPropertyName As String, _
ByVal objPropertyValue As Object, _
Optional ByVal sWbkName As String = "")

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sWbkName = "" Then
sWbkName = gApplicationExcel.ActiveWorkbook.Name
End If

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationExcel.Workbooks(sWbkName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Value = objPropertyValue
Exit Sub
End If
Next ipropertycount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomSet", "clsWbk", _
"change the 'custom' workbook property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Selection_IsChartSelected

public static bool Selection_IsChartSelected(string sSelectionType = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;

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":
{
ChartSelected = true;
break;
}

default:
{
ChartSelected = false;
break;
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("ChartSelected", "clsSel", "determine if there is currently a chart selected.", mobjCOMException, mobjException);
}
}
Public Shared Function Selection_IsChartSelected(Optional ByVal sSelectionType As String = "") _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim stypename As String

If sSelectionType.Length = 0 Then sSelectionType = clsWsh.ReturnSelection

Select Case sSelectionType
Case "ChartObject", _
"ChartArea", _
"Series", _
"ChartTitle", _
"PlotArea", _
"Axis", _
"AxisTitle", _
"Legend"

ChartSelected = True

Case Else
ChartSelected = False
End Select

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("ChartSelected", "clsSel", _
"determine if there is currently a chart selected.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Selection_IsRangeSelected

public static bool Selection_IsRangeSelected(bool bInformUser = false)
{
Excel.Range objSelection;

try
{
if (clsError.ErrorFlag() == true)
return;


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();
RangeSelected = false;
}
else
RangeSelected = true;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
objSelection = null/* TODO Change to default(_) if this is not a reference type */;

if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("RangeSelected", "clsSel", "determines if there is currently a range of cells selected.", mobjCOMException, mobjException);
}
}
Public Shared Function Selection_IsRangeSelected(Optional ByVal bInformUser As Boolean = False) _
As Boolean

Dim objSelection As Excel.Range

Try
If clsError.ErrorFlag() = True Then Exit Function


Dim icolumnfirst As Integer
Dim icolumnlast As Integer
Dim irowwfirst As Integer
Dim irowwlast As Integer

objSelection = CType(gApplicationExcel.Selection, Excel.Range)

icolumnfirst = objSelection.Column
icolumnlast = objSelection.Column + objSelection.Columns.Count - 1
irowwfirst = objSelection.Row
irowwlast = irowwfirst + objSelection.Rows.Count - 1

If icolumnfirst = icolumnlast And irowwfirst = irowwlast Then
If bInformUser = True Then
Call clszMessagesExcel.SelNoRangeSelectedInformation()
End If
RangeSelected = False
Else
RangeSelected = True
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objSelection = Nothing

If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("RangeSelected", "clsSel", _
"determines if there is currently a range of cells selected.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Selection_ReturnName

public static string SelectionNameReturn()
{
try
{
// an error occurs if there is nothing selected
SelectionNameReturn = gApplicationExcel.Selection.Name;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
SelectionNameReturn = "";
}
}
Public Shared Function SelectionNameReturn() As String

Try
'an error occurs if there is nothing selected
SelectionNameReturn = gApplicationExcel.Selection.Name

Catch objCOMException As System.Runtime.InteropServices.COMException

SelectionNameReturn = ""
End Try

End Function

Selection_ReturnTypeName

public static string ReturnSelectionTypeName()
{
try
{
if (clsError.ErrorFlag() == true)
return;

ReturnSelectionTypeName = TypeName(gApplicationExcel.Selection);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("ReturnSelectionTypeName", "clsSel", "return the description of the current selection.", mobjCOMException, mobjException);
}
}
Public Shared Function ReturnSelectionTypeName() As String

Try
If clsError.ErrorFlag() = True Then Exit Function

ReturnSelectionTypeName = TypeName(gApplicationExcel.Selection)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("ReturnSelectionTypeName", "clsSel", _
"return the description of the current selection.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Selection_ShapeRange

public static Excel.ShapeRange Selection_ShapeRange()
{
try
{
// an error occurs if there is nothing selected
SelectionShapeRange = gApplicationExcel.Selection.ShapeRange;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
SelectionShapeRange = null;
}
}
Public Shared Function Selection_ShapeRange() As Excel.ShapeRange

Try
'an error occurs if there is nothing selected
SelectionShapeRange = gApplicationExcel.Selection.ShapeRange

Catch objCOMException As System.Runtime.InteropServices.COMException

SelectionShapeRange = Nothing
End Try
End Function

Selection_TryPaste

public static void SelectionPaste()
{
try
{
gApplicationExcel.Selection.Paste();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
}
}
Public Shared Sub SelectionPaste()

Try
gApplicationExcel.Selection.Paste()

Catch objCOMException As System.Runtime.InteropServices.COMException

End Try

End Sub

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;
}
}
Public Shared Function Wbk_Active() As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim swbkname As String

swbkname = gApplicationExcel.ActiveWorkbook.Name
Active = True

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If ((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Active = False
End If
End Try
End Function

Wbk_Close

public static void Wbk_Close(bool bSave, string sWbkName = "", Windows.Forms.ListBox lstBoxName = null/* TODO Change to default(_) if this is not a reference type */, 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);
}
}
Public Shared Sub Wbk_Close(ByVal bSave As Boolean, _
Optional ByVal sWbkName As String = "", _
Optional ByVal lstBoxName As Windows.Forms.ListBox = Nothing, _
Optional ByVal bInformUser As Boolean = True)

Dim objWorkbook As Excel.Workbook

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sWbkName <> "" Then
objWorkbook = gApplicationExcel.Workbooks(sWbkName)
Else
objWorkbook = gApplicationExcel.ActiveWorkbook()
End If

If gApplicationExcel.Workbooks.Count = 0 Then Exit Sub

gApplicationExcel.StatusBar = "Closing the file : " & objWorkbook.Name & " ..."
If Not lstBoxName Is Nothing Then
lstBoxName.Text = "Closing the file : " & objWorkbook.Name & " ..."
'lstBoxName.Parent.Repaint()
End If

gApplicationExcel.ScreenUpdating = True
gApplicationExcel.DisplayAlerts = False
objWorkbook.Close(savechanges:=bSave)
gApplicationExcel.DisplayAlerts = True

gApplicationExcel.StatusBar = False
If Not lstBoxName Is Nothing Then
lstBoxName.Text = ""
'lstBoxName.Parent.Repaint()
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objWorkbook = Nothing

If ((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then
Call clsError.Handle("CloseWbk", "clsWbk", _
"close the workbook '" & sWbkName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Wbk_ColourPaletteDefine

public static void Wbk_ColourPaletteDefine()
{
Excel.Workbook objWorkbook;

try
{
if (clsError.ErrorFlag() == true)
return;

objWorkbook = gApplicationExcel.ActiveWorkbook;

// chart fill colours
objWorkbook.Colors(17) = Information.RGB(240, 76, 71);
objWorkbook.Colors(18) = Information.RGB(248, 148, 81);
objWorkbook.Colors(19) = Information.RGB(117, 0, 71);
objWorkbook.Colors(20) = Information.RGB(0, 171, 215);

// chart line colours
objWorkbook.Colors(25) = Information.RGB(240, 76, 71);
objWorkbook.Colors(26) = Information.RGB(248, 148, 81);
objWorkbook.Colors(27) = Information.RGB(117, 0, 71);
objWorkbook.Colors(28) = Information.RGB(0, 171, 215);
}
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 (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("ColourPaletteDefine", "clsWbk", "add the specific colours to the colour palette.", mobjCOMException, mobjException);
}
}
Public Shared Sub Wbk_ColourPaletteDefine()

Dim objWorkbook As Excel.Workbook

Try
If clsError.ErrorFlag() = True Then Exit Sub

objWorkbook = gApplicationExcel.ActiveWorkbook

'chart fill colours
objWorkbook.Colors(17) = RGB(240, 76, 71)
objWorkbook.Colors(18) = RGB(248, 148, 81)
objWorkbook.Colors(19) = RGB(117, 0, 71)
objWorkbook.Colors(20) = RGB(0, 171, 215)

'chart line colours
objWorkbook.Colors(25) = RGB(240, 76, 71)
objWorkbook.Colors(26) = RGB(248, 148, 81)
objWorkbook.Colors(27) = RGB(117, 0, 71)
objWorkbook.Colors(28) = RGB(0, 171, 215)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objWorkbook = Nothing

If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("ColourPaletteDefine", "clsWbk", _
"add the specific colours to the colour palette.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Wbk_GetFullName

public static string Wbk_GetFullName()
{
try
{
if (clsError.ErrorFlag() == true)
return;

FullNameGet = gApplicationExcel.ActiveWorkbook.FullName;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("FullNameGet", "clsWbk", "returns the fullname of the active presentation.", mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_GetFullName() As String

Try
If clsError.ErrorFlag() = True Then Exit Function

FullNameGet = gApplicationExcel.ActiveWorkbook.FullName

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("FullNameGet", "clsWbk", _
"returns the fullname of the active presentation.", _
mobjCOMException, mobjException)
End If
End Try
End Function

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, Windows.Forms.Label lblProgress = null/* TODO Change to default(_) if this is not a reference type */, bool bInformUser = true)
{
try
{
if (clsError.ErrorFlag() == true)
return;

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 (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
OpenWbk = false;

if (((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("OpenWbk", "clsWbk", "open the workbook called '" + sWbkName + sAdditional + sExtension + "'" + Constants.vbCrLf + "from the folder location" + Constants.vbCrLf + sFolderPath, mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_Open(ByVal sFolderPath As String, _
ByVal sWbkName As String, _
Optional ByVal iUpdateLinks As Integer = 0, _
Optional ByVal sAdditional As String = "", _
Optional ByVal sExtension As String = ".xlsx", _
Optional ByVal bReadOnly As Boolean = False, _
Optional ByVal lblProgress As Windows.Forms.Label = Nothing, _
Optional ByVal bInformUser As Boolean = True) As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

gApplicationExcel.StatusBar = "Opening the file : " & _
sFolderPath & sWbkName & sAdditional & sExtension & " ..."
If Not lblProgress Is Nothing Then
lblProgress.Text = "Opening the file : " & _
sFolderPath & sWbkName & sAdditional & sExtension & " ..."
'lblProgress.Parent.Repaint()
End If
gApplicationExcel.Workbooks.Open(FileName:=sFolderPath & sWbkName & sAdditional & sExtension, _
UpdateLinks:=iUpdateLinks, _
ReadOnly:=bReadOnly).RunAutoMacros(Excel.XlRunAutoMacro.xlAutoOpen)
OpenWbk = True
gApplicationExcel.StatusBar = False

If Not lblProgress Is Nothing Then
lblProgress.Text = ""
'lblProgress.Parent.Repaint()
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
OpenWbk = False

If ((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then
Call clsError.Handle("OpenWbk", "clsWbk", _
"open the workbook called '" & sWbkName & sAdditional & sExtension & "'" & _
vbCrLf & "from the folder location" & vbCrLf & sFolderPath, _
mobjCOMException, mobjException)
End If
End Try
End Function

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;

if (clsError.ErrorFlag() == true)
return;

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);
SaveAs = true;
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
gobjCOMException = objCOMException;
}
catch (Exception objException)
{
gobjException = objException;
}

finally
{
if (((IsNothing(gobjCOMException) == false | IsNothing(gobjException) == false)))
clsError.Handle("SaveAs", "clsWbk", "save the file \"" + sFileName + sExtension + " \"" + "in the directory" + Constants.vbCrLf + sFolderPath, gobjCOMException, gobjException);
}
}
Public Shared Function Wbk_SaveAs(ByVal sFolderPath As String, _
ByVal sFileName As String, _
Optional ByVal sExtension As String = ".xls", _
Optional ByVal bDeleteIfExists As Boolean = False, _
Optional ByVal bInformUser As Boolean = False, _
Optional ByVal sAdditional As String = "") As Boolean

Try
Dim breplace As Boolean

If clsError.ErrorFlag() = True Then Exit Function

SaveAs = False

If clsFile.Exists(sFolderPath, sFileName, sExtension, False) = True Then

If bInformUser = True Then
breplace = clsMessages.QuestionFileReplace(sFolderPath, sFileName)
End If

If breplace = True Then
If bDeleteIfExists = True Then
Call clsFile.Delete(sFolderPath, sFileName, sExtension)
End If

gApplicationExcel.ActiveWorkbook.SaveAs(FileName:=sFolderPath & sFileName & _
sAdditional & sExtension)
SaveAs = True
End If
Else
gApplicationExcel.ActiveWorkbook.SaveAs(FileName:=sFolderPath & sFileName & _
sAdditional & sExtension)
SaveAs = True
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If ((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then
Call clsError.Handle("SaveAs", "clsWbk", _
"save the file """ & sFileName & sExtension & " """ & _
"in the directory" & vbCrLf & sFolderPath, _
gobjCOMException, gobjException)
End If
End Try
End Function

Wbk_SelectWbk

public static void Wbk_SelectWbk(string sWbkName)
{
Excel.Workbook objWorkbook;

try
{
if (clsError.ErrorFlag() == true)
return;

objWorkbook = gApplicationExcel.Workbooks(sWbkName);

clszLateBindingExcel.clsWbkSelectWbk(objWorkbook);
}
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 (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("SelectWbk", "clsWbk", "", mobjCOMException, mobjException);
}
}

public static void Wbk_SelectWbk(Excel.Workbook objWorkbook)
{
try
{
if (clsError.ErrorFlag() == true)
return;

clszLateBindingExcel.clsWbkSelectWbk(objWorkbook);
}
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 (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("SelectWbk", "clsWbk", "", mobjCOMException, mobjException);
}
}
Public Shared Sub Wbk_SelectWbk(ByVal sWbkName As String)

Dim objWorkbook As Excel.Workbook

Try
If clsError.ErrorFlag() = True Then Exit Sub

objWorkbook = gApplicationExcel.Workbooks(sWbkName)

Call clszLateBindingExcel.clsWbkSelectWbk(objWorkbook)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objWorkbook = Nothing

If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("SelectWbk", "clsWbk", _
"", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Public Shared Sub Wbk_SelectWbk(ByVal objWorkbook As Excel.Workbook)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Call clszLateBindingExcel.clsWbkSelectWbk(objWorkbook)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objWorkbook = Nothing

If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("SelectWbk", "clsWbk", _
"", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Wbk_WshsAllToArray

public static string[] Wbk_WshsAllToArray(string sWbkName = "", string sSepearteChar = ";", string sIgnoreList = "")
{
Excel.Workbook objWorkbook;
Excel.Worksheet objWorksheet;

try
{
if (clsError.ErrorFlag() == true)
return;

string[] asworksheetnames;
int iworksheetcount;

if (Strings.Len(sWbkName) > 0)
{
objWorkbook = gApplicationExcel.Workbooks(sWbkName);
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}

asworksheetnames = new string[objWorkbook.Worksheets.Count - 1 + 1];

foreach (var objWorksheet in objWorkbook.Worksheets)
{
if (InStr(1, Strings.UCase(sIgnoreList), UCase(objWorksheet.Name)) == 0)
{
asworksheetnames[iworksheetcount] = objWorksheet.Name;
iworksheetcount = iworksheetcount + 1;
}
}

asworksheetnames = new string[iworksheetcount + 1];

WshsAllToArray = asworksheetnames;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("WshsAllToArray", "clsWbk", "return a concatenated string of all the worksheets in the active workbook", mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_WshsAllToArray(Optional ByVal sWbkName As String = "", _
Optional ByVal sSepearteChar As String = ";", _
Optional ByVal sIgnoreList As String = "") _
As String()

Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim asworksheetnames As String()
Dim iworksheetcount As Integer

If Len(sWbkName) > 0 Then
objWorkbook = gApplicationExcel.Workbooks(sWbkName)
Call clszLateBindingExcel.WorkbookSelect(objWorkbook)
End If

ReDim asworksheetnames(objWorkbook.Worksheets.Count - 1)

For Each objWorksheet In objWorkbook.Worksheets
If InStr(1, UCase(sIgnoreList), UCase(objWorksheet.Name)) = 0 Then
asworksheetnames(iworksheetcount) = objWorksheet.Name
iworksheetcount = iworksheetcount + 1
End If
Next objWorksheet

ReDim asworksheetnames(iworksheetcount)

WshsAllToArray = asworksheetnames

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("WshsAllToArray", "clsWbk", _
"return a concatenated string of all the worksheets in the active workbook", _
mobjCOMException, mobjException)
End If
End Try

End Function

Wbk_WshsAllToString

public static string Wbk_WshsAllToString(string sWbkName = "", string sSepearteChar = ";", string sIgnoreList = "")
{
Excel.Workbook objWorkbook;
Excel.Worksheet objWorksheet;

try
{
if (clsError.ErrorFlag() == true)
return;

string sallwshs;

if (Strings.Len(sWbkName) > 0)
{
objWorkbook = gApplicationExcel.Workbooks(sWbkName);
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}
sallwshs = "";

foreach (var objWorksheet in gApplicationExcel.ActiveWorkbook.Worksheets)
{
if (InStr(1, Strings.UCase(sIgnoreList), UCase(objWorksheet.Name)) == 0)
sallwshs = sallwshs + sSepearteChar + objWorksheet.Name;
}

WshsAllToString = Strings.Right(sallwshs, Strings.Len(sallwshs) - 1);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("WshsAllToString", "clsWbk", "return a concatenated string of all the worksheets in the active workbook", mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_WshsAllToString(Optional ByVal sWbkName As String = "", _
Optional ByVal sSepearteChar As String = ";", _
Optional ByVal sIgnoreList As String = "") _
As String

Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim sallwshs As String

If Len(sWbkName) > 0 Then
objWorkbook = gApplicationExcel.Workbooks(sWbkName)
Call clszLateBindingExcel.WorkbookSelect(objWorkbook)
End If
sallwshs = ""

For Each objWorksheet In gApplicationExcel.ActiveWorkbook.Worksheets

If InStr(1, UCase(sIgnoreList), UCase(objWorksheet.Name)) = 0 Then
sallwshs = sallwshs & sSepearteChar & objWorksheet.Name
End If

Next objWorksheet

WshsAllToString = Right(sallwshs, Len(sallwshs) - 1)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("WshsAllToString", "clsWbk", _
"return a concatenated string of all the worksheets in the active workbook", _
mobjCOMException, mobjException)
End If
End Try
End Function

Wbk_WshsSelectedToArray

public static string[] Wbk_WshsSelectedToArray()
{
Excel.Worksheet objworksheet;

try
{
if (clsError.ErrorFlag() == true)
return;

string[] asworksheetnames;
int iworksheetcount;

asworksheetnames = new string[gApplicationExcel.ActiveWindow.SelectedSheets.Count - 1 + 1];

foreach (var objworksheet in gApplicationExcel.ActiveWindow.SelectedSheets)
{
asworksheetnames[iworksheetcount] = objworksheet.Name;
iworksheetcount = iworksheetcount + 1;
}

WshsSelectedToArray = asworksheetnames;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
objworksheet = null/* TODO Change to default(_) if this is not a reference type */;

if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("WshsSelectedToArray", "clsWsh", "return an array containing " + "the names of all the worksheets that are currently selected.", mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_WshsSelectedToArray() As String()

Dim objworksheet As Excel.Worksheet

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim asworksheetnames As String()
Dim iworksheetcount As Integer

ReDim asworksheetnames(gApplicationExcel.ActiveWindow.SelectedSheets.Count - 1)

For Each objworksheet In gApplicationExcel.ActiveWindow.SelectedSheets
asworksheetnames(iworksheetcount) = objworksheet.Name
iworksheetcount = iworksheetcount + 1
Next

WshsSelectedToArray = asworksheetnames

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objworksheet = Nothing

If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("WshsSelectedToArray", "clsWsh", _
"return an array containing " & _
"the names of all the worksheets that are currently selected.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Wbk_WshsSelectedToString

public static string Wbk_WshsSelectedToString(string sWbkName = "", string sSepearteChar = ";")
{
Excel.Workbook objWorkbook;
Excel.Worksheet objWorksheet;

try
{
if (clsError.ErrorFlag() == true)
return;

string sallwshs;

if (Strings.Len(sWbkName) > 0)
{
objWorkbook = gApplicationExcel.Workbooks(sWbkName);
clszLateBindingExcel.WorkbookSelect(objWorkbook);
}
sallwshs = "";

foreach (var objWorksheet in gApplicationExcel.ActiveWindow.SelectedSheets)
sallwshs = sallwshs + sSepearteChar + objWorksheet.Name;

WshsSelectedToString = sallwshs.Substring(0, sallwshs.Length - 1);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}

finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
clsError.Handle("WshsAllToString", "clsWbk", "return a concatenated string of all the worksheets in the active workbook", mobjCOMException, mobjException);
}
}
Public Shared Function Wbk_WshsSelectedToString(Optional ByVal sWbkName As String = "", _
Optional ByVal sSepearteChar As String = ";") _
As String

Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim sallwshs As String

If Len(sWbkName) > 0 Then
objWorkbook = gApplicationExcel.Workbooks(sWbkName)
Call clszLateBindingExcel.WorkbookSelect(objWorkbook)
End If
sallwshs = ""

For Each objWorksheet In gApplicationExcel.ActiveWindow.SelectedSheets
sallwshs = sallwshs & sSepearteChar & objWorksheet.Name
Next objWorksheet

WshsSelectedToString = sallwshs.Substring(0, sallwshs.Length - 1)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_EXCEL = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("WshsAllToString", "clsWbk", _
"return a concatenated string of all the worksheets in the active workbook", _
mobjCOMException, mobjException)
End If
End Try

End Function

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top