C# Snippets
HighlightRegionsICanEdit
public void Protection_HighlightRegionsICanEdit(bool blnShowShading, bool bInformUser = false, bool bLogMessage = false)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if (gApplicationWord.ActiveWindow != null)
gApplicationWord.ActiveWindow.View.ShadeEditableRanges = System.Convert.ToInt32(blnShowShading);
}
catch (Exception ex)
{
if ((bLogMessage == true))
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex, "change the 'highlight the regions I can edit' option in this document", bInformUser);
}
}
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 bool Document_IsProtected(ref Word.Document objDocument)
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if ((objDocument == null))
objDocument = Document_GetActive();
if ((objDocument.ProtectionType == Word.WdProtectionType.wdNoProtection))
return false;
else
return true;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
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
Protect
public bool Document_Protect(ref Word.Document objDocument, string sPassword, bool bInformUser, string sDocumentType = "")
{
string strTempID = "";
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if ((objDocument == null))
objDocument = Document_GetActive();
if ((sDocumentType.Length == 0))
sDocumentType = modIdentifyDocument.DocumentTemplate_ReturnType(objDocument);
if ((modWordObjectModel.Document_IsProtected(objDocument) == true))
{
if ((bInformUser == true))
modMessages.Message_Protection_DocAlreadyProtected();
return false;
}
if ((sDocumentType != clsIdentifyDocumentAs.DocumentType_NotSupported.ToString))
{
strTempID = modIdentifyDocument.DocumentProperty_TemplateVersion_Return(objDocument);
if ((modGeneral.String_IsNumeric(strTempID, true) == true))
{
if (!System.Convert.ToDouble(strTempID) < 2.0)
{
if (objDocument.ProtectionType == Word.WdProtectionType.wdNoProtection)
objDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading, false, (object)sPassword, false, false);
return true;
}
else
{
modMessages.Message_Protection_NoProtectionAppliedOldTemplate(strTempID);
return false;
}
}
else
// not a number so its not valid
return false;
}
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);
}
finally
{
}
}
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
Selection_IsProtected
public bool Selection_IsProtected(ref Word.Document objDocument, Word.Range objRange = null/* TODO Change to default(_) if this is not a reference type */)
{
bool bprotected;
Word.Table rngTable;
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if (objDocument.ProtectionType == Word.WdProtectionType.wdNoProtection)
{
bprotected = false;
return bprotected;
}
if ((objRange == null))
objRange = gApplicationWord.Selection.Range;
if ((modWordTables.Table_InOne(objRange) == true))
{
rngTable = objRange.Tables(1);
if ((rngTable.Range.Editors.Count == 0))
bprotected = true;
else
bprotected = false;
}
else
// If oRange.Editors.Count = 0 Or _
// CType(oRange.Information(Word.WdInformation.wdActiveEndPageNumber), Integer) = 1 Then
// End If
if (gApplicationWord.Selection.Editors.Count == 0)
// there are no editors so it must be protected
bprotected = true;
return bprotected;
}
catch (Exception ex)
{
modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod(), null/* TODO Change to default(_) if this is not a reference type */, ex);
}
}
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
UnProtect
public bool Document_Unprotect(ref Word.Document objDocument, string sPassword, bool bInformUser, string sDocumentType = "")
{
try
{
Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod().Name + " start");
if (My.Settings.ERROR_OCCURRED == true)
return;
if ((objDocument == null))
objDocument = Document_GetActive();
if ((sDocumentType.Length == 0))
sDocumentType = modIdentifyDocument.DocumentTemplate_ReturnType(objDocument);
if ((sDocumentType != clsIdentifyDocumentAs.DocumentType_NotSupported.ToString))
{
if (!(objDocument.ProtectionType == Word.WdProtectionType.wdNoProtection))
{
// If (gDocumentState Is Nothing) Then
// gDocumentState = New clsWordDocumentState
// End If
// gDocumentState.DocumentTrackRevisions = objDocument.TrackRevisions
objDocument.Unprotect((object)sPassword);
// If gDocumentState.DocumentTrackRevisions = True Then
// objDocument.TrackRevisions = False
// End If
if ((bInformUser == true))
{
}
}
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);
}
}
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
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top