Code Snippets


Error_Flag

public static bool Error_Flag()
{
try
{
Microsoft.Win32.RegistryKey regKey;
object objObject;
bool bValue;

regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software");
regKey = regKey.OpenSubKey("VB and VBA Program Settings");
regKey = regKey.OpenSubKey("OfficeSolutions");
regKey = regKey.OpenSubKey(gsOFFICE_SOLUTION_GENERAL);

if (regKey == null)
{
return false;
}
else
{
objObject = regKey.GetValue("Error Flag");

if (objObject != null)
{
bValue = (bool)objObject;
return bValue;
}
else
{
// Major problem according to VB comment
return false;
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return false;
}
catch (Exception ex)
{
gobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"ErrorFlag",
"clsError",
"return the error flag from the registry.",
gobjCOMException,
gobjException);
}
}
}

Error_FlagChange

public static void Error_FlagChange(
bool bValue)
{
try
{
Microsoft.Win32.RegistryKey regKey;

regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software");
regKey = regKey.OpenSubKey("VB and VBA Program Settings");
if (regKey == null)
{
clsMessages.RegistryEntryHasBeenDeletedExclamation("VB and VBA Program Settings");
return;
}

regKey = regKey.OpenSubKey("OfficeSolutions");
if (regKey == null)
{
clsMessages.RegistryEntryHasBeenDeletedExclamation("OfficeSolutions");
return;
}

regKey = regKey.OpenSubKey(gsOFFICE_SOLUTION_GENERAL, true);
if (regKey == null)
{
clsMessages.RegistryEntryHasBeenDeletedExclamation(gsOFFICE_SOLUTION_GENERAL);
return;
}

regKey.SetValue("Error Flag", bValue);
gbEND_GENERAL = bValue;
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"ErrorFlagChange",
"clsError",
"change the error flag to the value: '" + bValue + "'.",
gobjCOMException,
gobjException);
}
}
}

Error_ListBoxUpdateText

public void Error_ListBoxUpdateText(
ref System.Windows.Forms.ListBox objListBox,
int iCounter,
string sMessage)
{
string sItemNo;

try
{
if (sMessage.Length > 0)
{
// VB: Space(2 - Len(CStr(iCounter)))
sItemNo = new string(' ', 2 - iCounter.ToString().Length) + iCounter;

if (sMessage.Length >= 5 && sMessage.Substring(0, 5) != "ERROR")
{
objListBox.Items.Insert(
0,
System.DateTime.Now.ToString("hh:mm:ss") + " - " + sMessage);
}
else
{
objListBox.Items.Insert(0, sMessage);
}
}
else
{
objListBox.Items.Insert(0, "");
}

objListBox.Refresh();
objListBox.Parent.Refresh();
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);
}
}

Error_New

public void Error_New(
string sSolutionName,
string sOfficeSolution,
string sVersion)
{
try
{
gsSOLUTION_NAME_GENERAL = sSolutionName;
gsOFFICE_SOLUTION_GENERAL = sOfficeSolution;
gsVERSION_GENERAL = sVersion;

gbDEBUG_GENERAL = clsError.DebugFlag("Debug General");
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"New",
"clsError",
"initialise the new 'clsError' object.",
gobjCOMException,
gobjException);
}
}
}

Exception

