- Excel Version
- 365
- 2021
This code will allow you to change a workbook's Sensitivity Label using VBA. It seems that each company gets to decide what those labels are. I also believe that each of those has a ID unique to that company. Each ID needs to be in this format:
"12345678-1234-1234-1234-1234567890AB" with alpha-numeric characters.
To find the ID's relatable to your company's Sensitivity Labels you will need to set the sensitivity in a workbook to each of the labels one at a time and run the GetSensitivityID macro after each change. The macro will display the ID in the Immediate window and you need to copy that. Paste that ID where each your variables set the value of the ID in the SetSensitivityLabel FUNCTION.
sPublic = "788c8a80-3a15-4016-b4c2-fc99999bfa99"
My company used Public, General, Non-Business and so forth. Once you have each of the IDs, you can call the SetSensitivityLabel Function providing the workbook and the label you want to apply.
I have changed and removed the IDs in the code below for obvious reasons. The SetSLabel SUB is an example of how to call the function. I use this to set new workbooks from my Quick Access Toolbar
"12345678-1234-1234-1234-1234567890AB" with alpha-numeric characters.
To find the ID's relatable to your company's Sensitivity Labels you will need to set the sensitivity in a workbook to each of the labels one at a time and run the GetSensitivityID macro after each change. The macro will display the ID in the Immediate window and you need to copy that. Paste that ID where each your variables set the value of the ID in the SetSensitivityLabel FUNCTION.
sPublic = "788c8a80-3a15-4016-b4c2-fc99999bfa99"
My company used Public, General, Non-Business and so forth. Once you have each of the IDs, you can call the SetSensitivityLabel Function providing the workbook and the label you want to apply.
I have changed and removed the IDs in the code below for obvious reasons. The SetSLabel SUB is an example of how to call the function. I use this to set new workbooks from my Quick Access Toolbar
VBA Code:
'WB: The workbook you want to change the Sensitivity on
'LblName: General
' Public
' Non-Business
' Confidential-NotProtected
' Confidential-ManualProtected
' Confidential-ProtectedABC
' Secret-ManuallyProtect
' Secret-ProtectedABC
Function SetSensitivityLabel(WB As Workbook, LblName As String)
Dim myLabelInfo As Office.LabelInfo
Dim Context As Variant
Dim objWorkbook As Workbook
Dim CurLabelID As String
Dim sPublic As String
Dim sGeneral As String
Dim sNonBusiness As String
Dim sConfNotProtect As String
Dim sConfManProtect As String
Dim sConfProtectBP As String
Dim sSecretManProtect As String
Dim sSecretProtectedBP As String
Set objWorkbook = ActiveWorkbook
Set myLabelInfo = objWorkbook.SensitivityLabel.CreateLabelInfo()
Set Context = CreateObject("Scripting.Dictionary")
sPublic = "788c8a80-3a15-4016-b4c2-fc99999bfa99"
sGeneral = ""
sNonBusiness = ""
sConfNotProtect = ""
sConfManProtect = ""
sConfProtectABC = ""
sSecretManProtect = ""
sSecretProtectedABC = ""
Select Case LblName
Case "General"
CurLabelID = sGeneral
Case "Public"
CurLabelID = sPublic
Case "Non-Business"
CurLabelID = sNonBusiness
Case "Confidential-NotProtected"
CurLabelID = sConfNotProtect
Case "Confidential-ManualProtected"
CurLabelID = sConfManProtect
Case "Confidential-ProtectedABC"
CurLabelID = sConfProtectABC
Case "Secret-ManuallyProtect"
CurLabelID = sSecretManProtect
Case "Secret-ProtectedABC"
CurLabelID = sSecretProtectedABC
End Select
With myLabelInfo
.AssignmentMethod = MsoAssignmentMethod.PRIVILEGED '1
.ContentBits = 4
.IsEnabled = True
.Justification = "Because" 'Make this whatever you want
.LabelId = CurLabelID
.LabelName = LblName
.SetDate = Now()
End With
objWorkbook.SensitivityLabel.SetLabel myLabelInfo, Context
End Function
Sub GetSensitivityID()
Dim myLabelInfo As Office.LabelInfo
Set myLabelInfo = ActiveWorkbook.SensitivityLabel.GetLabel()
Debug.Print myLabelInfo.LabelId
End Sub
VBA Code:
Sub SetSLabel()
SetSensitivityLabel ActiveWorkbook, "General"
End Sub