C# Snippets


ActivateUnsuccessful

Public Sub Document_ActivateUnsuccessful()
System.Windows.Forms.MessageBox.Show( _
"The document that you were working on has been closed or renamed." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"You will have to close and reopen this dialog box.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Exclamation)
End Sub

Active

Public Shared Function Active() As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

If gApplicationWord.Documents.Count > 0 Then
Active = True
Else
Active = False
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("Open", "clsDocument", _
"determine if there is a document active.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Active

Public Shared Function Active() As String

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim objPane As Word.View
Dim stemp As String

objPane = gApplicationWord.ActiveWindow.ActivePane.View

Select Case objPane.Type
Case Word.WdViewType.wdMasterView : stemp = "Master"
Case Word.WdViewType.wdNormalView : stemp = "Normal"
Case Word.WdViewType.wdOutlineView : stemp = "Outline"
Case Word.WdViewType.wdPrintPreview : stemp = "PrintPreview"
Case Word.WdViewType.wdPrintView : stemp = "Print"
Case Word.WdViewType.wdWebView : stemp = "Web"
End Select

Active = stemp

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("Active", msCLASSNAME, _
"return the type of pane that is currently active.", _
mobjCOMException, mobjException)
End If
End Try
End Function

CloseByName

Public Sub Document_CloseByName(ByVal sDocName As String, _
Optional ByVal bInformUser As Boolean = False)

Dim icount As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

For icount = 1 To gApplicationWord.Documents.Count
If (gApplicationWord.Documents(icount).Name = sDocName) Then
gApplicationWord.Documents(icount).Close(False)
Exit Sub
End If
Next icount

If (bInformUser = True) Then
'message saying that the document does not exist
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"", False)
End Try
End Sub

CloseDocument

Public Shared Sub CloseDocument(ByVal bSave As Boolean, _
Optional ByVal sDocumentName As String = "", _
Optional ByVal bInformUser As Boolean = True)

Dim objdocument As Word.Document
Dim objsaveoptions As Word.WdSaveOptions

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sDocumentName <> "" Then
objDocument = gApplicationWord.Documents(sDocumentName)
Else
objDocument = gApplicationWord.ActiveDocument
End If

If bSave = True Then objsaveoptions = Word.WdSaveOptions.wdSaveChanges
If bSave = False Then objsaveoptions = Word.WdSaveOptions.wdDoNotSaveChanges

If gApplicationWord.Documents.Count = 0 Then Exit Sub

Call CType(objdocument, Word._Document).Close(SaveChanges:=CType(objsaveoptions, Word.WdSaveOptions))

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("CloseDocument", msCLASSNAME, _
"close the active document.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

DeserializeFrom

Public Function DocVariable_DeserializeFrom(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
ByVal oDocValue As System.Type) As Object
Dim myObject As Object
Dim strObjectXml As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Return Nothing : Exit Function

strObjectXml = modWordObjectModel.DocVariable_Get(objDocument, sDocVariable, True, False)

If strObjectXml <> "" AndAlso strObjectXml <> " " Then

myObject = New Object
myObject = modGeneral.XML_DeserializeFromString(strObjectXml, oDocValue)
Return myObject

Else
Return Nothing
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return Nothing
End Try
End Function

Doc_Activate

Public Function Doc_Activate(ByRef objDocument As Word.Document, _
ByVal bInformUser As Boolean) As Boolean
Dim icount As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

For icount = 1 To gApplicationWord.Documents.Count
If (gApplicationWord.Documents(icount).Name = objDocument.Name) Then
objDocument.Activate()
Return True
Exit Function
End If
Next icount

If (bInformUser = True) Then
Call modMessages.Document_ActivateUnsuccessful()
End If
Return False

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return False
End Try
End Function

Doc_Save

Public Function Document_Save(ByRef objDocument As Word.Document, _
Optional ByVal bForceUserToSave As Boolean = False) As Boolean

Dim bDocumentSaveError As Boolean
Dim blnSucess As Boolean = False

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If objDocument.Saved = False Then
If objDocument.ReadOnly = False Then
bDocumentSaveError = True
objDocument.Save()
bDocumentSaveError = False
blnSucess = True
Else
Call modMessages.Document_CannotBeSavedIsReadOnly()
End If
Else
blnSucess = True
End If

Catch ex As System.Exception
If (bDocumentSaveError = False) Then
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Else
If (bForceUserToSave = True) Then
Call Document_SaveDocumentBeforeContinuing()
Call modWordObjectModel.Document_Save(objDocument, bForceUserToSave)
End If
End If
End Try

Return blnSucess
End Function

Doc_SaveAs

Public Shared Function Doc_SaveAs(ByVal sFolderPath As String, _
ByVal sFileName As String, _
Optional ByVal sExtension As String = ".xls", _
Optional ByVal bDeleteIfExists As Boolean = False, _
Optional ByVal bInformUser As Boolean = False, _
Optional ByVal sAdditional As String = "") As Boolean

Try
Dim breplace As Boolean

If clsError.ErrorFlag() = True Then Exit Function

SaveAs = False

If clsFile.Exists(sFolderPath, sFileName, sExtension, False) = True Then

If bInformUser = True Then
breplace = clszMessagesGeneral.FileReplaceQuestion(sFolderPath, sFileName)
End If

If breplace = True Then
If bDeleteIfExists = True Then
Call clsFile.Delete(sFolderPath, sFileName, sExtension)
End If

gApplicationWord.ActiveDocument.SaveAs(FileName:=sFolderPath & sFileName & _
sAdditional & sExtension)
SaveAs = True
End If
Else
gApplicationWord.ActiveDocument.SaveAs(FileName:=sFolderPath & sFileName & _
sAdditional & sExtension)
SaveAs = True
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("SaveAs", msCLASSNAME, _
"save the file '" & sFileName & sExtension & " '" & _
"in the following directory:" & _
gsCRLF & "'" & sFolderPath & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocProperty_DoesNotExist

Public Sub DocProperty_DoesNotExist(ByVal sPropertyName As String)
System.Windows.Forms.MessageBox.Show( _
"The document property '" & sPropertyName & "' does not exist.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

DocProperty_UnableToAddDocumentIsProtected

Public Sub DocProperty_UnableToAddDocumentIsProtected(ByVal sPropertyName As String)
System.Windows.Forms.MessageBox.Show( _
"Unable to create the document property '" & sPropertyName & "' because the document is protected.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

DocumentPropertyBuiltInCount

Public Shared Function DocumentPropertyBuiltInCount(ByVal sDocumentName As String) _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

DocumentPropertyBuiltInCount = _
gApplicationWord.Documents(sDocumentName).BuiltInDocumentProperties.Count


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("DocumentPropertyBuiltInCount", msCLASSNAME, _
"return the total number of 'built-in' document properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyBuiltInExists

Public Shared Function DocumentPropertyBuiltInExists(ByVal sDocumentName As String, _
ByVal sPropertyName As String) _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.documentss(sDocumentName).BuiltInDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
DocumentPropertyBuiltInExists = True
Exit Function
End If
Next ipropertycount
End With

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("DocumentPropertyBuiltInExists", msCLASSNAME, _
"determine if the 'built-in' document property '" & sPropertyName & "' exists.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyBuiltInGet

Public Shared Function DocumentPropertyBuiltInGet(ByVal sDocumentName As String, _
ByVal sPropertyName As String, _
ByVal objDefaultValue As Object) _
As Object

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).BuiltInDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
DocumentPropertyBuiltInGet = .Item(ipropertycount).Value
Exit Function
End If
Next ipropertycount
End With

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("DocumentPropertyBuiltInGet", msCLASSNAME, _
"return the 'built-in' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyBuiltInSet

Public Shared Sub DocumentPropertyBuiltInSet(ByVal sDocumentName As String, _
ByVal sPropertyName As String, _
ByVal objPropertyValue As Object)

Dim ipropertycount As Integer
Dim sitemname As String

Try
If clsError.ErrorFlag() = True Then Exit Sub

With gApplicationWord.Documents(sDocumentName).BuiltInDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Value = objPropertyValue
Exit Sub
End If
Next ipropertycount
End With


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("DocumentPropertyBuiltInSet", msCLASSNAME, _
"change the value of the 'built-in' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

DocumentPropertyCustomAdd

Public Shared Sub DocumentPropertyCustomAdd(ByVal sDocumentName As String, _
ByVal sPropertyName As String, _
ByVal sPropertyType As String, _
ByVal objPropertyValue As Object)
Try
If clsError.ErrorFlag() = True Then Exit Sub

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).CustomDocumentProperties

.Add(Name:=sPropertyName, _
LinkToContent:=Office.MsoTriState.msoFalse, _
Value:=objPropertyValue, _
Type:=clsOfficeCore.PropertyTypeReturn(sPropertyType))

End With

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("DocumentPropertyCustomAdd", msCLASSNAME, _
"add the 'custom' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

DocumentPropertyCustomCount

Public Shared Function DocumentPropertyCustomCount(ByVal sDocumentName As String) _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

DocumentPropertyCustomCount = _
gApplicationWord.Documents(sDocumentName).CustomDocumentProperties.Count


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("DocumentPropertyCustomCount", msCLASSNAME, _
"return the total number of 'custom' document properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyCustomDelete

Public Shared Sub DocumentPropertyCustomDelete(ByVal sDocumentName As String, _
ByVal sPropertyName As String)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Delete()
Exit Sub
End If
Next ipropertycount
End With

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("DocumentPropertyCustomDelete", msCLASSNAME, _
"delete the 'custom' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

DocumentPropertyCustomExists

Public Shared Function DocumentPropertyCustomExists(ByVal sDocumentName As String, _
ByVal sPropertyName As String) _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
DocumentPropertyCustomExists = True
Exit Function
End If
Next ipropertycount
End With

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("DocumentPropertyCustomExists", msCLASSNAME, _
"determine if the 'custom' document property '" & sPropertyName & "' exists.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyCustomGet

Public Shared Function DocumentPropertyCustomGet(ByVal sDocumentName As String, _
ByVal sPropertyName As String, _
ByVal objDefaultValue As Object) _
As Object

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
DocumentPropertyCustomGet = .Item(ipropertycount).Value
Exit Function
End If
Next ipropertycount
End With

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("DocumentPropertyCustomGet", msCLASSNAME, _
"return the 'custom' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

DocumentPropertyCustomSet

Public Shared Sub DocumentPropertyCustomSet(ByVal sDocumentName As String, _
ByVal sPropertyName As String, _
ByVal objPropertyValue As Object)

Try
If clsError.ErrorFlag() = True Then Exit Sub


Dim ipropertycount As Integer
Dim sitemname As String

With gApplicationWord.Documents(sDocumentName).CustomDocumentProperties
For ipropertycount = 1 To .Count
sitemname = .Item(ipropertycount).Name

If sitemname = sPropertyName Then
.Item(ipropertycount).Value = objPropertyValue
Exit Sub
End If
Next ipropertycount
End With


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("DocumentPropertyCustomSet", msCLASSNAME, _
"change the value of the 'custom' document property '" & sPropertyName & ".", _
mobjCOMException, mobjException)
End If
End Try
End Sub

DocumentSave

Public Shared Function DocumentSave(ByVal objDocument As Word.Document) _
As Boolean
Try

'an error can occur if the (file > save as) dialog box is dismissed
objDocument.Save()
'seems to cause a normal exception and not a COM exception ?????

DocumentSave = True

'Catch objCOMException As System.Runtime.InteropServices.COMException
' DocumentSave = False
Catch objException As Exception
DocumentSave = False
End Try
End Function

DocVariable_Add

Adds a custom variable to a document.
Public Sub DocVariable_Add(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
ByVal sDocValue As String, _
Optional ByVal bResetValue As Boolean = False, _
Optional ByVal bInformUser As Boolean = False)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If DocVariable_Exists(objDocument, sDocVariable) = False Then
objDocument.Variables.Add(sDocVariable, CType(sDocValue, Object))
Else
If (bResetValue = True) Then
objDocument.Variables(sDocVariable).Value = sDocValue
End If
End If

Catch ex As System.Exception
If (bInformUser = True) Then
Call modMessages.DocVariable_UnableToAdd(sDocVariable)
End If
End Try
End Sub

DocVariable_Delete

Removes a custom variable from the active document.
Public Sub DocVariable_Delete(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bCheckExists As Boolean = False)

Dim ovariable As Word.Variable

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If (bCheckExists = True) Then
If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) = False Then Exit Sub
End If

ovariable = objDocument.Variables(sDocVariable)
ovariable.Delete()

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

DocVariable_Deleted

Public Sub DocVariable_Deleted(ByVal sDocVariable As String)

Dim smessage As String = ""

smessage = "The document variable '" & sDocVariable & "' has 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

DocVariable_DoesNotExist

Public Sub DocVariable_DoesNotExist(ByVal sDocVariable As String)
System.Windows.Forms.MessageBox.Show( _
"The document variable '" & sDocVariable & "' does not exist.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

DocVariable_Exists

Public Function VariableExists(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bInformUser As Boolean = False) As Boolean

Dim oVariable As Word.Variable
Dim strValue As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

oVariable = objDocument.Variables(sDocVariable)
strValue = oVariable.Name

'Call Tracer_Add2("VARIABLE", "The document variable '" & sDocVariable & "' exists")
Return True

Catch ex As System.Exception
Return False
Call Tracer_Add2("VARIABLE", "The document variable '" & sDocVariable & "' does not exist")
If (bInformUser = True) Then
Call modMessages.DocVariable_DoesNotExist(sDocVariable)
End If
End Try

End Function

DocVariable_Get

Returns the value of a custom variable in the active document.
Public Function DocVariable_Get(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bCheckExists As Boolean = True, _
Optional ByVal bInformUser As Boolean = False, _
Optional ByVal bCheckMultiples As Boolean = False, _
Optional ByVal iFirstNumber As Integer = 1, _
Optional ByVal sDifferentDefault As String = "") As String

Dim sTempVarName As String
Dim sConcatenated As String = ""
Dim intCounter As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Return sDifferentDefault : Exit Function

DocVariable_Get = sDifferentDefault

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If bCheckMultiples = False Then
If bCheckExists = False Then
Return objDocument.Variables(sDocVariable).Value
Else
If DocVariable_Exists(objDocument, sDocVariable) = True Then
Return objDocument.Variables(sDocVariable).Value
Else
Call Tracer_Add2("VARIABLE", "'" & sDocVariable & "' doesn't exist", _
True, System.Reflection.MethodBase.GetCurrentMethod)

If (bInformUser = True) Then
modMessages.Message_General_DocVariableNotCreated(sDocVariable)
End If
End If
End If

Else
If modWordObjectModel.DocVariable_MultiplesExist(objDocument, sDocVariable) = True Then

intCounter = iFirstNumber
sTempVarName = sDocVariable & CStr(intCounter)

Do Until modWordObjectModel.DocVariable_Exists(objDocument, sTempVarName) = False
sConcatenated = sConcatenated & objDocument.Variables(sTempVarName).Value

intCounter = intCounter + 1
sTempVarName = sDocVariable & CStr(intCounter)
Loop
Return sConcatenated.Trim
Else
Return objDocument.Variables(sDocVariable).Value
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return ""
End Try
End Function

DocVariable_GetBoolean

Public Function DocVariable_GetBoolean(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bCheckExists As Boolean = True, _
Optional ByVal bInformUser As Boolean = False) As Boolean

'returns False if the document variable does not exist
Dim svariable As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) = True Then
svariable = modWordObjectModel.DocVariable_Get(objDocument, sDocVariable, False)
If svariable.Trim.ToUpper = "TRUE" Or svariable.Trim.ToUpper = "FALSE" Then
Return CType(svariable.Trim, Boolean)
End If
Else
Call Tracer_Add2("VARIABLE", sDocVariable & "' doesn't exist", _
True, System.Reflection.MethodBase.GetCurrentMethod)

If bInformUser = True Then
modMessages.Message_General_DocVariableNotCreated(sDocVariable)
End If
Return False
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

DocVariable_GetInteger

Public Function DocVariable_GetInteger(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bCheckExists As Boolean = True, _
Optional ByVal bInformUser As Boolean = False) As Integer
'returns -1 if the document variable does not exist
Dim svariable As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) = True Then
svariable = modWordObjectModel.DocVariable_Get(objDocument, sDocVariable, False)
If (svariable.Trim.Length > 0) Then
Return CType(svariable.Trim, Integer)
Else
Return -1
End If
Else
Call Tracer_Add2("VARIABLE", "The document variable '" & sDocVariable & "' does not exist", _
True, System.Reflection.MethodBase.GetCurrentMethod)

If bInformUser = True Then
modMessages.Message_General_DocVariableNotCreated(sDocVariable)
End If
Return -1
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

DocVariable_GetLong

Public Function DocVariable_GetLong(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bCheckExists As Boolean = True, _
Optional ByVal bInformUser As Boolean = False) As Long

'returns -1 if the document variable does not exist
Dim svariable As String

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) = True Then
svariable = modWordObjectModel.DocVariable_Get(objDocument, sDocVariable, False)
If (svariable.Trim.Length > 0) Then
Return CType(svariable.Trim, Long)
Else
Return -1
End If
Else
Call Tracer_Add2("VARIABLE", sDocVariable & "' doesn't exist", _
True, System.Reflection.MethodBase.GetCurrentMethod)

If bInformUser = True Then
modMessages.Message_General_DocVariableNotCreated(sDocVariable)
End If
Return -1
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

DocVariable_MultiplesDelete

Public Function DocVariable_MultiplesDelete(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
Optional ByVal bStartAtZero As Boolean = False) As Boolean

Dim stempvariable As String
Dim icounter As Integer
Dim ovariable As Word.Variable

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If (bStartAtZero = True) Then
icounter = 0
Else
icounter = 1
End If

stempvariable = sDocVariable & CStr(icounter)

Do Until modWordObjectModel.DocVariable_Exists(objDocument, stempvariable) = False
ovariable = objDocument.Variables(stempvariable)
ovariable.Delete()
icounter = icounter + 1

stempvariable = sDocVariable & CStr(icounter)
'Return True
Loop

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

DocVariable_MultiplesExist

Public Function DocVariable_MultiplesExist(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String) As Boolean

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) = True Then
Return False
Else
sDocVariable = sDocVariable & "1"
If objDocument.Variables(sDocVariable).Value.Length > 0 Then Return True
If objDocument.Variables(sDocVariable).Value.Length = 0 Then Return False
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

DocVariable_Save

Public Function DocVariable_Save(ByRef objDocument As Word.Document, _
ByVal sDocVariable As String, _
ByVal sDocValue As String, _
Optional ByVal bCheckMultiples As Boolean = True, _
Optional ByVal bStartAtZero As Boolean = False) As Boolean

Dim intVars As Integer = 0
Dim iSegmentLength As Integer = 65200
Dim blnCompleted As Boolean = False

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If (bCheckMultiples = True) Then
Call modWordObjectModel.DocVariable_MultiplesDelete(objDocument, sDocVariable, bStartAtZero)
End If

If sDocValue Is Nothing Then
sDocValue = " "
End If

If (sDocValue <> " ") And Not (sDocVariable Like "strHTML%") Then
Call Tracer_Add2("VARIABLE", "The document variable '" & sDocVariable & "' is attempting to save: " & _
"'" & sDocValue & "'")
End If

If sDocValue.Length > iSegmentLength Then
intVars = (sDocValue.Length \ iSegmentLength)
If sDocValue.Length Mod iSegmentLength > 0 Then intVars = intVars + 1
End If

If intVars > 0 Then

Dim iStart As Integer
Dim intCutoff As Long
Dim allStrings(intVars - 1) As Object

iStart = 1
intCutoff = iSegmentLength

For i As Integer = 0 To intVars - 1
ReDim Preserve allStrings(i)
allStrings(i) = Microsoft.VisualBasic.Mid(sDocValue, iStart, iSegmentLength)
iStart = iStart + iSegmentLength
Next i

If allStrings.Length > 0 Then
For i As Integer = 1 To allStrings.Length
objDocument.Variables.Add(sDocVariable & CStr(i), allStrings(i - 1))
blnCompleted = True

Call modSpecific.SaveHTMLLevels(objDocument, sDocVariable, CStr(i))
Next i
Else

Call modSpecific.SaveHTMLLevels(objDocument, sDocVariable, CStr(0))
End If
End If

If blnCompleted = False Then
If sDocVariable = "" Then sDocVariable = " "

If modWordObjectModel.DocVariable_Exists(objDocument, sDocVariable) Then
objDocument.Variables(sDocVariable).Value = sDocValue

'This is only being used while FID Addin is active!
If Microsoft.VisualBasic.Left(sDocVariable, 13) = "strWebSummary" Then
If modWordObjectModel.DocVariable_Exists(objDocument, "strWebSummary2") Then
objDocument.Variables("strWebSummary2").Value = " "
End If
If modWordObjectModel.DocVariable_Exists(objDocument, "strWebSummary3") Then
objDocument.Variables("strWebSummary3").Value = " "
End If
End If
Else
objDocument.Variables.Add(sDocVariable, CType(sDocValue, Object))
End If

Return True
blnCompleted = True
End If

Return blnCompleted

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
If (sDocValue <> " ") And Not (sDocVariable Like "strHTML%") Then
Call Tracer_Add2("VARIABLE", "The document variable '" & sDocVariable & "' has been saved.")
End If
End Try
End Function

DocVariable_UnableToAdd

Public Sub DocVariable_UnableToAdd(ByVal sDocVariable As String)
System.Windows.Forms.MessageBox.Show( _
"Unable to create the document variable '" & sDocVariable & "'.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

Find_StringMakeBold

Public Sub Find_StringMakeBold(ByVal objRange As Word.Range, _
ByVal findString As String)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

'removes the bold formatting from everything between the tags
With objRange.Find
.ClearFormatting()
.MatchWildcards = True
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = findString
.MatchCase = False

.Replacement.ClearFormatting()
.Replacement.Font.Bold = -1 'True
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

'removes the bold tags (open and close)
If findString.IndexOf("\") > -1 Then
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\<\/b\>"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Find_StringMakeItalic

Public Sub Find_StringMakeItalic(ByVal objRange As Word.Range, _
ByVal findString As String)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

'removes the bold formatting from everything between the tags
With objRange.Find
.ClearFormatting()
.MatchWildcards = True
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = findString
.MatchCase = False

.Replacement.ClearFormatting()
.Replacement.Font.Italic = -1 'True
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

'removes the bold tags (open and close)
If findString.IndexOf("\") > -1 Then
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\<\/i\>"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

Find_StringMakeSuper

Public Sub Find_StringMakeSuper(ByVal objRange As Word.Range, _
ByVal findString As String)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

With objRange.Find
.ClearFormatting()
.MatchWildcards = True
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = findString
.MatchCase = False

.Replacement.ClearFormatting()
.Replacement.Font.Superscript = -1 'True
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

If findString.IndexOf("\") > -1 Then
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
With objRange.Find
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\<\/sup\>"
.MatchCase = False
.Replacement.Text = ""
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

FindRemoveLinkTags

Public Sub FindRemoveLinkTags(ByVal objRange As Word.Range)
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

With objRange.Find
.ClearFormatting()
.MatchWildcards = True
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\"
.MatchCase = True
With .Replacement
.ClearFormatting()
.Text = ""
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

With objRange.Find
.ClearFormatting()
.MatchWildcards = True
.Format = False
.Wrap = Word.WdFindWrap.wdFindContinue
.Forward = True
.Text = "\<\/link\>"
.MatchCase = True
With .Replacement
.ClearFormatting()
.Text = ""
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
objRange = Nothing
End Try
End Sub

FullNameGet

Public Shared Function FullNameGet() As String

Try
If clsError.ErrorFlag() = True Then Exit Function

FullNameGet = gApplicationWord.ActiveDocument.FullName

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("FullNameGet", "clsDocument", _
"returns the fullname of the active document.", _
mobjCOMException, mobjException)
End If
End Try
End Function

GetActive

Public Function Document_GetActive() As Word.Document
Try
Return gApplicationWord.ActiveDocument

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"", False)
Return Nothing
End Try
End Function

HasATable

Function Document_HasATable(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

If objDocument.Content.Tables.Count > 0 Then
Return True
Else
Return False
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

HasBeenSaved

Public Function Document_HasBeenSaved(ByRef objDocument As Word.Document, _
ByVal bInformUser As Boolean, _
ByVal bDisplaySaveAs As Boolean) As Boolean
Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

' Prompts user to SaveAs, if the doc has not been saved
If (objDocument.Name <> objDocument.FullName) Then

Call Tracer_Add2("DOCUMENT", "Has Been Saved - " & objDocument.FullName, False)
Return True
Exit Function
Else

If (bInformUser = True) Then
Call modMessages.Document_HasNotBeenSaved()
Return False
End If

If (bDisplaySaveAs = True) Then
If (gApplicationWord.Dialogs(Word.WdWordDialog.wdDialogFileSaveAs).Show() = -1) Then
Return True
Else
Return False
End If
Else
Return False
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

HasNotBeenSaved

Public Sub Document_HasNotBeenSaved()
System.Windows.Forms.MessageBox.Show( _
"This document has not been saved." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"You need to save the file first.", _
My.Settings.APP_WINFORMS_TITLE, _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Information)
End Sub

IsDisplayed

Public Function Document_IsDisplayed() As Boolean

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If gApplicationWord.Documents.Count > 0 Then
Return True
End If

Catch ex As System.Exception
Return False
End Try
End Function

IsReading

Public Function View_IsReading(Optional ByVal bInformUser As Boolean = True) As Boolean
Try
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (gApplicationWord.ActiveWindow.View.ReadingLayout) = True Then

If (bInformUser = True) Then
Call modMessages.View_IsReading()
End If
Return False
Else
Return True
End If

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"", False)
End Try
End Function

LastPageWithContent

Public Function Document_LastPageWithContent(ByRef objDocument As Word.Document) As Integer

Dim iEndAdjustedPageNumber As Integer
Dim iLastPageNumber As Integer

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If modSpecific.TableExists(objDocument) Then

iLastPageNumber = CType(oAnalystCertRange.Information(Word.WdInformation.wdActiveEndPageNumber), Integer)

If (iLastPageNumber = 0) Then
iEndAdjustedPageNumber = CType(objDocument.Content.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber), Integer)
iLastPageNumber = (iEndAdjustedPageNumber - 2)
End If
End If

If iLastPageNumber > 0 Then iLastPageNumber -= 1

Return iLastPageNumber

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

NewDocument

Public Shared Function NewDocument(ByVal enNewDocumentType As zBET_enNewDocumentType, _
ByVal sFolderPath As String, _
ByVal sFileName As String, _
Optional ByVal sExtension As String = ".dot", _
Optional ByVal sAdditional As String = "") As _
Word.Document

Dim objDocument As Word.Document

Try
If clsError.ErrorFlag() = True Then Exit Function

gApplicationWord.StatusBar = "Creating the document from : " & _
sFolderPath & sFileName & _
sAdditional & sExtension & " ..."

objDocument = gApplicationWord.Documents.Add( _
Template:=sFolderPath & sFileName & sAdditional & sExtension, _
NewTemplate:=False, _
DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, _
Visible:=True)

gApplicationWord.StatusBar = ""

NewDocument = objDocument

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
objDocument = Nothing

If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Dim serrortext As String

If enNewDocumentType = zBET_enNewDocumentType.FromTemplate Then serrortext = "template"
If enNewDocumentType = zBET_enNewDocumentType.FromDocument Then serrortext = "document"

Call clsError.Handle("NewDocument", msCLASSNAME, _
"create a new document from the " & serrortext & ":" & _
gsCRLF & "'" & sFolderPath & sFileName & sAdditional & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

NewDocumentBlank

Public Shared Sub NewDocumentBlank()

Try
If clsError.ErrorFlag() = True Then Exit Sub

gApplicationWord.StatusBar = "Creating a new document..."

gApplicationWord.Documents.Add( _
Template:="", _
NewTemplate:=False, _
DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, _
Visible:=True)

gApplicationWord.StatusBar = ""

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("NewDocumentBlank", msCLASSNAME, _
"create a new blank document.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

OpenDocument

Public Shared Function OpenDocument(ByVal sFolderPath As String, _
ByVal sFileName As String, _
Optional ByVal sExtension As String = ".doc", _
Optional ByVal sAdditional As String = "", _
Optional ByVal bInformUser As Boolean = False) _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

gApplicationWord.StatusBar = "Opening the document : " & _
sFolderPath & sFileName & _
sAdditional & sExtension & " ..."

gApplicationWord.Documents.Open( _
FileName:=sFolderPath & sFileName & sAdditional & sExtension)

OpenDocument = True

gApplicationWord.StatusBar = ""

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("OpenDocument", msCLASSNAME, _
"open the following document:" & _
gsCRLF & "'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

Page_Count

Returns the number of printed pages in the document, excluding hidden text and revisions.
Public Function Document_PageCount(ByRef objDocument As Word.Document) As Integer

Dim objView As Word.View
Dim objRevisionsView As Word.WdRevisionsView
Dim bShowRevisionsAndComments As Boolean
Dim bShowAll As Boolean
Dim bShowHiddenText As Boolean
Dim iCount As Integer = 0

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

objView = objDocument.ActiveWindow.View
' Do not show Revisions or Comments
bShowRevisionsAndComments = objView.ShowRevisionsAndComments
objView.ShowRevisionsAndComments = False
' Show Final View
objRevisionsView = objView.RevisionsView
objView.RevisionsView = Word.WdRevisionsView.wdRevisionsViewFinal
bShowAll = objView.ShowAll ' Do not show hidden text, Note: must toggle the ShowAll property
bShowHiddenText = objView.ShowHiddenText

objView.ShowAll = False
objView.ShowHiddenText = False

' Note: ComputeStatistics is faster than using the Information object
iCount = objDocument.ComputeStatistics(Word.WdStatistic.wdStatisticPages)

objView.ShowRevisionsAndComments = bShowRevisionsAndComments
objView.RevisionsView = objRevisionsView
objView.ShowAll = bShowAll
objView.ShowHiddenText = bShowHiddenText

Return iCount

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

PropertyBuiltInCount

Public Shared Function PropertyBuiltInCount(Optional ByVal sDocumentName As String = "") _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyBuiltInCount = objDocumentProperties.Count

If sDocumentName = "" Then
sDocumentName = gApplicationWord.ActiveDocument.Name
End If

PropertyBuiltInCount = clszWorkAround.DocumentPropertyBuiltInCount(sDocumentName)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInCount", "clsDocument", _
"return the number of 'built-in' document properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

PropertyBuiltInExists

Public Shared Function PropertyBuiltInExists(ByVal sPropertyName As String, _
Optional ByVal sDocumentName As String = "") _
As Boolean

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim ipropertycount As Integer
'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationWord.Documents(sDocumentName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'For ipropertycount = 1 To objDocumentProperties.Count
' If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
' PropertyBuiltInExists = True
' Exit Function
' End If
'Next ipropertycount

If sDocumentName = "" Then
sDocumentName = gApplicationWord.ActiveDocument.Name
End If

PropertyBuiltInExists = _
clszWorkAround.DocumentPropertyBuiltInExists(sDocumentName, sPropertyName)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInExists", "clsDocument", _
"determine if the 'built-in' document property '" & sPropertyName & "' exists.", _
mobjCOMException, mobjException)
End If
End Try
End Function

PropertyBuiltInGet

Public Shared Function PropertyBuiltInGet(ByVal sPropertyName As String, _
Optional ByVal objDefaultValue As Object = Nothing, _
Optional ByVal sDocumentName As String = "") _
As Object

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyBuiltInGet = objDocumentProperties.Item(sPropertyName).Value


If sDocumentName = "" Then
sDocumentName = gApplicationWord.ActiveDocument.Name
End If

PropertyBuiltInGet = _
clszWorkAround.DocumentPropertyBuiltInGet(sDocumentName, sPropertyName, _
objDefaultValue)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyBuiltInGet", "clsDocument", _
"return the 'built-in' document property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

PropertyBuiltInSet

Public Sub DocProperty_BuiltInSet(ByRef objDocument As Word.Document, _
ByVal sPropertyName As String, _
ByVal sPropertyValue As String)

Dim objDocumentProperties As Office.DocumentProperties

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

objDocumentProperties = CType(objDocument.BuiltInDocumentProperties, Office.DocumentProperties)

For ipropertycount As Integer = 1 To objDocumentProperties.Count
If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
objDocumentProperties.Item(ipropertycount).Value = sPropertyValue
Exit Sub
End If
Next ipropertycount

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

PropertyCustomAdd

Public Sub DocProperty_CustomAdd(ByRef objDocument As Word.Document, _
ByVal sPropertyName As String, _
ByVal sPropertyValue As String)

Dim objDocumentProperties As Office.DocumentProperties

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

If (modWordObjectModel.Document_IsProtected(objDocument) = False) Then

objDocumentProperties = CType(objDocument.CustomDocumentProperties, Office.DocumentProperties)
objDocumentProperties.Add(sPropertyName, False, Office.MsoDocProperties.msoPropertyTypeString, sPropertyValue)
Else
Call modMessages.DocProperty_UnableToAddDocumentIsProtected(sPropertyName)
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"add the custom property '" & sPropertyName & "' " & _
"with the value '" & sPropertyValue & "' " & _
"in the active document.")
End Try
End Sub

PropertyCustomCount

Public Shared Function PropertyCustomCount(Optional ByVal sDocumentName As String = "") _
As Integer

Try
If clsError.ErrorFlag() = True Then Exit Function

'Dim objDocumentProperties As Office.DocumentProperties
'objDocumentProperties = _
' CType(gApplicationPowerPoint.Presentations(sPresentationName).BuiltInDocumentProperties, _
' Office.DocumentProperties)
'PropertyCustomCount = objDocumentProperties.Count

If sDocumentName = "" Then
sDocumentName = gApplicationWord.ActiveDocument.Name
End If

PropertyCustomCount = clszWorkAround.DocumentPropertyCustomCount(sDocumentName)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomCount", "clsDocument", _
"return the number of 'custom' document properties.", _
mobjCOMException, mobjException)
End If
End Try
End Function

PropertyCustomDelete

Public Shared Sub PropertyCustomDelete(ByVal sPropertyName As String, _
Optional ByVal sDocumentName As String = "")

Try
If clsError.ErrorFlag() = True Then Exit Sub


Call clszWorkAround.DocumentPropertyCustomDelete(sDocumentName, sPropertyName)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("PropertyCustomDelete", "clsDocument", _
"delete the 'custom' presentation property '" & sPropertyName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

PropertyCustomExists

Public Function DocProperty_CustomExists(ByRef objDocument As Word.Document, _
ByVal sPropertyName As String) As Boolean

Dim objDocumentProperties As Office.DocumentProperties

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

objDocumentProperties = CType(objDocument.CustomDocumentProperties, Office.DocumentProperties)

For ipropertycount As System.Int32 = 1 To objDocumentProperties.Count
If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
DocProperty_CustomExists = True
Exit Function
End If
Next ipropertycount

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Function

PropertyCustomGet

Public Function DocProperty_CustomGet(ByRef objDocument As Word.Document, _
ByVal sPropertyName As String, _
Optional ByVal bInformUser As Boolean = False) As Object

Dim objDocumentProperties As Office.DocumentProperties

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Return Nothing : Exit Function

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

objDocumentProperties = CType(objDocument.CustomDocumentProperties, Office.DocumentProperties)

For ipropertycount As Integer = 1 To objDocumentProperties.Count
If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
DocProperty_CustomGet = objDocumentProperties.Item(ipropertycount).Value
Tracer_Add2("DOCUMENT", "The document property '" & sPropertyName & "' exists.")
Exit Function
End If
Next ipropertycount

Tracer_Add2("DOCUMENT", "The document property '" & sPropertyName & "' does not exist.")
If (bInformUser = True) Then
modMessages.DocProperty_DoesNotExist(sPropertyName)
End If
Return ""

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"get the custom property '" & sPropertyName & "' " & _
"from the active document.", False)
Return ""
End Try
End Function

PropertyCustomSet

Public Sub DocProperty_CustomSet(ByRef objDocument As Word.Document, _
ByVal sPropertyName As String, _
ByVal sPropertyValue As String)

Dim objDocumentProperties As Office.DocumentProperties

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If (objDocument Is Nothing) Then objDocument = Document_GetActive()

objDocumentProperties = CType(objDocument.CustomDocumentProperties, Office.DocumentProperties)

For ipropertycount As Integer = 1 To objDocumentProperties.Count
If objDocumentProperties.Item(ipropertycount).Name = sPropertyName Then
objDocumentProperties.Item(ipropertycount).Value = sPropertyValue
Exit Sub
End If
Next ipropertycount

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

RulerDisplayed

Public Shared Function RulerDisplayed() As Boolean

Try

RulerDisplayed = gApplicationWord.ActiveWindow.ActivePane.DisplayRulers()

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("RulerDisplayed", "clsPowerPoint", _
"determine if the ruler is currently displayed..", _
gobjCOMException, gobjException)
End If
End Try
End Function

SaveDocument

Public Shared Sub SaveDocument(Optional ByVal sDocumentName As String = "")

Dim objdocument As Word.Document

Try
If clsError.ErrorFlag() = True Then Exit Sub

If sDocumentName <> "" Then
objdocument = gApplicationWord.Documents(sDocumentName)
Else
objdocument = gApplicationWord.ActiveDocument
End If

Call clszLateBindingWord.DocumentSave(objdocument)

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("SaveDocument", msCLASSNAME, _
"save the active document.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

SelectView

Public Shared Sub SelectView(ByVal sViewName As String)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Select Case sViewName
Case "Master"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdMasterView

Case "Normal"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdNormalView

Case "Outline"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdOutlineView

Case "PrintPreview"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintPreview

Case "Print"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView

Case "Web"
gApplicationWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView


Case Else
Call System.Windows.Forms.MessageBox.Show( _
"Something else", "Document_WindowSelect")

End Select

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("SelectView", msCLASSNAME, _
"select the view '" & sViewName & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

SettingsFile_Exists

Public Function SettingsFile_Exists(Optional ByVal bInformUser As Boolean = True) As Boolean

Try
Return modGeneral.File_Exists(SettingsFile_Return, bInformUser)

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return False
End Try
End Function

SettingsFile_Retrieve

Public Function SettingsFile_Retrieve() As clsSettingsFile

Dim objsettingsfile As clsSettingsFile

Try
If (modSpecific.SettingsFile_Exists(False) = False) Then

gSettingsFile = New clsSettingsFile

objsettingsfile = gSettingsFile

Call modSpecific.SettingsFile_Save()

Call Tracer_Add2("SYSTEM", "User Settings File - Created: " & objsettingsfile.UserProfileFolder & objsettingsfile.UserSettingsFileName)

Else
objsettingsfile = CType(modGeneral.XML_DeserializeFromXmlFile(SettingsFile_Return, SettingsFile_GetType), clsSettingsFile)

'default to no detailed logging
objsettingsfile.DisplayDetailedLogging = False
objsettingsfile.GrantFullAccess = False

Call Tracer_Add2("SYSTEM", "User Settings File - Read: " & objsettingsfile.UserProfileFolder & objsettingsfile.UserSettingsFileName)
End If

modGeneral.LogFile_Purge(objsettingsfile)
modGeneral.Tracer_Start("WORD", objsettingsfile)

Return objsettingsfile

Catch ex As Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Return Nothing
End Try
End Function

SettingsFile_Return

Public Function SettingsFile_Return() As String
Dim sfullfolderpath As String

sfullfolderpath = My.Settings.FOLDER_USERPROFILE.Replace("%APPLICATIONDATA%", _
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)) & _
"UserSettings.xml"

Return sfullfolderpath
End Function

SettingsFile_Save

Public Sub SettingsFile_Save()

Dim sfullfolderpath As String

Try

If (gSettingsFile Is Nothing) Then
If SettingsFile_Exists(True) = True Then
Call modMessages.SettingsFile_CannotBeUpdated()
End If
Exit Sub
Else
modGeneral.XML_SerializeToXmlFile(gSettingsFile, _
gSettingsFile.UserProfileFolder & gSettingsFile.UserSettingsFileName)

sfullfolderpath = My.Settings.FOLDER_USERPROFILE.Replace("%APPLICATIONDATA%", _
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)) & _
"UserSettings.xml"

Call Tracer_Add2("SYSTEM", "User Settings File - Saved: " & sfullfolderpath)
End If

Catch ex As 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