C# Snippets


Apply

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 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 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 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 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 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 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

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