C# - Snippets
Bookmark_Exists
public bool Bookmark_Exists(ref Word.Document objDocument, string sBookmarkName, bool bInformUser = false)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
return objDocument.Bookmarks.Exists(sBookmarkName);
}
catch (Exception ex)
{
if ((bInformUser == true))
modMessages.Bookmark_DoesNotExist(sBookmarkName);
}
}
Field_CodeTextExists
public bool Field_CodeTextExists(ref Word.Document objDocument, int ifieldcount, bool bInformUser = true)
{
Word.Field oField;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
oField = objDocument.Fields(ifieldcount);
if (!(oField.Code.Text == null))
return true;
else
{
if ((bInformUser == true))
modMessages.Field_InvalidCodeText(oField);
return false;
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Fields_DocumentContainsAny
public static bool DocumentContainsAny(string sDocumentName = "")
{
Word.Document objdocument;
try
{
if (clsError.ErrorFlag() == true)
return;
if (sDocumentName != "")
objdocument = gApplicationWord.Documents(sDocumentName);
else
objdocument = gApplicationWord.ActiveDocument;
if (objdocument.Fields.Count > 0)
DocumentContainsAny = true;
else
DocumentContainsAny = false;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("DocumentContainsAny", msCLASSNAME, "determine if the document contains any fields.", mobjCOMException, mobjException);
}
}
Fields_DocumentContainsAnyType
public static bool DocumentContainsAnyType(Word.WdFieldType objFieldType, string sDocumentName = "")
{
Word.Document objdocument;
int ifieldcount;
try
{
if (clsError.ErrorFlag() == true)
return;
if (sDocumentName != "")
objdocument = gApplicationWord.Documents(sDocumentName);
else
objdocument = gApplicationWord.ActiveDocument;
DocumentContainsAnyType = false;
if (objdocument.Fields.Count > 0)
{
for (ifieldcount = 1; ifieldcount <= objdocument.Fields.Count; ifieldcount++)
{
if (objdocument.Fields(ifieldcount).Type == objFieldType)
DocumentContainsAnyType = true;
}
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("DocumentContainsAnyType", msCLASSNAME, " .", mobjCOMException, mobjException);
}
}
Fields_UnlinkAll
public void Fields_UnlinkAll(ref Word.Document objDocument)
{
Word.Field objField;
try
{
foreach (var objField in objDocument.Fields)
{
if (objField.Type == Word.WdFieldType.wdFieldDDEAuto)
objField.Unlink();
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
Fields_UpdateAll
Updates all the fields in the active document, including all sections, headers and footers.public void Fields_Update(ref Word.Document objDocument)
{
// Updates Field Numbers for Charts and Tables
int i;
string strField;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if (objDocument.Fields.Count == 0)
return;
for (i = objDocument.Fields.Count; i >= 1; i += -1)
{
strField = objDocument.Fields(i).Code.Text;
if (Microsoft.VisualBasic.InStr(1, strField, @"SEQ Figure \* ARABIC") > 0 | Microsoft.VisualBasic.InStr(1, strField, "REF _Ref") > 0)
objDocument.Fields(i).Update();
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
GoToTypeText
public static void GoToTypeText(string sBookmarkName, string sText)
{
try
{
if (clsError.ErrorFlag() == true)
return;
clszLateBindingWord.SelectionGoToBookmark(sBookmarkName);
gApplicationWord.Selection.TypeText(Text: sText);
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("GoToTypeText", msCLASSNAME, "insert the text '" + sText + "' into the bookmark '" + sBookmarkName + "'.", mobjCOMException, mobjException);
}
}
HeaderUpdate
public static void HeaderUpdate(int iSectionNo, Word.WdHeaderFooterIndex objHeaderFooterType)
{
try
{
if (clsError.ErrorFlag() == true)
return;
Word.HeaderFooter objHeader;
Word.Field objField;
if (gApplicationWord.ActiveDocument.Sections.Count < iSectionNo)
{
clszMessagesWord.SectionDoesNotExistInformation(iSectionNo);
return;
}
objHeader = gApplicationWord.ActiveDocument.Sections(iSectionNo).Headers(objHeaderFooterType);
foreach (var objField in objHeader.Range.Fields)
objField.Update();
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("HeaderUpdate", msCLASSNAME, " .", mobjCOMException, mobjException);
}
}
Message_Bookmark_DoesNotExist
public void Bookmark_DoesNotExist(string sBookmarkName)
{
string smessage = "";
smessage = "The bookmark '" + sBookmarkName + "' does not exist in this document.";
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(" ", " "));
}
Message_Field_InvalidCodeText
public void Field_InvalidCodeText(Word.Field objField)
{
string smessage = "";
smessage = "This document contains an invalid '" + objField.Type.ToString() + "' field.";
System.Windows.Forms.MessageBox.Show(smessage, My.Settings.APP_WINFORMS_TITLE + " - Invalid Field", 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(" ", " "));
}
Message_Figures_AutoNumbersRemoved
public void Figures_AutoNumbersRemoved()
{
string smessage = "";
smessage = "Existing figure fields have been removed from this document.";
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(" ", " "));
}
Message_Figures_HaveBeenUpdated
public void Figures_HaveBeenUpdated()
{
string smessage = "";
smessage = "All the Figure Numbers in this document have been updated.";
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(" ", " "));
}
RemoveFigures
public void Fields_RemoveFigures(ref Word.Document objDocument)
{
int i;
string strField;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if (objDocument.Fields.Count == 0)
return;
for (i = objDocument.Fields.Count; i >= 1; i += -1)
{
strField = objDocument.Fields(i).Code.Text;
if (Microsoft.VisualBasic.InStr(1, strField, @"SEQ Figure \* ARABIC") > 0 | Microsoft.VisualBasic.InStr(1, strField, "REF _Ref") > 0)
objDocument.Fields(i).Unlink();
}
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
SelectionGoToBookmark
public static void SelectionGoToBookmark(string sBookmarkName)
{
gApplicationWord.Selection.GoTo(What: Word.WdGoToItem.wdGoToBookmark, Name: sBookmarkName);
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top