C# Snippets


HighlightRegionsICanEdit

Public Sub Protection_HighlightRegionsICanEdit(ByVal blnShowShading As Boolean, _
Optional ByVal bInformUser As Boolean = False, _
Optional ByVal bLogMessage As Boolean = False)

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

If gApplicationWord.ActiveWindow IsNot Nothing Then
gApplicationWord.ActiveWindow.View.ShadeEditableRanges = CType(blnShowShading, Integer)
End If

Catch ex As System.Exception
If (bLogMessage = True) Then
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"change the 'highlight the regions I can edit' option in this document", bInformUser)
End If
End Try
End Sub

IsProtected

Public Function Document_IsProtected(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 Is Nothing) Then objDocument = Document_GetActive()

If (objDocument.ProtectionType = Word.WdProtectionType.wdNoProtection) Then
Return False
Else
Return True
End If

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

IsProtected

Public Function Selection_IsProtected(ByRef objDocument As Word.Document, _
Optional ByVal objRange As Word.Range = Nothing) As Boolean

Dim bprotected As Boolean
Dim rngTable As Word.Table

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

If objDocument.ProtectionType = Word.WdProtectionType.wdNoProtection Then
bprotected = False
Return bprotected
End If

If (objRange Is Nothing) Then
objRange = gApplicationWord.Selection.Range
End If

If (modWordTables.Table_InOne(objRange) = True) Then
rngTable = objRange.Tables(1)

If (rngTable.Range.Editors.Count = 0) Then
bprotected = True
Else
bprotected = False
End If

Else
'If oRange.Editors.Count = 0 Or _
' CType(oRange.Information(Word.WdInformation.wdActiveEndPageNumber), Integer) = 1 Then

'End If
If gApplicationWord.Selection.Editors.Count = 0 Then
'there are no editors so it must be protected
bprotected = True
End If
End If

Return bprotected

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

Protect

Public Function Document_Protect(ByRef objDocument As Word.Document, _
ByVal sPassword As String, _
ByVal bInformUser As Boolean, _
Optional ByVal sDocumentType As String = "") _
As Boolean

Dim strTempID 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 (sDocumentType.Length = 0) Then
sDocumentType = modIdentifyDocument.DocumentTemplate_ReturnType(objDocument)
End If

If (modWordObjectModel.Document_IsProtected(objDocument) = True) Then
If (bInformUser = True) Then
Call modMessages.Message_Protection_DocAlreadyProtected()
End If
Return False
End If

If (sDocumentType <> clsIdentifyDocumentAs.DocumentType_NotSupported.ToString) Then

strTempID = modIdentifyDocument.DocumentProperty_TemplateVersion_Return(objDocument)

If (modGeneral.String_IsNumeric(strTempID, True) = True) Then
If Not CSng(strTempID) < 2.0 Then
If objDocument.ProtectionType = Word.WdProtectionType.wdNoProtection Then

objDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading, False, _
CType(sPassword, Object), False, False)

End If
Return True

Else
Call modMessages.Message_Protection_NoProtectionAppliedOldTemplate(strTempID)
Return False
End If
Else
'not a number so its not valid
Return False
End If
Else
Return False
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
Finally
'gDocumentState.DocumentTrackRevisions = False
'gDocumentState = Nothing
End Try
End Function

UnProtect

Public Function Document_Unprotect(ByRef objDocument As Word.Document, _
ByVal sPassword As String, _
ByVal bInformUser As Boolean, _
Optional ByVal sDocumentType 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 (sDocumentType.Length = 0) Then
sDocumentType = modIdentifyDocument.DocumentTemplate_ReturnType(objDocument)
End If

If (sDocumentType <> clsIdentifyDocumentAs.DocumentType_NotSupported.ToString) Then

If Not (objDocument.ProtectionType = Word.WdProtectionType.wdNoProtection) Then
'If (gDocumentState Is Nothing) Then
' gDocumentState = New clsWordDocumentState
'End If
'gDocumentState.DocumentTrackRevisions = objDocument.TrackRevisions

objDocument.Unprotect(CType(sPassword, Object))

'If gDocumentState.DocumentTrackRevisions = True Then
' objDocument.TrackRevisions = False
'End If

If (bInformUser = True) Then
'message that document has been unprotected
End If
End If

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

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