所以我有一个解决我的问题的方法,但我并不真正理解这个问题,而且我的解决方法很粗糙。我有一个文档级别的自定义,可以插入自定义文档中未包含的其他文档的工作表:
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
或者
Private Sub MaterialSummaryUpdater()
CurrentSheet = Nothing
Globals.MaterialSummaryWorksheet.UsedRange(5, 26).Clear()
For Each Me.CurrentSheet In Globals.EOSWorkbook.Worksheets
If CurrentSheet.Name <> NameSet Then 'this is abbreviated test to check make sure only the sheets we need are added [excluding NameSet]
Dim CurrentCount1, CurrentCount2, CurrentCount3, MasterCount1, MasterCount2, MasterCount3 As Int32
CurrentCount1 = CurrentSheet.Cells(1, 28).Value
CurrentCount2 = CurrentSheet.Cells(1, 33).Value
CurrentCount3 = CurrentSheet.Cells(1, 39).Value
If CurrentCount1 > 0 Then
MasterCount1 = Globals.MaterialSummaryWorksheet.Cells(2, 3).Value
Globals.MaterialSummaryWorksheet.Range(Globals.MaterialSummaryWorksheet.Cells((5 + MasterCount1), 1), Globals.MaterialSummaryWorksheet.Cells((4 + MasterCount1 + CurrentCount1), 6)).Value = CurrentSheet.Range(CurrentSheet.Cells(4, 25), CurrentSheet.Cells((3 + CurrentCount1), 30)).Value
End If
If CurrentCount2 > 0 Then
MasterCount2 = Globals.MaterialSummaryWorksheet.Cells(2, 8).Value
Globals.MaterialSummaryWorksheet.Range(Globals.MaterialSummaryWorksheet.Cells((5 + MasterCount2), 7), Globals.MaterialSummaryWorksheet.Cells((4 + MasterCount2 + CurrentCount2), 10)).Value = CurrentSheet.Range(CurrentSheet.Cells(4, 31), CurrentSheet.Cells((3 + CurrentCount2), 35)).Value
End If
If CurrentCount3 > 0 Then
MasterCount3 = Globals.MaterialSummaryWorksheet.Cells(2, 13).Value
Globals.MaterialSummaryWorksheet.Range(Globals.MaterialSummaryWorksheet.Cells((5 + MasterCount3), 12), Globals.MaterialSummaryWorksheet.Cells((4 + MasterCount3 + CurrentCount3), 16)).Value = CurrentSheet.Range(CurrentSheet.Cells(4, 36), CurrentSheet.Cells((3 + CurrentCount3), 40)).Value
End If
End If
Next
End Sub
我应该注意,这不会发生在我正在开发的计算机上,而只会发生在部署中,这可能与this question或this one相关。
虽然我觉得这个问题对我来说有点不同。首先,我尝试在其上部署它的所有机器都安装了 VSTO Tools for Office。其次,如果我运行一个脚本来调用自定义文档中的一个命名表,它就可以工作。在我添加工作表之前简单地添加一个毫无价值的变量似乎可以解决问题:
Dim currentcount As Int32 = Globals.HistologyLaborSummaryWorksheet.Cells(2, 11).value
但是,如果我在添加工作表后调用它,那没关系它会失败。我的简单解决方法是将其添加到LabReportTemplateAdder
子中,但我仍然不明白为什么它会失败,以及为什么会修复它。显然工作表存在,但我不知道它是否与修改工作表索引有关,或者是否需要在类似于我之前遇到的 ROT 问题的地方注册工作表。
如果有一个更好的解决方案,我正在寻找一个更好的解决方案,并解释这里真正失败的原因。
谢谢。
编辑:我现在在更多地方遇到了这个问题,而且,解决方法似乎很粗糙。这是一个完整的错误:
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()