此命令不可用,因为使用此应用程序的许可证已过期- Excel自动化

zc0qhyus  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(430)

我有一个. NET Windows应用程序(它是用VB.Net编写的,但用C#也会发生同样的情况),在这个应用程序中我遇到了一个This command is unavailable because the license to use this application has expired错误。
此错误的堆栈跟踪为:at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.Select()
失败的代码行是:wrksht.Range("A2").Select()。它并不总是失败!它可以在失败前工作几次,也可以在第一次调用时失败。
要了解该过程的工作原理:
1.打开Word文档
1.找到图表(通过形状)
1.图表数据即被激活
1.访问工作表

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

Windows应用程序正在终端服务示例上运行,Office 365正在使用共享激活许可证(已激活)。同一应用程序在使用Office 365的独立PC上成功运行,但使用标准许可证。
有什么想法吗?

    • 附加信息**

我在Word中创建了一个简单的宏来绕过任何. Net/应用程序问题。宏每次在.Select上都会出错。但是,如果我将.Select更改为.FormulaR1C1 = "TEST",它就会完成。事实上,各种Word和Excel自动化都在工作,似乎只有在使用Word图表对象的工作表时,.Select才会出现问题。
参见代码:

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
    • 进一步调查结果**

Office许可证有效且已激活,但是,当Word打开Excel(通过chrt.ChartData.Activate命令)时,Excel的标题栏中显示"未授权的产品"。如果手动打开Excel,则显示为已授权且已激活。下图显示Word打开Excel时显示的内容:

如果我选择从Word手动设置图表的数据源(不使用VBA),Excel将打开,并且标题栏中没有未授权产品消息。

yqlxgs2m

yqlxgs2m1#

这不是答案,但可能有用,它展示了如何在不使用允许启用Option Strict的后期绑定的情况下做类似的事情。

添加COM引用(项目=〉添加引用... =〉COM)

  • 微软Excel xx.x对象库
  • 微软Word xx.x对象库
    添加以下Imports语句
  • Imports Microsoft.Office.Interop
Private Sub Test(filename As String)
    Dim excelWorkbook As Excel.Workbook = Nothing
    Dim excelWorksheet As Excel.Worksheet = Nothing
    Dim wordApp As Word.Application = Nothing
    Dim wordDoc As Word.Document = Nothing

    If Not System.IO.File.Exists(filename) Then
        Throw New Exception($"Error - '{filename}' doesn't exist.")
    End If

    Try
        Debug.WriteLine($"filename: '{filename}'")

        'create new instance
        wordApp = New Word.Application()

        'open Word document
        wordDoc = wordApp.Documents.Open(DirectCast(filename, Object))

        Dim chrt As Word.Chart = Nothing

        If wordDoc.InlineShapes.Count > 0 Then
            For Each oShape As Word.InlineShape In wordDoc.InlineShapes
                chrt = oShape.Chart

                Debug.WriteLine($"Title: '{chrt.ChartTitle.Text}' ChartType: {chrt.ChartType.ToString()}")

                If chrt IsNot Nothing Then
                    chrt.ChartData.Activate()
                    excelWorkbook = DirectCast(chrt.ChartData.Workbook, Excel.Workbook)
                    excelWorksheet = DirectCast(excelWorkbook.Worksheets(1), Excel.Worksheet)

                    Debug.WriteLine($"Value: '{excelWorksheet.Range("A2").Cells().Value}'")

                    excelWorksheet.Range("B2").Select()
                    'excelWorksheet.Range("A2").FormulaR1C1 = "Test 1"
                End If
            Next
        End If
    Catch ex As Exception
        Debug.WriteLine($"Error - {ex.Message}")
        Throw
    Finally
        If excelWorksheet IsNot Nothing Then
            'release all resources
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet)

            excelWorksheet = Nothing
        End If

        If excelWorkbook IsNot Nothing Then
            excelWorkbook.Close(DirectCast(False, Object))

            'release all resources
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook)

            excelWorkbook = Nothing
        End If

        If wordDoc IsNot Nothing Then
            wordDoc.Close(DirectCast(False, Object))

            'release all resources
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordDoc)

            wordDoc = Nothing
        End If

        If wordApp IsNot Nothing Then
            wordApp.Quit()

            'release all resources
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp)

            wordApp = Nothing
        End If
    End Try
End Sub

用法(假设有一个名为“ButtonRun”的按钮)

Private Sub ButtonRun_Click(sender As Object, e As EventArgs) Handles ButtonRun.Click
    Using ofd As OpenFileDialog = New OpenFileDialog()
        ofd.Filter = "Word Document (*.docx)|*.docx"

        If ofd.ShowDialog() = DialogResult.OK Then
            Test(ofd.FileName)
        End If
    End Using
End Sub

资源

相关问题