C# Snippets
Message_WorksheetAlreadyExists
public static void Message_WorksheetAlreadyExists(string sWshName)
{
System.Windows.Forms.MessageBox.Show("The worksheet '" + sWshName + "'" + " already exists in this workbook.", gsDIALOG_PREFIX_EXCEL, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information);
}
Public Shared Sub Message_WorksheetAlreadyExists(ByVal sWshName As String)
Call System.Windows.Forms.MessageBox.Show( _
"The worksheet '" & sWshName & "'" & _
" already exists in this workbook.", _
gsDIALOG_PREFIX_EXCEL, _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)
End Sub
Wsh_AddNew
public static void AddNew(string sWshName, string sAfterWshName = "", string sBeforeWshName = "", Excel.XlSheetVisibility objSheetHidden = Excel.XlSheetVisibility.xlSheetVisible)
{
Excel.Workbook objworkbook;
Excel.Worksheet objworksheet;
try
{
if (clsError.ErrorFlag() == true)
return;
string sactivewsh;
if (clsWsh.Exists(sWshName) == -1)
{
objworkbook = gApplicationExcel.ActiveWorkbook;
sactivewsh = (Excel.Worksheet)objworkbook.ActiveSheet.Name;
gApplicationExcel.ScreenUpdating = false;
objworkbook.Sheets.Add();
objworksheet = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
objworksheet.Name = sWshName;
if ((sBeforeWshName.Length > 0))
objworksheet.Move(before: gApplicationExcel.Sheets(sBeforeWshName));
if ((sAfterWshName.Length > 0))
objworksheet.Move(After: gApplicationExcel.Sheets(sAfterWshName));
// then inserts the new worksheet at the end of all the existing worksheets
if ((sBeforeWshName.Length == 0) & (sAfterWshName.Length == 0))
objworksheet.Move(After: gApplicationExcel.Sheets(objworkbook.Worksheets.Count));
objworksheet.Visible = objSheetHidden;
(Excel.Worksheet)objworkbook.Worksheets(sactivewsh).Select();
gApplicationExcel.ScreenUpdating = true;
}
else
clszMessagesExcel.WorksheetAlreadyExistsInformation(sWshName);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
gApplicationExcel.ScreenUpdating = true;
objworkbook = null/* TODO Change to default(_) if this is not a reference type */;
if (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("AddNew", msCLASSNAME, "add the new worksheet '" + sWshName + "' " + "after the worksheet '" + sAfterWshName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub AddNew(ByVal sWshName As String, _
Optional ByVal sAfterWshName As String = "", _
Optional ByVal sBeforeWshName As String = "", _
Optional ByVal objSheetHidden As Excel.XlSheetVisibility = _
Excel.XlSheetVisibility.xlSheetVisible)
Dim objworkbook As Excel.Workbook
Dim objworksheet As Excel.Worksheet
Try
If clsError.ErrorFlag() = True Then Exit Sub
Dim sactivewsh As String
If clsWsh.Exists(sWshName) = -1 Then
objworkbook = gApplicationExcel.ActiveWorkbook
sactivewsh = CType(objworkbook.ActiveSheet, Excel.Worksheet).Name
gApplicationExcel.ScreenUpdating = False
objworkbook.Sheets.Add()
objworksheet = CType(gApplicationExcel.ActiveSheet, Excel.Worksheet)
objworksheet.Name = sWshName
If (sBeforeWshName.Length > 0) Then
objworksheet.Move(before:=gApplicationExcel.Sheets(sBeforeWshName))
End If
If (sAfterWshName.Length > 0) Then
objworksheet.Move(After:=gApplicationExcel.Sheets(sAfterWshName))
End If
'then inserts the new worksheet at the end of all the existing worksheets
If (sBeforeWshName.Length = 0) And (sAfterWshName.Length = 0) Then
objworksheet.Move(After:=gApplicationExcel.Sheets(objworkbook.Worksheets.Count))
End If
objworksheet.Visible = objSheetHidden
CType(objworkbook.Worksheets(sactivewsh), Excel.Worksheet).Select()
gApplicationExcel.ScreenUpdating = True
Else
Call clszMessagesExcel.WorksheetAlreadyExistsInformation(sWshName)
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
gApplicationExcel.ScreenUpdating = True
objworkbook = Nothing
If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("AddNew", msCLASSNAME, _
"add the new worksheet '" & sWshName & "' " & _
"after the worksheet '" & sAfterWshName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Wsh_Delete
public static void Wsh_Delete(string sWshName, bool bDisplayAlerts = true)
{
try
{
if (clsError.ErrorFlag() == true)
return;
Excel.Workbook objworkbook;
Excel.Worksheet objworksheet;
gApplicationExcel.DisplayAlerts = bDisplayAlerts;
objworkbook = gApplicationExcel.ActiveWorkbook;
objworksheet = (Excel.Worksheet)objworkbook.ActiveSheet;
objworksheet.Delete();
gApplicationExcel.DisplayAlerts = true;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("Delete", msCLASSNAME, "delete the worksheet '" + sWshName + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub Delete(ByVal sWshName As String, _
Optional ByVal bDisplayAlerts As Boolean = True)
Try
If clsError.ErrorFlag() = True Then Exit Sub
Dim objworkbook As Excel.Workbook
Dim objworksheet As Excel.Worksheet
gApplicationExcel.DisplayAlerts = bDisplayAlerts
objworkbook = gApplicationExcel.ActiveWorkbook
objworksheet = CType(objworkbook.ActiveSheet, Excel.Worksheet)
objworksheet.Delete()
gApplicationExcel.DisplayAlerts = True
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("Delete", msCLASSNAME, _
"delete the worksheet '" & sWshName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Wsh_DeleteAllExcept
public static void Wsh_DeleteAllExcept(string sWshNamesToKeep, string sSeperateChar = ";", bool bDisplayAlerts = true, string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
Excel.Workbook objworkbook;
Excel.Workbook objworkbookreturn;
Excel.Worksheet objworksheet;
Excel.Worksheet objworksheetreturn;
string swshnametokeep;
if (sSeperateChar == "")
sSeperateChar = Microsoft.VisualBasic.Chr(10);
if (sWbkName.Length > 0)
{
objworkbookreturn = gApplicationExcel.ActiveWorkbook;
(Excel._Workbook)objworkbookreturn.Activate();
}
objworksheetreturn = (Excel.Worksheet)objworkbook.ActiveSheet;
gApplicationExcel.DisplayAlerts = false;
foreach (var objworksheet in objworkbook.Worksheets)
{
if (clsWsh.NameIsInList(objworksheet.Name, sWshNamesToKeep) == false)
objworksheet.Delete();
}
gApplicationExcel.DisplayAlerts = bDisplayAlerts;
objworksheetreturn.Select();
if (sWbkName.Length > 0)
(Excel._Workbook)objworkbookreturn.Activate();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("DeleteAllExcept", msCLASSNAME, "delete all the workbooks except the ones in the list.", mobjCOMException, mobjException);
}
}
Public Shared Sub DeleteAllExcept(ByVal sWshNamesToKeep As String, _
Optional ByVal sSeperateChar As String = ";", _
Optional ByVal bDisplayAlerts As Boolean = True, _
Optional ByVal sWbkName As String = "")
Try
If clsError.ErrorFlag() = True Then Exit Sub
Dim objworkbook As Excel.Workbook
Dim objworkbookreturn As Excel.Workbook
Dim objworksheet As Excel.Worksheet
Dim objworksheetreturn As Excel.Worksheet
Dim swshnametokeep As String
If sSeperateChar = "" Then sSeperateChar = Microsoft.VisualBasic.Chr(10)
If sWbkName.Length > 0 Then
objworkbookreturn = gApplicationExcel.ActiveWorkbook
CType(objworkbookreturn, Excel._Workbook).Activate()
End If
objworksheetreturn = CType(objworkbook.ActiveSheet, Excel.Worksheet)
gApplicationExcel.DisplayAlerts = False
For Each objworksheet In objworkbook.Worksheets
If clsWsh.NameIsInList(objworksheet.Name, sWshNamesToKeep) = False Then
objworksheet.Delete()
End If
Next objworksheet
gApplicationExcel.DisplayAlerts = bDisplayAlerts
objworksheetreturn.Select()
If sWbkName.Length > 0 Then
CType(objworkbookreturn, Excel._Workbook).Activate()
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("DeleteAllExcept", msCLASSNAME, _
"delete all the workbooks except the ones in the list.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Wsh_Exists
public static bool Wsh_Exists(
Excel.Workbook workbook,
string sheetName)
{
try
{
if (workbook == null || sheetName == null)
{
return false;
}
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
// sheet = workbook.Sheets[i];
if (sheet == null || sheet.Name == null)
continue;
if (sheet.Name.ToLower().Equals(sheetName.ToLower()))
{
return true;
}
}
return false;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return false;
}
}
Public Shared Function Wsh_Exists(ByVal sWshName As String, _
Optional ByVal bInformUser As Boolean = False) _
As Integer
Try
If clsError.ErrorFlag() = True Then Exit Function
Dim objworkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim bexists As Boolean
Dim iwshcount As Integer
Dim iwshcounter As Integer
objworkbook = gApplicationExcel.ActiveWorkbook
bexists = False
iwshcounter = -1
For iwshcount = 1 To objworkbook.Worksheets.Count
objWorksheet = CType(objworkbook.Worksheets(iwshcount), Excel.Worksheet)
If sWshName = objWorksheet.Name Then bexists = True
If sWshName = objWorksheet.Name Then iwshcounter = 0
Next iwshcount
If bexists = False Then
' For iwshcount = 1 To ActiveWorkbook.Worksheets.Count
' If InStr(Worksheets(iwshcount).Name, sWshName) > 0 Then
' iwshcounter = iwshcounter + 1
' Else
' iwshcounter = -1
' End If
' Next iwshcount
Else
If bInformUser = True Then
Call clszMessagesExcel.WorksheetAlreadyExistsInformation(sWshName)
End If
End If
Exists = iwshcounter
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("Exists", "clsWsh", _
"determine if the worksheet name '" & sWshName & "' " & _
"already exists in the active workbook.", _
mobjCOMException, mobjException)
End If
End Try
End Function
Wsh_GetNameOfActive
public static string Wsh_GetNameOfActive()
{
Excel.Workbook objworkbook;
Excel.Worksheet objworksheet;
try
{
if (clsError.ErrorFlag() == true)
return;
objworkbook = gApplicationExcel.ActiveWorkbook;
objworksheet = (Excel.Worksheet)gApplicationExcel.ActiveSheet;
ActiveName = objworksheet.Name;
}
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("ActiveName", "clsWsh", "returns the name of the currently active worksheet.", mobjCOMException, mobjException);
}
}
Public Shared Function Wsh_GetNameOfActive() As String
Dim objworkbook As Excel.Workbook
Dim objworksheet As Excel.Worksheet
Try
If clsError.ErrorFlag() = True Then Exit Function
objworkbook = gApplicationExcel.ActiveWorkbook
objworksheet = CType(gApplicationExcel.ActiveSheet, Excel.Worksheet)
ActiveName = objworksheet.Name
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("ActiveName", "clsWsh", _
"returns the name of the currently active worksheet.", _
mobjCOMException, mobjException)
End If
End Try
End Function
Wsh_GetSheetByName
public static Excel.Worksheet getSheetByName(Excel.Workbook workbook, string sheetName)
{
try
{
if (workbook == null || sheetName == null)
{
return null;
}
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
// sheet = workbook.Sheets[i];
if (sheet == null || sheet.Name == null)
continue;
if (sheet.Name.Equals(sheetName))
{
return sheet;
}
}
return null;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return null;
}
}
Wsh_Hide
public void HideWsh(string sWshName, bool bShowIt)
{
Excel.Workbook objworkbook;
Excel.Worksheet objworksheet;
try
{
if (clsError.ErrorFlag() == true)
return;
objworkbook = gApplicationExcel.ActiveWorkbook;
objworksheet = (Excel.Worksheet)objworkbook.Worksheets(sWshName);
if (bShowIt == true & objworksheet.Visible == Excel.XlSheetVisibility.xlSheetHidden)
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;
else if (bShowIt == false & objworksheet.Visible == Excel.XlSheetVisibility.xlSheetVisible)
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
else
{
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true | ((IsNothing(mobjCOMException) == false | IsNothing(mobjException) == false)))
{
string serrortext;
if (bShowIt == true)
serrortext = "Show ";
if (bShowIt == false)
serrortext = "Hide ";
clsError.Handle("HideWsh", "clsWsh", serrortext + "the worksheet called '" + sWshName + "'.", mobjCOMException, mobjException);
}
}
}
Public Sub HideWsh(ByVal sWshName As String, _
ByVal bShowIt As Boolean)
Dim objworkbook As Excel.Workbook
Dim objworksheet As Excel.Worksheet
Try
If clsError.ErrorFlag() = True Then Exit Sub
objworkbook = gApplicationExcel.ActiveWorkbook
objworksheet = CType(objworkbook.Worksheets(sWshName), Excel.Worksheet)
If bShowIt = True And _
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetHidden Then
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetVisible
ElseIf bShowIt = False And _
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetVisible Then
objworksheet.Visible = Excel.XlSheetVisibility.xlSheetHidden
Else 'don't do anything
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
Dim serrortext As String
If bShowIt = True Then serrortext = "Show "
If bShowIt = False Then serrortext = "Hide "
Call clsError.Handle("HideWsh", "clsWsh", _
serrortext & "the worksheet called '" & sWshName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Wsh_Insert
public static Excel.Worksheet Wsh_Insert(
Excel.Application excelApp,
Excel.Workbook workbook,
string sheetName = null,
string beforeSheet = null,
string afterSheet = null,
Constants.OrientationStyle orientationStyle = Constants.OrientationStyle.NONE)
{
try
{
if (excelApp == null)
{
return null;
}
// Create a new empty workbook in a new workbook set.
//Excel.Workbook workbook = excelApp.ActiveWorkbook;
Excel.Worksheet worksheet = null;
// Add a new worksheet
if (workbook == null)
{
return null;
}
if (sheetName != null)
{
if (sheetExists(workbook, sheetName))
{
worksheet = workbook.Worksheets[sheetName];
clearSheetContent(worksheet);
return workbook.Worksheets[sheetName];
}
}
worksheet = workbook.Worksheets.Add();
if (sheetName != null)
{
worksheet.Name = sheetName;
}
excelApp.DisplayAlerts = true;
Wbk_SaveWorkbook(workbook);
excelApp.DisplayAlerts = false;
if (beforeSheet != null && sheetExists(workbook, beforeSheet))
{
worksheet.Move(workbook.Sheets[beforeSheet], System.Type.Missing);
}
if (afterSheet != null && sheetExists(workbook, afterSheet))
{
worksheet.Move(System.Type.Missing, workbook.Sheets[afterSheet]);
}
setOrientation(worksheet, orientationStyle);
return worksheet;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return null;
}
}
Wsh_InsertWorksheetAtBeginning
public static Excel.Worksheet insertWorksheetAtBeginning(
Excel.Application excelApp,
string sheetName,
Constants.OrientationStyle orientationStyle = Constants.OrientationStyle.NONE)
{
try
{
Excel.Workbook workbook = excelApp.ActiveWorkbook;
Excel.Worksheet worksheet = null;
// Add a new worksheet
if (workbook == null || sheetName == null)
{
return null;
}
if (sheetName != null)
{
if (sheetExists(excelApp.ActiveWorkbook, sheetName))
{
worksheet = excelApp.ActiveWorkbook.Worksheets[sheetName];
clearSheetContent(worksheet);
worksheet.Move(workbook.Sheets[1], System.Type.Missing);
return worksheet;
}
}
worksheet = workbook.Worksheets.Add();
setOrientation(worksheet, orientationStyle);
if (sheetName != null)
{
worksheet.Name = sheetName;
}
worksheet.Move(workbook.Sheets[1], System.Type.Missing);
return worksheet;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return null;
}
}
Wsh_InsertWorksheetAtEnd
public static Excel.Worksheet insertWorksheetAtEnd(
Excel.Application excelApp,
string sheetName,
Constants.OrientationStyle orientationStyle = Constants.OrientationStyle.NONE)
{
try
{
Excel.Workbook workbook = excelApp.ActiveWorkbook;
Excel.Worksheet worksheet = null;
// Add a new worksheet
if (workbook == null || sheetName == null)
{
return null;
}
if (sheetName != null)
{
if (sheetExists(excelApp.ActiveWorkbook, sheetName))
{
worksheet = excelApp.ActiveWorkbook.Worksheets[sheetName];
CommonUtil.WorksheetUtil.unprotectWorksheet(excelApp.ActiveWorkbook, sheetName);
clearSheetContent(worksheet);
worksheet.Move(System.Type.Missing, workbook.Sheets[workbook.Sheets.Count]);
return worksheet;
}
}
worksheet = workbook.Worksheets.Add();
setOrientation(worksheet, orientationStyle);
if (sheetName != null)
{
worksheet.Name = sheetName;
}
worksheet.Move(System.Type.Missing, workbook.Sheets[workbook.Sheets.Count]);
return worksheet;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return null;
}
}
Wsh_IsSheetVisible
public static bool sheetVisible(
Excel.Workbook workbook,
string sheetName)
{
try
{
if (workbook == null || sheetName == null)
{
return false;
}
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
if (sheet == null || sheet.Name == null)
continue;
if (sheet.Name.ToLower().Equals(sheetName.ToLower()))
{
return (sheet.Visible == Excel.XlSheetVisibility.xlSheetVisible);
}
}
return false;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return false;
}
}
Wsh_NameIsInList
public static bool NameIsInList(string sWshName, string sWshConcatenation, string sSeperateChar = ";", string sWbkName = "")
{
try
{
if (clsError.ErrorFlag() == true)
return;
string swshnametokeep;
string stemporary;
NameIsInList = false;
stemporary = sWshConcatenation;
while (stemporary.Length > 0)
{
if (stemporary.IndexOf(sSeperateChar) > -1)
swshnametokeep = stemporary.Substring(0, stemporary.IndexOf(sSeperateChar));
else
{
swshnametokeep = stemporary;
stemporary = "";
}
if (sWshName == swshnametokeep)
{
NameIsInList = true;
return;
}
stemporary = stemporary.Substring(stemporary.IndexOf(sSeperateChar) + 1);
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_EXCEL == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("NameIsInList", msCLASSNAME, "determine if the worksheet name '" + sWshName + "' " + "is in the following concatenated list:" + gsCRLF + gsCRLF + sWshConcatenation, mobjCOMException, mobjException);
}
}
Public Shared Function NameIsInList(ByVal sWshName As String, _
ByVal sWshConcatenation As String, _
Optional ByVal sSeperateChar As String = ";", _
Optional ByVal sWbkName As String = "") _
As Boolean
Try
If clsError.ErrorFlag() = True Then Exit Function
Dim swshnametokeep As String
Dim stemporary As String
NameIsInList = False
stemporary = sWshConcatenation
Do While stemporary.Length > 0
If stemporary.IndexOf(sSeperateChar) > -1 Then
swshnametokeep = stemporary.Substring(0, stemporary.IndexOf(sSeperateChar))
Else
swshnametokeep = stemporary
stemporary = ""
End If
If sWshName = swshnametokeep Then
NameIsInList = True
Exit Function
End If
stemporary = stemporary.Substring(stemporary.IndexOf(sSeperateChar) + 1)
Loop
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_EXCEL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("NameIsInList", msCLASSNAME, _
"determine if the worksheet name '" & sWshName & "' " & _
"is in the following concatenated list:" & _
gsCRLF & gsCRLF & sWshConcatenation, _
mobjCOMException, mobjException)
End If
End Try
End Function
Wsh_Paste
public static void WorksheetPaste(Excel.Worksheet objWorksheet)
{
try
{
objWorksheet.Paste();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
}
}
Public Shared Sub WorksheetPaste(ByVal objWorksheet As Excel.Worksheet)
Try
objWorksheet.Paste()
Catch objCOMException As System.Runtime.InteropServices.COMException
End Try
End Sub
Wsh_Protect
public static bool protectWorksheet(
Excel.Workbook workbook,
string sheetName)
{
try
{
//Excel.Worksheet worksheet;
//worksheet = getSheetByName(workbook, sheetName);
//worksheet.Protect(Password: "mypassword");
return true;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return false;
}
}
Wsh_Select
public static void Wsh_Select(string sWshName)
{
Excel.Workbook objworkbook;
Excel.Worksheet objworksheet;
try
{
if (clsError.ErrorFlag() == true)
return;
objworkbook = gApplicationExcel.ActiveWorkbook;
objworksheet = (Excel.Worksheet)objworkbook.Worksheets(sWshName);
objworksheet.Select();
}
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("Wsh_Select", "clsWsh", "select the worksheet called '" + sWshName + "' in the active workbook.", mobjCOMException, mobjException);
}
}
Public Shared Sub Wsh_Select(ByVal sWshName As String)
Dim objworkbook As Excel.Workbook
Dim objworksheet As Excel.Worksheet
Try
If clsError.ErrorFlag() = True Then Exit Sub
objworkbook = gApplicationExcel.ActiveWorkbook
objworksheet = CType(objworkbook.Worksheets(sWshName), Excel.Worksheet)
objworksheet.Select()
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("Wsh_Select", "clsWsh", _
"select the worksheet called '" & sWshName & "' in the active workbook.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
Wsh_UnProtect
public static bool unprotectWorksheet(
Excel.Workbook workbook,
string sheetName)
{
try
{
//Excel.Worksheet worksheet;
//worksheet = getSheetByName(workbook, sheetName);
//worksheet.Unprotect(Password: "london");
return true;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return false;
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top