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.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Excel does not have a built-in "sensitivity label" feature so you must need something that is customized. What form would you like this label to take? File metadata (custom property)? Watermark? Header? Footer? Part of the file name?
 
Upvote 0
Seems it's an IT tech supported thing

 
Upvote 0
I was not familiar with this. It appears to be a feature that is added on at the enterprise level, not something that is a built-in Excel feature. Sorry, but I can't even research it if it's not available in my environment.
 
Upvote 0
Excel does not have a built-in "sensitivity label" feature so you must need something that is customized. What form would you like this label to take? File metadata (custom property)? Watermark? Header? Footer? Part of the file name?
Custom property
 
Upvote 0
Yes enterprise level! No Problem
I was not familiar with this. It appears to be a feature that is added on at the enterprise level, not something that is a built-in Excel feature. Sorry, but I can't even research it if it's not available in my environment.
 
Upvote 0
If a sensitivity label hasn't been applied to it as of yet then I found the easiest way to do this is to create a template and set the sensitivity label in it. I then just import the data into the template and save as. It will save the template sensitivity settings with it.
 
Upvote 0
So, I got it to work with the following code, derived from the example here:

VBA Code:
Dim lblInfo As Office.LabelInfo
Dim noSenseLabel As SensitivityLabel
Set xlSQL = CreateObject("Excel.Application")
xlSQL.Visible = False
'xlSQL.Visible = True

If xlSQS Is Nothing Then
    Set xlSQS = xlSQL.Workbooks.Add
    Set noSenseLabel = xlSQS.SensitivityLabel
    Set lblInfo = noSenseLabel.CreateLabelInfo()
    
    With lblInfo
        .AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
        .LabelId = "9203368f-916c-4d59-8292-9f1c6a1e8f39"
        .LabelName = "MyLabelName"
        .SiteId = "6c15903a-880e-4e17-818a-6cb4f7935615"
    End With
    
    noSenseLabel.SetLabel lblInfo, lblInfo
    
    xlSQS.SaveAs xlFn, 51

    'Set noSenseLabel = xlSQS.SensitivityLabel
    'Set lblInfo = noSenseLabel.GetLabel()
    'Debug.Print lblInfo.LabelId
    'Debug.Print lblInfo.LabelName
    'Debug.Print lblInfo.SiteId

Else
End If

The trick is getting the LabelId and SiteId. As long as the file or document is "Visible," the prompt to manually select the Sensitivity Level will show up after the attempt to save the document. The commented-out code should help you get that info for your enterprise, after the initial doc is saved. Then you can just replace with your company's info.
 
Upvote 0
So, I got it to work with the following code, derived from the example here:

VBA Code:
Dim lblInfo As Office.LabelInfo
Dim noSenseLabel As SensitivityLabel
Set xlSQL = CreateObject("Excel.Application")
xlSQL.Visible = False
'xlSQL.Visible = True

If xlSQS Is Nothing Then
    Set xlSQS = xlSQL.Workbooks.Add
    Set noSenseLabel = xlSQS.SensitivityLabel
    Set lblInfo = noSenseLabel.CreateLabelInfo()
   
    With lblInfo
        .AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
        .LabelId = "9203368f-916c-4d59-8292-9f1c6a1e8f39"
        .LabelName = "MyLabelName"
        .SiteId = "6c15903a-880e-4e17-818a-6cb4f7935615"
    End With
   
    noSenseLabel.SetLabel lblInfo, lblInfo
   
    xlSQS.SaveAs xlFn, 51

    'Set noSenseLabel = xlSQS.SensitivityLabel
    'Set lblInfo = noSenseLabel.GetLabel()
    'Debug.Print lblInfo.LabelId
    'Debug.Print lblInfo.LabelName
    'Debug.Print lblInfo.SiteId

Else
End If

The trick is getting the LabelId and SiteId. As long as the file or document is "Visible," the prompt to manually select the Sensitivity Level will show up after the attempt to save the document. The commented-out code should help you get that info for your enterprise, after the initial doc is saved. Then you can just replace with your company's info.
Hello rmagedyn,
I have the same problem, but i couldn't get your example to work. It seems the setting for xlSQS is missing. Any help would be really appreciated.

Thanks in advance
Bernd
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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