public static void Exception(
System.Reflection.MethodBase methodBase,
ref System.Runtime.InteropServices.COMException comEx,
ref System.Exception exObj,
string userMessage = "",
bool informUser = true,
bool unableTo = true,
string tracerCategory = "",
string captionSuffix = "",
System.Windows.Forms.MessageBoxIcon icon = System.Windows.Forms.MessageBoxIcon.Stop)
{
string subName = "";
string declaringType = "";
string className = "";
string exceptionMessage = "";
string innerException = "";
string stackTrace = "";

try
{
if (Properties.Settings.Default.ERROR_OCCURRED)
return;

if (modGlobals.garDisplayedWindowsForms != null && informUser)
modGlobals.garDisplayedWindowsForms.Clear();

subName = methodBase.Name;
if (subName == ".ctor") subName = "Constructor";

declaringType = methodBase.DeclaringType.ToString();
className = declaringType.Substring(declaringType.IndexOf('.') + 1);

if (comEx != null)
{
exceptionMessage = "COM EXCEPTION - " + comEx.Message + " (" + comEx.Source + ")";
if (comEx.InnerException != null)
innerException = comEx.InnerException.Message;
stackTrace = comEx.StackTrace;
}

if (exObj != null)
{
exceptionMessage = exObj.Message + " (" + exObj.Source + ")";
if (exObj.InnerException != null)
innerException = exObj.InnerException.Message;
stackTrace = exObj.StackTrace;
}

if (userMessage.Length > 0 && unableTo)
userMessage = "Unable to " + userMessage;

if (tracerCategory.Length == 0)
{
if (icon.ToString() == "Hand") tracerCategory = "EXCEPTION";
if (icon.ToString() == "Warning") tracerCategory = "WARNING";
}

tracerCategory = informUser
? tracerCategory + "_MESSAGE"
: tracerCategory + "_SUPPRESSED";

Tracer_Add2("", "", true);
Tracer_Add2(tracerCategory, "", true);
Tracer_Add2("", className + " - " + subName, true);

if (userMessage.Length > 0) Tracer_Add2("", userMessage, true);
if (exceptionMessage.Length > 0) Tracer_Add2("", exceptionMessage, true);
if (innerException.Length > 0) Tracer_Add2("", innerException, true);
if (stackTrace.Length > 0) Tracer_Add2("", stackTrace, true);

Tracer_Add2("", "", true);

if (captionSuffix.Length == 0)
captionSuffix = "ADDIN (" + modConstants.gsVERSION_VSRELEASE + ") " + className + " - " + subName;
else
captionSuffix = "ADDIN (" + modConstants.gsVERSION_VSRELEASE + ") - " + captionSuffix;

if (informUser)
{
modGlobals.gbProgressBar_Running = false;
System.Windows.Forms.Application.DoEvents();

if (userMessage.Length > 0)
{
System.Windows.Forms.MessageBox.Show(
userMessage + Environment.NewLine + Environment.NewLine + exceptionMessage,
captionSuffix,
System.Windows.Forms.MessageBoxButtons.OK,
icon);
}
else
{
System.Windows.Forms.MessageBox.Show(
exceptionMessage,
captionSuffix,
System.Windows.Forms.MessageBoxButtons.OK,
icon);
}

if (icon == System.Windows.Forms.MessageBoxIcon.Error)
{
Properties.Settings.Default.ERROR_OCCURRED = true;
Properties.Settings.Default.Save();
}
}

comEx = null;
exObj = null;
}
catch (System.Exception ex)
{
string msg =
"A serious error has occurred - please send an email to us." + Environment.NewLine + Environment.NewLine +
"Message : " + ex.Message + Environment.NewLine + Environment.NewLine +
"Inner Exception : " + ex.InnerException + Environment.NewLine + Environment.NewLine +
"Source : " + ex.Source + Environment.NewLine + Environment.NewLine +
"Stack Trace : " + ex.StackTrace;

Tracer_Add2("", "", true);
Tracer_Add2("EXCEPTION", "", true);
Tracer_Add2("", msg.Replace(Environment.NewLine, " ").Replace(" ", " "), true);
Tracer_Add2("", "", true);

System.Windows.Forms.MessageBox.Show(
msg,
"modMessages - Exception",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
}

Exception_Warning

public void Exception_Warning(
System.Reflection.MethodBase objMethodBase,
string sUserMessage = "",
string sCaptionSuffix = "",
System.Windows.Forms.MessageBoxIcon objMessageBoxIcon = System.Windows.Forms.MessageBoxIcon.Warning,
bool bInformUser = true)
{
string sExceptionMessage = "";

try
{
modMessages.Exception(
objMethodBase,
null,
null,
sUserMessage,
bInformUser,
false,
"WARNING",
sCaptionSuffix,
objMessageBoxIcon);
}
catch (Exception ex)
{
sExceptionMessage =
"A serious error has occurred - please send an email to us" +
System.Environment.NewLine + System.Environment.NewLine +
"Message : " + ex.Message +
System.Environment.NewLine + System.Environment.NewLine +
"Inner Exception : " + ex.InnerException.ToString() +
System.Environment.NewLine + System.Environment.NewLine +
"Source : " + ex.Source +
System.Environment.NewLine + System.Environment.NewLine +
"Stack Trace : " + ex.StackTrace;

Tracer_Add2("", "", true);
Tracer_Add2("EXCEPTION", "", true);
Tracer_Add2("", sExceptionMessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "), true);
Tracer_Add2("", "", true);

System.Windows.Forms.MessageBox.Show(
sExceptionMessage,
"modMessages - Warning",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
}

Exception_WebPage

public void Exception_WebPage(
en_EXCEPTION_WEBPAGE enErrorType,
System.Reflection.MethodBase objMethodBase,
Exception objException,
string sWebPageAddress)
{
try
{
switch (enErrorType)
{
case en_EXCEPTION_WEBPAGE.zUnavailable:
WebPage_Unavailable(
objMethodBase,
objException,
sWebPageAddress);
break;

case en_EXCEPTION_WEBPAGE.zUnauthorized401:
WebPage_Unauthorized401(
objMethodBase,
objException,
sWebPageAddress);
break;

case en_EXCEPTION_WEBPAGE.zOther:
WebPage_GeneralError(
objMethodBase,
objException,
sWebPageAddress);
break;

default:
WebPage_GeneralError(
objMethodBase,
objException,
sWebPageAddress);
break;
}
}
catch
{}
}

Exception_WebService

public void Exception_WebService(
en_EXCEPTION_WEBSERVICE enExceptionType,
System.Reflection.MethodBase objMethodBase,
Exception objException,
string sWebServiceAddress,
string sMethodName,
string sXMLRequest)
{
try
{
switch (enExceptionType)
{
case en_EXCEPTION_WEBSERVICE.zUnableToReturnAnyDataFromThisXML:
WebService_UnableToReturnAnyDataFromThisXML2(
objMethodBase,
sWebServiceAddress,
sXMLRequest,
modMessages.sSendScreenShot);
break;

case en_EXCEPTION_WEBSERVICE.zUnableToRetrieveAnyData:
WebService_UnableToRetrieveAnyData2(
objMethodBase,
objException,
sWebServiceAddress);
break;

case en_EXCEPTION_WEBSERVICE.zEndpointNotFound:
WebService_EndpointNotFound2(
objMethodBase,
objException,
sWebServiceAddress,
modMessages.sSendScreenShot);
break;

case en_EXCEPTION_WEBSERVICE.zMethodNoDataReturned:
WebService_MethodNoDataReturned2(
objMethodBase,
sMethodName,
sWebServiceAddress,
modMessages.sSendScreenShot);
break;

case en_EXCEPTION_WEBSERVICE.zOther:
// VB intentionally does nothing here
break;
}
}
catch
{
// VB swallowed all exceptions silently — so C# must do the same
}
}

Message_LogFile_NotAccessible

public void Message_LogFile_NotAccessible(
clsSettingsFile objsettingsfile)
{
string smessage = "";

smessage =
"There was a problem establishing a connection to the log file(s):" +
System.Environment.NewLine +
System.Environment.NewLine +
objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName1 +
System.Environment.NewLine +
objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName2 +
System.Environment.NewLine +
objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName3 +
System.Environment.NewLine +
System.Environment.NewLine +
"Close ALL instances of Word including any hidden instances in your Task Manager, before re-starting the application.";

System.Windows.Forms.MessageBox.Show(
smessage,
My.Settings.APP_WINFORMS_TITLE + " - Log File Not Accessible",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2(
"MESSAGE",
smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}

Message_LogFile_NotFound

public void Message_LogFile_NotFound(
string sFullPath)
{
string smessage = "";

smessage =
"The following log file cannot be found." +
System.Environment.NewLine +
System.Environment.NewLine +
"'" + sFullPath + "'";

System.Windows.Forms.MessageBox.Show(
smessage,
My.Settings.APP_WINFORMS_TITLE,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);

smessage = smessage + " (" + System.Reflection.MethodBase.GetCurrentMethod().Name + ")";

Tracer_Add2(
"MESSAGE",
smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "));
}

MessageBox_CenterApplication

public static void MessageBox_CenterApplication(
string caption)
{
try
{
Structure_RECT oRect;
System.IntPtr ohandle;
System.Windows.Forms.NativeWindow nativeWindow;

oRect = new Structure_RECT();

Constants.gMESSAGEBOX_APPLICATION_HANDLE = new System.IntPtr(Constants.gEXCELAPPLICATION.Hwnd);

ohandle = Constants.gMESSAGEBOX_APPLICATION_HANDLE;

nativeWindow = new System.Windows.Forms.NativeWindow();
nativeWindow.AssignHandle(ohandle);

Constants.GetWindowRect(ohandle, ref oRect);

nativeWindow.ReleaseHandle();

int ExcelWindow_CenterX = oRect.Left + (oRect.Right - oRect.Left) / 2;
int ExcelWindow_CenterY = oRect.Top + (oRect.Bottom - oRect.Top) / 2;

Constants.gTHREAD_MESSAGEBOXCENTER = new System.Threading.Thread(() =>
{
Structure_RECT oRect2;
System.IntPtr ohandle2;

oRect2 = new Structure_RECT();

while ((ohandle2 = Constants.FindWindow(null, caption)) == System.IntPtr.Zero) ;

Constants.GetWindowRect(ohandle2, ref oRect2);

int Msgbox_Left = ExcelWindow_CenterX - (int)(oRect2.Right - oRect2.Left) / 2;
int Msgbox_Top = ExcelWindow_CenterY - (int)(oRect2.Bottom - oRect2.Top) / 2;

Msgbox_Left = (Msgbox_Left < 0) ? 10 : Msgbox_Left;
Msgbox_Top = (Msgbox_Top < 0) ? 10 : Msgbox_Top;

int result = Constants.MoveWindow(ohandle2, Msgbox_Left, Msgbox_Top,
oRect2.Right - oRect2.Left,
oRect2.Bottom - oRect2.Top, true);
});

Constants.gTHREAD_MESSAGEBOXCENTER.Start();

//Must include this after the MessageBox.Show
//Constants.gTHREAD_MESSAGEBOXCENTER.Abort();
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}


MessageShow

public static void MessageShow(
System.Reflection.MethodBase oMethodBase,
System.Exception oException,
string sUserMessage = "message",
string sCaptionSuffix = "")
{
string sText = "";
string sCaption = "";

string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";
string sExceptionMessage = "";
string sInnerException = "";
string sStackTrace = "";

if (modConstants.FOUND_ERROR == true)
{
return;
}

sSubroutineName = oMethodBase.Name;
if (sSubroutineName == ".ctor")
{
sSubroutineName = "Constructor";
}
sDeclaringType = oMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);

if (oException != null)
{
if (sCaptionSuffix != "Web Service Error")
{
string msubstring = "";
msubstring = oException.Message;
if (msubstring.Length > 300)
{
msubstring = msubstring.Substring(0, 300);
}

sExceptionMessage = "*" + msubstring + "*" + Environment.NewLine +
"(" + oException.Source + ")";
}
else
{
sExceptionMessage = oException.Message.Substring(0, 300) + " ....";
}

if (oException.InnerException != null)
{
sInnerException = oException.InnerException.Message;
}
sStackTrace = oException.StackTrace;
}

if (sUserMessage != "message")
{
sText = sUserMessage + Environment.NewLine + Environment.NewLine + sExceptionMessage;
}
else
{
sText = sExceptionMessage;
}

if (sCaptionSuffix.Length == 0)
{
sCaption = modConstants.MESSAGEBOX_CAPTION + sClassName + "-" + sSubroutineName;
}
else
{
sCaption = modConstants.MESSAGEBOX_CAPTION + sCaptionSuffix;
}

System.Windows.Forms.MessageBox.Show(sText, sCaption,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);

modConstants.FOUND_ERROR = true;
}

public static void MessageShow(
System.Reflection.MethodBase oMethodBase,
ref Exception oException,
string sUserMessage = "message",
string sCaptionSuffix = "",
bool bInformUser = true,
bool bUnableTo = true,
string sTracerCategory = "",
System.Windows.Forms.MessageBoxIcon objMessageBoxIcon = System.Windows.Forms.MessageBoxIcon.Stop)
{
string sText = "";
string sCaption = "";

string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";
string sExceptionMessage = "";
string sInnerException = "";
string sStackTrace = "";

try
{
if (Constants.FOUND_ERROR == true)
return;

sSubroutineName = oMethodBase.Name;
if (sSubroutineName == ".ctor")
sSubroutineName = "Constructor";

sDeclaringType = oMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);

if (oException != null)
{
string msubstring = oException.Message;
if (msubstring.Length > 300)
{
msubstring = msubstring.Substring(0, 300) + "....";
}

if (sCaptionSuffix != "Web Service Error")
{
sExceptionMessage =
"*" + msubstring + "*" +
System.Environment.NewLine +
"(" + oException.Source + ")";
}
else
{
sExceptionMessage = msubstring;
}

if (oException.InnerException != null)
sInnerException = oException.InnerException.Message;

sStackTrace = oException.StackTrace;
}

if (sUserMessage != "message")
{
sText =
sUserMessage +
System.Environment.NewLine +
System.Environment.NewLine +
sExceptionMessage;
}
else
{
sText = sExceptionMessage;
}

if (sUserMessage.Length > 0 && bUnableTo == true)
{
sUserMessage = "Unable to " + sUserMessage;
}

if (sTracerCategory.Length == 0)
{
if (objMessageBoxIcon.ToString() == "Hand")
sTracerCategory = "EXCEPTION";

if (objMessageBoxIcon.ToString() == "Warning")
sTracerCategory = "WARNING";
}

if (bInformUser == false)
sTracerCategory = sTracerCategory + "_SUPPRESSED";
else
sTracerCategory = sTracerCategory + "_MESSAGE";

if (sCaptionSuffix.Length == 0)
{
sCaptionSuffix =
Constants.MESSAGE_CAPTION +
sClassName + " - " + sSubroutineName;
}
else
{
sCaptionSuffix = Constants.MESSAGE_CAPTION + sCaptionSuffix;
}

if (bInformUser == true)
{
if (sUserMessage.Length > 0)
{
System.Windows.Forms.MessageBox.Show(
sUserMessage +
System.Environment.NewLine +
System.Environment.NewLine +
sExceptionMessage,
sCaptionSuffix,
System.Windows.Forms.MessageBoxButtons.OK,
objMessageBoxIcon);
}
else
{
System.Windows.Forms.MessageBox.Show(
sExceptionMessage,
sCaptionSuffix,
System.Windows.Forms.MessageBoxButtons.OK,
objMessageBoxIcon);
}

if (objMessageBoxIcon == System.Windows.Forms.MessageBoxIcon.Error)
{
Constants.FOUND_ERROR = true;
}
}

// VB resets the ByRef exception
oException = null;
}
catch (Exception ex)
{
string sExceptionMessage2 =
"A VERY serious error has occurred." +
System.Environment.NewLine + System.Environment.NewLine +
"Message : " + ex.Message +
System.Environment.NewLine + System.Environment.NewLine +
"Inner Exception : " + ex.InnerException.ToString() +
System.Environment.NewLine + System.Environment.NewLine +
"Source : " + ex.Source +
System.Environment.NewLine + System.Environment.NewLine +
"Stack Trace : " + ex.StackTrace;

System.Windows.Forms.MessageBox.Show(
sExceptionMessage2,
"modMessages - Exception",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
}

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