Our IT department applied a policy where we have to specify Sensitivity Label in Excel. I have successfully been able to do this in VBA. Does anyone know how to in c#, microsoft.interop.excel?
code for vba below
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
I found out how to do it. From my testing it doesn't work on .NET Framework (even 4.8.1), I tried on .NET 6.0 and it works out of the box.
Create a new Console application based on .NET 6.0, add a COM reference to Microsoft Office Excel 16 Object Library (if O365 is installed on our system you should just find it under the COM list).
Write your code (basically converting your VBA), start with something simple as:
then:
You should get intellisense while writing
SensitivityLabeland see a squiggly red line under it telling you that you need to reference something likeoffice, 15.0.0.0, ignore that and add another COM reference: Microsoft Office 16 Object Library.The squiggly red line will still be there, build the project and after that everything will be fine.
Unfortunately I was not able to find a reliable way for .NET Framework.