• If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.
  • Excel articles and downloadable files provided in the articles have not been reviewed by MrExcel Publishing. Please apply the provided methods / codes and open the files at your own risk. If you have any questions regarding an article, please use the Article Discussion section.
Jeffrey Mahoney

Change Sensitivity Label Programatically

Excel Version
  1. 365
  2. 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


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
Author
Jeffrey Mahoney
Views
11,393
First release
Last update
Rating
0.00 star(s) 0 ratings

More Excel articles from Jeffrey Mahoney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top