VBA to set Sensitivity Label

adilsabirazeez

New Member
Joined
Mar 13, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am generating a set of excel files into a folder. I want to set the sensitivity label of all these files to a particular category like "Highly Confidential". I am not able to find VBA to automate this.

If anyone has any solution, then please post it.

Thanks in advance.
 
Hi,

I am generating a set of excel files into a folder. I want to set the sensitivity label of all these files to a particular category like "Highly Confidential". I am not able to find VBA to automate this.

If anyone has any solution, then please post it.

Thanks in advance.


I looked everywhere and could not find a straight forward answer. I got this working from info from all over the place and it should work for everyone. Drop these in a worksheet, make sure there is a sheet1. Use the get label info to get the info from the currently set label on YOUR sheet. Update the setLabelInfo subs with your own label information. Then you can use them to set/switch the sensitivity label as needed. I tried to keep these as simple as possible so you can modify as needed or copy the parts needed into your code.

VBA Code:
Sub SetLabelInfoINTERNAL()
     Dim myLabelInfo As LabelInfo
     Set wb = ThisWorkbook
     Set myLabelInfo = wb.SensitivityLabel.CreateLabelInfo()
     
     With myLabelInfo
     'put your information here ***********************
      .ActionId = "c0f18baa-5255-463b-8176-67e75ee5abc4"
      .AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
      .ContentBits = 2
      .IsEnabled = True
      .Justification = "Some justification needed only if downgrading label."
      .LabelId = "eae83a4f-cc5f-4276-a1aa-251cec914c6b"
      .LabelName = "Internal"
      .SetDate = Now()
      .SiteId = "d554a4b7-b1f6-4714-a568-fd4d48c146a6"
     End With
    
    Call wb.SensitivityLabel.setLabel(myLabelInfo, myLabelInfo)

End Sub


Sub SetLabelInfoEXTERNAL()
     Dim myLabelInfo As LabelInfo
     Set wb = ThisWorkbook
     Set myLabelInfo = wb.SensitivityLabel.CreateLabelInfo()
     
     With myLabelInfo
     'Put Your information here ***********************
      .ActionId = "e00c1536-0827-5aca-8e56-ef075b47683a"
      .AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
      .ContentBits = 2
      .IsEnabled = True
      .Justification = "Some justification needed only if downgrading label."
      .LabelId = "97be529e-63ab-4d1a-985e-0328b4465a07"
      .LabelName = "External"
      .SetDate = Now()
      .SiteId = "d584a8b7-b1f2-7714-a278-fd4d83c145a6"
     End With
    
    Call wb.SensitivityLabel.setLabel(myLabelInfo, myLabelInfo)

End Sub

Sub getLabelInfo()

    Dim senLabelInfo As Office.LabelInfo
    
    Set wb = ThisWorkbook
    Set senLabelInfo = wb.SensitivityLabel.GetLabel()
    Call wb.Sheets("Sheet1").Activate
    
    property1 = senLabelInfo.LabelName
    property2 = senLabelInfo.LabelId
    property3 = senLabelInfo.SiteId
    property4 = senLabelInfo.SetDate
    property5 = senLabelInfo.ActionId
    property6 = senLabelInfo.Application
    property7 = senLabelInfo.AssignmentMethod
    property8 = senLabelInfo.ContentBits
    property9 = senLabelInfo.Creator
    property10 = senLabelInfo.IsEnabled
    property11 = senLabelInfo.Justification
    
    Range("A1").Value = "Label Name"
    Range("B1").Value = property1
    Range("A2").Value = "Lablel ID"
    Range("B2").Value = property2
    Range("A3").Value = "Site ID"
    Range("B3").Value = property3
    Range("A4").Value = "Set Date"
    Range("B4").Value = property4
    Range("A5").Value = "ActionId"
    Range("B5").Value = property5
    Range("A6").Value = "Application"
    Range("B6").Value = property6
    Range("A7").Value = "AssignmentMethod"
    Range("B7").Value = property7
    Range("A8").Value = "ContentBits"
    Range("B8").Value = property8
    Range("A9").Value = "Creator"
    Range("B9").Value = property9
    Range("A10").Value = "IsEnabled"
    Range("B10").Value = property10
    Range("A11").Value = "Justification"
    Range("B11").Value = property11

End Sub
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi, are all these solutions given for Office 365?
Can I use it to set the sensitivity label on the Office 2016 Excel version? If not any code for this version?
Please help, I need to implement this for a program in VBA.
 
Upvote 0
The code below I posted in another thread worked for the OP for an Excel file

----------------------------------------------------------------------------------------------------------------

You can try saving the workbook manually after setting the label

then run the code below

VBA Code:
Sub getlabel()
    Dim myLabelInfo As Office.LabelInfo
    Dim labelpart1 As String, labelpart2 As String

    Set wb = ThisWorkbook

    Set myLabelInfo = wb.SensitivityLabel.getlabel()

    labelpart1 = myLabelInfo.LabelId
    labelpart2 = myLabelInfo.SiteId

    With Sheets("Sheet1")                        ' change Sheet1 to an empty sheet

        .Range("A1").Value = labelpart1 ' Label ID
        .Range("A2").Value = labelpart2 ' Site ID

    End With


End Sub


Then use the information it gives you as per the below followed by your save line in the main code in the new workbooks

VBA Code:
Dim docSenseLabel As SensitivityLabel
Dim labelInfo As Office.labelInfo

Set docSenseLabel = wb.SensitivityLabel
Set labelInfo = docSenseLabel.CreateLabelInfo()

With labelInfo
    .AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
    .LabelId = "the labelId you got"
    .LabelName = "Important"   'or whatever label you saved earlier
    .SiteId = "the site Id you got"
End With

docSenseLabel.setlabel labelInfo, labelInfo
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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