Jeffrey Mahoney

Change Sensitivity Label Programatically

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
3,142
Office Version
  1. 365
Platform
  1. Windows
Jeffrey Mahoney submitted a new Excel article:

Change Sensitivity Label Programatically - Change Sensitivity Label Azure Company Policy VBA Macro

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...

Read more about this Excel article...
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I was looking out for this from long time.. Thank you for sharing it :)
 
It took me days to find some code and then trial and error until it worked. You're welcome
 
I would like to contribute on how I managed to solve it.
In my case, my code works to add sensitivity label with VBA Access, that is, Access opens a spreadsheet and assigns a sensitivity label.
To figure out which label to use, I created a spreadsheet and manually assigned it. Then, open the spreadsheet using the code and identify the ID to continue. I call with a buttons.


'--------------- ID identification
Sub GetSensitivityID()

Dim myLabelInfo As Office.LabelInfo
Dim XL As Object
Dim Cell As Variant
Set XL = CreateObject("Excel.Application")

With XL
.Visible = False
.displayalerts = False
.Workbooks.Open "C:\Users\CS33\Documents\4.PMO\3.Raizen\SGR\Arquivo não compilado\TemplateSafran.xlsx"
Set myLabelInfo = ActiveWorkbook.SensitivityLabel.GetLabel()
Rótulo190 = myLabelInfo.LabelId 'Este rótulo serve para verfificar o ID
.ActiveWorkbook.Close (True)
End With

Set XL = Nothing
'Set myLabelInfo = ActiveWorkbook.SensitivityLabel.GetLabel()
'Debug.Print myLabelInfo.LabelId
End Sub
'--------------- ID identification






'--------------- ID assignment
Dim XL As Object
Dim Cell As Variant
Set XL = CreateObject("Excel.Application")

With XL
.Visible = False
.displayalerts = False
.Workbooks.Open "C:\Users\CS33\Documents\4.PMO\3.Raizen\SGR\Arquivo não compilado\TemplateSafran.xlsx"




Dim myLabelInfo As Office.LabelInfo
Dim Context As Variant
Dim objWorkbook As Workbook
Dim sInterno As String

Set objWorkbook = ActiveWorkbook
Set myLabelInfo = objWorkbook.SensitivityLabel.CreateLabelInfo()
Set Context = CreateObject("Scripting.Dictionary")

sInterno = "0c6b9222-ff7d-4c6e-80fc-d2dead453d3c"

With myLabelInfo
.AssignmentMethod = MsoAssignmentMethod.PRIVILEGED '1
.ContentBits = 4
.IsEnabled = True
.Justification = "Because" 'Make this whatever you want
.LabelId = sInterno
.LabelName = LblName
.SetDate = Now()
End With

objWorkbook.SensitivityLabel.SetLabel myLabelInfo, Context




.ActiveWorkbook.Close (True)
.Quit
End With
Set XL = Nothing
'--------------- ID assignment
 
I would like to contribute on how I managed to solve it.
Well I'm not sure why you would use Access to set the sensitivity in Excel. Did you read the artice?
 
Yes!
I read the article and thank you. Helped me a lot.
My intention is just to demonstrate a different use.
To explain my case, I had to mention Access. I have an Access system that exports Excel spreadsheets, and in this case, I needed to assign sensitivity labels.
 
Is there a way to use VBA to assign the sensitivity label to a workbook that hasn't been opened? I ask because we have a number of people in my organization that need to pull data in Power Query from workbooks that are flagged with the most restrictive setting, and are encrypted until opened. As these workbooks are large, it would be better if I could code a sub that would get the original label ID and label name, use the values to reduce sensitivity while the query was being refreshed, and then immediately reset the sensitivity to the initial values. It may be a rather large ask, but I am hoping there's a better way than telling teammates that they will just have to open each 100+megabyte workbook in the background before running their Power Query.
 
A macro could be created to open all of them, assign the sensitivity, then save and close each. I know there are ways to read workbooks without opening them. This is a different animal.
 

Forum statistics

Threads
1,223,868
Messages
6,175,084
Members
452,611
Latest member
bls2024

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