This tip provides a solution to the problem of automatizing Excel file creation without Azure bugging the user to manually select the protection label.
Introduction
This article solves the problem of automatizing Excel file creation without Azure bugging the user to manually select the protection label. I have been struggling with this for quite some time now - I have a bunch of batch scripts which are producing CSV files and converting them into Excel files; but when my company started using Azure Information Protection (AIP), the scripts would pause before saving the Excel file requesting a manual input of a label.
I have been looking for this solution for quite some time. There are some workaround solutions on the Internet, including using a prelabelled Excel files, Powershell and dummy emails. The usage of prelabelled blank Excel files seemed like a plausible solution, but when the files got changed, Azure required entering a label again.
I found this in the official Microsoft documentation, but it is very scarce and you basically have to figure it out on your own.
The concrete solution shown is for Excel workbook, but it should be applicable also to any other Office document.
Using the Code
This is all the code you need after you have edited your objWorkbook
(Excel.Workbook
object) and are ready to save.
(Note #1: SensitivityLabel.SetLabel
requires a context
object, it can practically be anything if you are not using it, I chose dictionary
).
(Note #2: You can add other properties to your LabelInfo
, you can find it in the official Microsoft documentation here).
Dim myLabelInfo, context
Set myLabelInfo = objWorkbook.SensitivityLabel.CreateLabelInfo()
Set context = CreateObject("Scripting.Dictionary")
With myLabelInfo
.AssignmentMethod = 1
.IsEnabled = True
.LabelId = "--your labelID--"
.LabelName = "--your label name--"
.SetDate = Now()
End With
objWorkbook.SensitivityLabel.SetLabel myLabelInfo, context
Naturally, you need to change --your labelID-- with the actual label ID, and --your label name-- with an actual label name.
You can find this out by asking your company's administrator (whoever set up Azure) - or you can check for your own by using Powershell cmdlets:
You first find an office file with a label that you need, let's say, for example that the file you found is C:\myFile.xlsx; this is how you will find the label info for that file in Powershell:
Get-AIPFileStatus "C:\myFile.xlsx"
If you want to set a label using PS cmdlet, you can try this:
Set-AIPFileLabel -Path "C:\myFile.xlsx" -LabelId "--your labelID--"
History
- 1st February, 2022: Initial version