Vb.net VSTO文档自定义:缺少控制表引用,除非先采取其他操作

Vb.net VSTO文档自定义:缺少控制表引用,除非先采取其他操作,vb.net,excel,deployment,vsto,Vb.net,Excel,Deployment,Vsto,所以我有一个解决问题的方法,但我并不真正理解这个问题,而且我的方法很粗糙。我有一个文档级自定义,可以插入自定义文档中未包含的其他文档中的图纸: Private Sub LabReportTemplateAdder() Dim ReportTemplate As Excel.Workbook CurrentRun = Marshal.GetActiveObject("Excel.Application") ReportTemplate = CurrentRun.Workbo

所以我有一个解决问题的方法,但我并不真正理解这个问题,而且我的方法很粗糙。我有一个文档级自定义,可以插入自定义文档中未包含的其他文档中的图纸:

Private Sub LabReportTemplateAdder()
    Dim ReportTemplate As Excel.Workbook
    CurrentRun = Marshal.GetActiveObject("Excel.Application")
    ReportTemplate = CurrentRun.Workbooks.Open("C:\Reports\Templates\" & LabReportListBox.SelectedItem())
    ReportTemplate.Worksheets(1).Move(Before:=Globals.ThisWorkbook.Sheets(5))
End Sub
实际上,在部署过程中,该脚本每次都可以正常工作。但是,当我尝试修改添加的模板(即从数据库添加信息)时,修改(许多不同的操作)都会失败,并出现缺少引用的错误:

此文档可能无法按预期运行,因为缺少以下控件:Sheet5。依赖此控件的数据将不会自动显示或更新,并且其他自定义功能将不可用。请与管理员或此文档的作者联系以获取进一步帮助

失败代码类型的示例如下:

Private Sub AllMaterialsAdder(xxDataGridView As DataGridView, CostColumnID As Double, InsertColumnID As Double, CountColumnID As Double, DescriptionIndex As Integer, CostIndex As Integer)
    CurrentSheet = Globals.ThisWorkbook.ActiveSheet
    If CurrentSheet.Name = NameSet Then 'this is abbreviated test to check make sure only the sheets we need are added
        MsgBox("The active sheet isn't a Lab Report.  It's " & CurrentSheet.Name & ".")
    Else
        Dim ItemCount As Double
        ItemCount = CurrentSheet.Cells(1, CountColumnID).value
        For Each row As DataGridViewRow In xxDataGridView.SelectedRows
            CurrentSheet.Cells((4 + ItemCount), InsertColumnID).value = xxDataGridView.Item(DescriptionIndex, row.Index).Value
            CurrentSheet.Cells((4 + ItemCount), CostColumnID).value = xxDataGridView.Item(CostIndex, row.Index).Value
            ItemCount = ItemCount + 1
        Next
    End If
End Sub

但是,如果我在添加一张工作表后调用它,它将失败,这无关紧要。我的简单解决方法是将其添加到
LabReportTemplateAdder
sub中,但我仍然不明白它为什么会失败,以及为什么会修复它。很明显,这些工作表是存在的,但我不知道这是否与修改工作表索引有关,或者工作表是否需要注册到类似的地方

如果有更好的解决方案,我正在寻找一个更好的解决方案,并解释这里真正失败的地方。
谢谢

编辑: 我现在在更多的地方遇到了这种情况,而且,这方面的工作似乎很粗糙。下面是一个完整的错误:

Microsoft.VisualStudio.Tools.Applications.Runtime.ControlNotFoundException: 
This document might not function as expected because the following control is missing:
Sheet5. Data that relies on this control will not be automatically displayed or updated, 
and other custom functionality will not be available. Contact your administrator or the 
author of this document for further assistance. ---> 
System.Runtime.InteropServices.COMException: Programmatic access to the Microsoft Office 
Visual Basic for Applications project system could not be enabled. If Microsoft Office 
Word or Microsoft Office Excel is running, it can prevent programmatic access from being 
enabled. Exit Word or Excel before opening or creating your project.

  at Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemProvider.GetHostObject(String primaryType, String primaryCookie, IntPtr& hostObject)

   at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.Office.Tools.IHostItemProvider.GetHostObject(Type primaryType, String primaryCookie)

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetObjects()

   --- End of inner exception stack trace ---

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetObjects()

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetPrimaryControl()

   at Microsoft.Office.Tools.Excel.WorksheetImpl.get_Cells()

   at Microsoft.Office.Tools.Excel.WorksheetBase.get_Cells()

基于您正在插入工作表的事实。

我也看到了。我也许应该把它加入讨论。强制添加的工作表只放在文档定制中的所有工作表之后似乎比添加虚拟变量要粗糙得多,我仍然不知道出了什么问题。此外,所有内容都是最新的。此外,发布在那里的解决方案似乎并不总是有效。由于包含错误代码,它无法工作。
Dim currentcount As Int32 = Globals.HistologyLaborSummaryWorksheet.Cells(2, 11).value
Microsoft.VisualStudio.Tools.Applications.Runtime.ControlNotFoundException: 
This document might not function as expected because the following control is missing:
Sheet5. Data that relies on this control will not be automatically displayed or updated, 
and other custom functionality will not be available. Contact your administrator or the 
author of this document for further assistance. ---> 
System.Runtime.InteropServices.COMException: Programmatic access to the Microsoft Office 
Visual Basic for Applications project system could not be enabled. If Microsoft Office 
Word or Microsoft Office Excel is running, it can prevent programmatic access from being 
enabled. Exit Word or Excel before opening or creating your project.

  at Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemProvider.GetHostObject(String primaryType, String primaryCookie, IntPtr& hostObject)

   at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.Office.Tools.IHostItemProvider.GetHostObject(Type primaryType, String primaryCookie)

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetObjects()

   --- End of inner exception stack trace ---

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetObjects()

   at Microsoft.Office.Tools.Excel.WorksheetImpl.GetPrimaryControl()

   at Microsoft.Office.Tools.Excel.WorksheetImpl.get_Cells()

   at Microsoft.Office.Tools.Excel.WorksheetBase.get_Cells()