C# Snippets
Apply
public void Style_Apply(string sStyleName, bool bClearFormatting = false)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if ((bClearFormatting == true))
gApplicationWord.Selection.ClearFormatting();
gApplicationWord.Selection.Style = sStyleName;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "Unable to apply the style '" + sStyleName + "' to the current selection." + System.Environment.NewLine + "This style does not exist in this template.", null/* Conversion error: Set to default value for this argument */, false);
}
}
Public Sub Style_Apply(ByVal sStyleName As String, _
Optional ByVal bClearFormatting As Boolean = False)
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub
If (bClearFormatting = True) Then
gApplicationWord.Selection.ClearFormatting()
End If
gApplicationWord.Selection.Style = sStyleName
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"Unable to apply the style '" & sStyleName & "' to the current selection." & _
System.Environment.NewLine & _
"This style does not exist in this template.", , False)
End Try
End Sub
ExistsInDocument
public bool Style_ExistsInDocument(Word.Document objDocument, string sStyleName, bool bInformUser = false)
{
Word.Style objstyle = null/* TODO Change to default(_) if this is not a reference type */;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
objstyle = objDocument.Styles(sStyleName);
return true;
}
catch (Exception ex)
{
return false;
if ((bInformUser == true))
modMessages.Style_DoesNotExist(sStyleName);
}
}
Public Function Style_ExistsInDocument(ByVal objDocument As Word.Document, _
ByVal sStyleName As String, _
Optional ByVal bInformUser As Boolean = False) As Boolean
Dim objstyle As Word.Style = Nothing
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
objstyle = objDocument.Styles(sStyleName)
Return True
Catch ex As Exception
Return False
If (bInformUser = True) Then
Call modMessages.Style_DoesNotExist(sStyleName)
End If
End Try
End Function
FormattingMatches
public bool Styles_FormattingMatches(ref Word.Document objDocument, string sDocumentType, string targetStyleName, Word.Paragraph targetPara)
{
int intMatchRating;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
intMatchRating = Style_MatchFormatting(objDocument, sDocumentType, targetStyleName, targetPara);
if (intMatchRating > 6)
return true;
else
return false;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "match the successfully match the formatting of this paragraph.");
return false;
}
}
Public Function Styles_FormattingMatches(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal targetStyleName As String, _
ByVal targetPara As Word.Paragraph) As Boolean
Dim intMatchRating As Integer
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
intMatchRating = Style_MatchFormatting(objDocument, sDocumentType, targetStyleName, targetPara)
If intMatchRating > 6 Then
Return True
Else
Return False
End If
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"match the successfully match the formatting of this paragraph.")
Return False
End Try
End Function
MatchFormatting
public int Style_MatchFormatting(ref Word.Document objDocument, string sDocumentType, string targetStyleName, Word.Paragraph targetPara)
{
bool bdocumentprotected;
int intMatchPoints = 0;
Word.Style oParaStyle = null/* TODO Change to default(_) if this is not a reference type */;
Word.Style targetStyle;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
oParaStyle = (Word.Style)targetPara.Style;
if (oParaStyle.NameLocal == targetStyleName)
intMatchPoints = 7;
try
{
targetStyle = objDocument.Styles(targetStyleName);
}
catch (Exception ex)
{
targetStyle = null/* TODO Change to default(_) if this is not a reference type */;
}
if (targetStyle != null)
{
if ((modSpecific.Document_IsValidAndProtected(objDocument) == true))
{
bdocumentprotected = true;
modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, false);
}
// 1
if (targetPara.Range.Font.Bold == targetStyle.Font.Bold)
intMatchPoints += 1;
// 2
if (targetPara.Range.Font.Italic == targetStyle.Font.Italic)
intMatchPoints += 1;
// 3
if (targetPara.Range.Font.Name == targetStyle.Font.Name)
intMatchPoints += 1;
// 4
if (targetPara.Range.Font.Size == targetStyle.Font.Size)
intMatchPoints += 1;
// 5
if (targetPara.SpaceAfter == targetStyle.ParagraphFormat.SpaceAfter)
intMatchPoints += 1;
// 6
if (targetPara.SpaceBefore == targetStyle.ParagraphFormat.SpaceBefore)
intMatchPoints += 1;
// 7
if (targetPara.Range.Font.Color == targetStyle.Font.Color)
intMatchPoints += 1;
// 8
if (targetPara.Range.Font.AllCaps == targetStyle.Font.AllCaps)
intMatchPoints += 1;
// 9
if (targetPara.Range.ParagraphFormat.Alignment == targetStyle.ParagraphFormat.Alignment)
intMatchPoints += 1;
}
return intMatchPoints;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
finally
{
if ((bdocumentprotected == true))
modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, false);
}
}
Public Function Style_MatchFormatting(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal targetStyleName As String, _
ByVal targetPara As Word.Paragraph) As Integer
Dim bdocumentprotected As Boolean
Dim intMatchPoints As Integer = 0
Dim oParaStyle As Word.Style = Nothing
Dim targetStyle As Word.Style
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
oParaStyle = CType(targetPara.Style, Word.Style)
If oParaStyle.NameLocal = targetStyleName Then
intMatchPoints = 7
End If
Try
targetStyle = objDocument.Styles(targetStyleName)
Catch ex As System.Exception
targetStyle = Nothing
End Try
If targetStyle IsNot Nothing Then
If (modSpecific.Document_IsValidAndProtected(objDocument) = True) Then
bdocumentprotected = True
Call modWordObjectModel.Document_Unprotect(objDocument, gsDOCUMENTPASSWORD, False)
End If
'1
If targetPara.Range.Font.Bold = targetStyle.Font.Bold Then
intMatchPoints += 1
End If
'2
If targetPara.Range.Font.Italic = targetStyle.Font.Italic Then
intMatchPoints += 1
End If
'3
If targetPara.Range.Font.Name = targetStyle.Font.Name Then
intMatchPoints += 1
End If
'4
If targetPara.Range.Font.Size = targetStyle.Font.Size Then
intMatchPoints += 1
End If
'5
If targetPara.SpaceAfter = targetStyle.ParagraphFormat.SpaceAfter Then
intMatchPoints += 1
End If
'6
If targetPara.SpaceBefore = targetStyle.ParagraphFormat.SpaceBefore Then
intMatchPoints += 1
End If
'7
If targetPara.Range.Font.Color = targetStyle.Font.Color Then
intMatchPoints += 1
End If
'8
If targetPara.Range.Font.AllCaps = targetStyle.Font.AllCaps Then
intMatchPoints += 1
End If
'9
If targetPara.Range.ParagraphFormat.Alignment = targetStyle.ParagraphFormat.Alignment Then
intMatchPoints += 1
End If
End If
Return intMatchPoints
Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
If (bdocumentprotected = True) Then
Call modWordObjectModel.Document_Protect(objDocument, gsDOCUMENTPASSWORD, False)
End If
End Try
End Function
Style_DoesNotExist
public void Style_DoesNotExist(string sStyleName)
{
string smessage = "";
smessage = "The style '" + sStyleName + "' 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(" ", " "));
}
Public Sub Style_DoesNotExist(ByVal sStyleName As String)
Dim smessage As String = ""
smessage = "The style '" & sStyleName & "' 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 & ")"
Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub
ToArrayList
public System.Collections.ArrayList Styles_ToArrayList(ref Word.Document objDocument)
{
System.Collections.ArrayList alsStyles;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
{
return null; return;
}
alsStyles = new System.Collections.ArrayList();
for (int icount = 1; icount <= (objDocument.Styles.Count); icount++)
alsStyles.Add(objDocument.Styles(icount).NameLocal);
return alsStyles;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
return null;
}
}
Public Function Styles_ToArrayList(ByRef objDocument As Word.Document) As System.Collections.ArrayList
Dim alsStyles As System.Collections.ArrayList
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Return Nothing : Exit Function
alsStyles = New System.Collections.ArrayList
For icount As Integer = 1 To (objDocument.Styles.Count)
alsStyles.Add(objDocument.Styles(icount).NameLocal)
Next icount
Return alsStyles
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return Nothing
End Try
End Function
Update
public bool Styles_Update(ref Word.Document objDocument)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
objDocument.UpdateStyles();
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "update the document styles from the template.");
}
}
Public Function Styles_Update(ByRef objDocument As Word.Document) As Boolean
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
objDocument.UpdateStyles()
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"update the document styles from the template.")
End Try
End Function
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top