27-02-24 09:33 AM
Hey guys,
This is gonna be a pretty long post 😉
So lately my organization introduced mandatory sensitivity labels in Office 365 documents. We're unable to save any newly created document unless we set the label during saving (if no label is already present, of course). This is not a functionality supported in Excel VBO, so I started investigating the subject to come up with a way to implement that.
There is, of course, the Microsoft Information Protection VBO, but there are some downsides to it:
What I already have:
For now, this is what I came up with as a quick workaround:
Dim xlApp as Object = GetInstance(handle)
Dim xlwb as Object = GetWorkbook(handle,workbookname)
xlApp.DisplayAlerts = False
if xlwb.SensitivityLabel.getlabel().LabelId = "" Then
'add vba module
Dim vbamodule = xlwb.VBProject.VBComponents.Add(1)
'inject macro into the module we created
vbamodule.CodeModule.AddFromString(macro)
'run macro to change the sensitivity label
xlApp.Run(macro_name)
'erase our module
xlApp.VBE.ActiveVBProject.VBComponents.Remove(vbamodule)
End If
xlApp.DisplayAlerts = True
Sub setLabel()
On Error GoTo saveError
Dim label As SensitivityLabel
Dim labelInfo As Office.labelInfo
If (Application.ThisWorkbook.SensitivityLabel.GetLabel() = "") Then
Set label = Application.ThisWorkbook.SensitivityLabel
Set labelInfo = label.CreateLabelInfo()
With labelInfo
.AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
.LabelId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
.LabelName = "xxxxxxx"
.SiteId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
End With
label.setLabel labelInfo, labelInfo
End If
Exit Sub
saveError:
Dim fs, f, currentdte
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\temp\sensitivityLabel.log", 2, True)
f.WriteLine CStr(Now) + " - " + Err.Description
f.Close
End Sub
So basically we have the ability to set the label using a macro before saving the document and all is well in the world :)... *huge "BUT" incoming*This solution works for the most part. Not sure why, but in certain processes, under some circumstances, which I'm unable to pinpoint, this solution gives out an error:
Could not execute code stage because exception thrown by code stage: Can't perform operation since the project is protected.
Plus, it just seems... Hacky? Can't really say, that this is a stable, production-ready solution. At least not to my standards as a dev 😉 I feel we can do better here.
What I'm after:
I'm attempting to implement the whole thing as a code stage with no macro-injecting-hope-it-works-hoop-jumping.
The main issue here is this line of the macro code:
.AssignmentMethod = MsoAssignmentMethod.PRIVILEGED
My research shows, that it's connected to the Microsoft Office 16.0 Object Library, thus there's no problem to call it via VBA. Soo, I should be able to reference the MSO.dll in the Excel VBO's Initialize page and use it in my code, right? Well, no. The moment I add mso.dll to the references, this happens:
So far I also had no success in using the Microsoft.InformationProtection.dll to create a valid AssignmentMethod object.
So, my questions are:
1) How do you guys handle the Sensitivity Label in your work envs?
2) Do you have any idea how can I improve my solution to be more bullet-proof (in case of my workaround solution) or work at all? (in case of my "What I'm after" solution)?
3) Does BluePrism, as the provider of the Excel VBO, has any plans to actually address this problem (which is not exactly new, according to my findings)?
SOLUTION:
In case anyone's looking for a solution, here's the code stage (no additional references required):
Dim xlApp as Object = GetInstance(handle)
Dim xlwb as Object = GetWorkbook(handle,workbookname)
Dim sensLabel As Object = xlwb.SensitivityLabel
Dim labelInfo As Object = sensLabel.CreateLabelInfo()
If (sensLabel.GetLabel().LabelId() = "") Then
With labelInfo
.AssignmentMethod = 1
.LabelId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
.LabelName = "xxxxxx"
.SiteId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
End With
sensLabel.setLabel(labelInfo, labelInfo)
End If
This, of course, sets a given label just in case of lack of a set label in the workbook. You can modify/delete the If statement to alter this behavior 🙂
------------------------------
Tomasz Sadlon
RPA Dev
Nordea AB
Europe/Warsaw
------------------------------
Answered! Go to Answer.
28-02-24 11:23 AM
The GetLabel call results in a LabelInfo (COM)Object
In VBA this is an Object that will auto-resolve to a string(in fact the LabelId property of the LabelInfo Object)
Here you either try to explicitly call .ToString() on the end - if that does give the same outcome as VBA
If xlwb.SensitivityLabel.GetLabel().ToString() = ""
OR better yet, go for the known property that you want to test(which is LabelId I assume):
If xlwb.SensitivityLabel.GetLabel().LabelId = "" Then
27-02-24 12:34 PM
Hi T,
As a workaround, would it be feasible to not create a new document but instead open an existing template with the sensitivity label already set, populate that and then do a 'Save as'? The template can either be totally blank or contain template text, as long as the sensitivity label has already been assigned in it.
27-02-24 03:54 PM
Try getting rid of any reference to the Office dll. VB has the implicit typing/infer option set in BP, so try declare the Sensitivity & labelInfo variables as Object and hopefully their COM implementations stand up (some members can be mangled such as some sendermail stuff in Outlook, and the removeDuplicates method in Excel).
Put things back as they were, and can't you just use the value of the enum?
.AssignmentMethod = 1 'MsoAssignmentMethod.PRIVILEGED = 1
On another point wouldn't it be better to return error strings after catch and have the exception logging for all your VBO calls in a separate page?
I doubt BP will address this as their VBO is meant as a starter codeset - and very much needs your own considered enhancement.
27-02-24 04:22 PM
Hi Tom,
There is an asset for M365 - Excel in DX but unfortunately it doesn't have assign sensitivity label action. You need to create the custom action to assign the sensitivity label. Refer the below Graph API documentation to create the custom action.
DX Asset : Microsoft 365 - Excel - 1.5.0
Graph API : Graph API - assignSensitivityLabel
27-02-24 05:46 PM
I would go with Paul's suggestion to use a template.
27-02-24 09:08 PM
@Athiban Mahamathi Mathialagan,
Glad you pointed this out. For the longest time Microsoft only supported this under the beta release of the Graph API. Now that it shows up as part of the officially supported 1.0 release, we will look to add it to the connectors we have on the DX. 😉
Cheers,
27-02-24 09:11 PM
------------------------------
Leonardo Soares
RPA Developer
América/Brazil
------------------------------
28-02-24 10:04 AM
@TomaszSadlon I quickly knocked up an action to use this property as it hadn't been in use for me before.
Works for me as simple as said above.
Take out any referencing in the Initialize page re office dlls and declare as Objects. Then you simply need to use the Int value:
.AssignmentMethod = 1
In general, when trying to convert VBA code to VB.NET, just use the Integer value of any Enums (find by hovering over a started routine in VBA editor, or look it up on the docs page)
28-02-24 11:05 AM
Hey Anton,
I tried doing this before and failed... Maybe you could point out my mistake 🙂
This is the code stage:
Dim xlApp as Object = GetInstance(handle)
Dim xlwb as Object = GetWorkbook(handle,workbookname)
Dim sensLabel As Object
Dim labelInfo As Object
If (xlwb.SensitivityLabel.GetLabel() = "") Then
sensLabel = xlwb.SensitivityLabel
labelInfo = sensLabel.CreateLabelInfo()
With labelInfo
.AssignmentMethod = 1
.LabelId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
.LabelName = "xxxxx"
.SiteId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
End With
sensLabel.setLabel(labelInfo, labelInfo)
End If
Here are the references:
And this is the error I'm getting:
Internal : Could not execute code stage because exception thrown by code stage: Overload resolution failed because no Public '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean':
Argument matching parameter 'a' cannot convert from '__ComObject' to 'String'.
Any idea what am I missing?
28-02-24 11:23 AM
The GetLabel call results in a LabelInfo (COM)Object
In VBA this is an Object that will auto-resolve to a string(in fact the LabelId property of the LabelInfo Object)
Here you either try to explicitly call .ToString() on the end - if that does give the same outcome as VBA
If xlwb.SensitivityLabel.GetLabel().ToString() = ""
OR better yet, go for the known property that you want to test(which is LabelId I assume):
If xlwb.SensitivityLabel.GetLabel().LabelId = "" Then