I have a .NET Windows application (which is written in VB.Net, but the same occurs with C#) where I am getting a This command is unavailable because the license to use this application has expired
error.
The stack trace to this error is:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.Select()
The line of code that is failing is: wrksht.Range("A2").Select()
. It does not always fail! It can work several times before failing or can fail the first time it is called.
To be clear about how the process works:
- A Word document is opened
- A chart is found (via a shape)
- The chart data is activated
- The worksheet is accessed
Dim chrt As _word.Chart = shp.Chart
chrt.ChartData.Activate()
Dim wrksht As _excel.Worksheet = chrt.ChartData.Workbook.WorkSheets(1)
wrksht.Range("A2").Select() 'Line that errors
The Windows application is running on a Terminal Services instance and Office 365 is using a Shared Activation License (which is activated). The same application runs successfully on a standalone PC using Office 365, but with a standard license.
Any ideas?
Additional Info
I have created a simple macro in Word to bypass any .Net/application issues. The macro errors every time on the .Select
. However, if I change the .Select
to .FormulaR1C1 = "TEST"
, then it completes. In fact all sorts of Word and Excel automation is working, it seems to only have issue with the .Select
once the worksheet for the Word chart object is used.
See code:
Sub Test()
Dim shp As InlineShape
Dim chrt As Chart
Dim wrksht As Excel.Worksheet
Set shp = getInlineShapeFromTitle("Score_Graph_Question_15964_4")
Set chrt = shp.Chart
chrt.ChartData.Activate
Set wrksht = chrt.ChartData.Workbook.Worksheets(1)
'wrksht.Range("A1").FormulaR1C1 = "TEST 1"
wrksht.Range("A1").Select
chrt.ChartData.Workbook.Application.Quit
End Sub
Private Function getInlineShapeFromTitle(title As String) As InlineShape
Dim shp As InlineShape
Dim shp1 As InlineShape
For Each shp1 In ActiveDocument.InlineShapes
If shp1.title = title Then
Set shp = shp1
Exit For
End If
Next
Set getInlineShapeFromTitle = shp
End Function
Further Findings
The Office license is valid and activated, however, when Word opens Excel (via the chrt.ChartData.Activate
command), Excel has "Unlicensed Product" in the title bar. If Excel is opened manually, it shows as licensed and activated. This image shows what is shown when Word opens Excel:
If I choose to set the datasource of the chart from Word manually (not using VBA), Excel opens and the unlicensed product message is not in the title bar.