C# Snippets
Bookmark_DoesNotExist
Public Sub Bookmark_DoesNotExist(ByVal sBookmarkName As String)
Dim smessage As String = ""
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 & ")"
Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub
CodeTextExists
Public Function Field_CodeTextExists(ByRef objDocument As Word.Document, _
ByVal ifieldcount As Integer, _
Optional ByVal bInformUser As Boolean = True) As Boolean
Dim oField As Word.Field
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
oField = objDocument.Fields(ifieldcount)
If Not (oField.Code.Text Is Nothing) Then
Return True
Else
If (bInformUser = True) Then
Call modMessages.Field_InvalidCodeText(oField)
End If
Return False
End If
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function
DocumentContainsAny
Public Shared Function DocumentContainsAny(Optional ByVal sDocumentName As String = "") _
As Boolean
Dim objdocument As Word.Document
Try
If clsError.ErrorFlag() = True Then Exit Function
If sDocumentName <> "" Then
objdocument = gApplicationWord.Documents(sDocumentName)
Else
objdocument = gApplicationWord.ActiveDocument
End If
If objdocument.Fields.Count > 0 Then
DocumentContainsAny = True
Else
DocumentContainsAny = False
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("DocumentContainsAny", msCLASSNAME, _
"determine if the document contains any fields.", _
mobjCOMException, mobjException)
End If
End Try
End Function
DocumentContainsAnyType
Public Shared Function DocumentContainsAnyType(ByVal objFieldType As Word.WdFieldType, _
Optional ByVal sDocumentName As String = "") _
As Boolean
Dim objdocument As Word.Document
Dim ifieldcount As Integer
Try
If clsError.ErrorFlag() = True Then Exit Function
If sDocumentName <> "" Then
objdocument = gApplicationWord.Documents(sDocumentName)
Else
objdocument = gApplicationWord.ActiveDocument
End If
DocumentContainsAnyType = False
If objdocument.Fields.Count > 0 Then
For ifieldcount = 1 To objdocument.Fields.Count
If objdocument.Fields(ifieldcount).Type = objFieldType Then
DocumentContainsAnyType = True
End If
Next ifieldcount
End If
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("DocumentContainsAnyType", msCLASSNAME, _
" .", _
mobjCOMException, mobjException)
End If
End Try
End Function
Exists
Public Function Bookmark_Exists(ByRef objDocument As Word.Document, _
ByVal sBookmarkName As String, _
Optional ByVal bInformUser As Boolean = False) As Boolean
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function
Return objDocument.Bookmarks.Exists(sBookmarkName)
Catch ex As System.Exception
If (bInformUser = True) Then
Call modMessages.Bookmark_DoesNotExist(sBookmarkName)
End If
End Try
End Function
Field_InvalidCodeText
Public Sub Field_InvalidCodeText(ByVal objField As Word.Field)
Dim smessage As String = ""
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 & ")"
Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub
Figures_AutoNumbersRemoved
Public Sub Figures_AutoNumbersRemoved()
Dim smessage As String = ""
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 & ")"
Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub
Figures_HaveBeenUpdated
Public Sub Figures_HaveBeenUpdated()
Dim smessage As String = ""
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 & ")"
Call Tracer_Add2("MESSAGE", smessage.Replace(System.Environment.NewLine, " ").Replace(" ", " "))
End Sub
GoToTypeText
Public Shared Sub GoToTypeText(ByVal sBookmarkName As String, _
ByVal sText As String)
Try
If clsError.ErrorFlag() = True Then Exit Sub
Call clszLateBindingWord.SelectionGoToBookmark(sBookmarkName)
gApplicationWord.Selection.TypeText(Text:=sText)
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("GoToTypeText", msCLASSNAME, _
"insert the text '" & sText & "' into the bookmark '" & sBookmarkName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
HeaderUpdate
Public Shared Sub HeaderUpdate(ByVal iSectionNo As Integer, _
ByVal objHeaderFooterType As Word.WdHeaderFooterIndex)
Try
If clsError.ErrorFlag() = True Then Exit Sub
Dim objHeader As Word.HeaderFooter
Dim objField As Word.Field
If gApplicationWord.ActiveDocument.Sections.Count < iSectionNo Then
Call clszMessagesWord.SectionDoesNotExistInformation(iSectionNo)
Exit Sub
End If
objHeader = gApplicationWord.ActiveDocument.Sections(iSectionNo).Headers(objHeaderFooterType)
For Each objField In objHeader.Range.Fields
objField.Update()
Next objField
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("HeaderUpdate", msCLASSNAME, _
" .", _
mobjCOMException, mobjException)
End If
End Try
End Sub
RemoveFigures
Public Sub Fields_RemoveFigures(ByRef objDocument As Word.Document)
Dim i As Integer
Dim strField As String
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub
If objDocument.Fields.Count = 0 Then
Exit Sub
End If
For i = objDocument.Fields.Count To 1 Step -1
strField = objDocument.Fields(i).Code.Text
If Microsoft.VisualBasic.InStr(1, strField, "SEQ Figure \* ARABIC") > 0 Or _
Microsoft.VisualBasic.InStr(1, strField, "REF _Ref") > 0 Then
objDocument.Fields(i).Unlink()
End If
Next i
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub
SelectionGoToBookmark
Public Shared Sub SelectionGoToBookmark(ByVal sBookmarkName As String)
gApplicationWord.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, _
Name:=sBookmarkName)
End Sub
UnlinkAll
Public Sub Fields_UnlinkAll(ByRef objDocument As Word.Document)
Dim objField As Word.Field
Try
For Each objField In objDocument.Fields
If objField.Type = Word.WdFieldType.wdFieldDDEAuto Then
objField.Unlink()
End If
'objField.LinkFormat.BreakLink
Next objField
Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub
UpdateAll
Updates all the fields in the active document, including all sections, headers and footers.Public Sub Fields_Update(ByRef objDocument As Word.Document)
'Updates Field Numbers for Charts and Tables
Dim i As Integer
Dim strField As String
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub
If objDocument.Fields.Count = 0 Then
Exit Sub
End If
For i = objDocument.Fields.Count To 1 Step -1
strField = objDocument.Fields(i).Code.Text
If Microsoft.VisualBasic.InStr(1, strField, "SEQ Figure \* ARABIC") > 0 Or _
Microsoft.VisualBasic.InStr(1, strField, "REF _Ref") > 0 Then
objDocument.Fields(i).Update()
End If
Next i
Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top