だから私は自分の問題を回避していますが、問題を本当に理解していません。私の回避策は粗雑です。カスタマイズされたドキュメントに含まれていない他のドキュメントからシートを挿入できるドキュメント レベルのカスタマイズがあります。
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
これは、開発中のコンピューターでは発生せず、展開時にのみ発生することに注意してください。これにより、この質問またはこの質問に関連する可能性があります。
私にとっては問題が少し違うように感じますが。まず、これを展開しようとしているすべてのマシンには、VSTO Tools for Office がインストールされています。次に、カスタマイズされたドキュメント内の名前付きシートの 1 つを呼び出すスクリプトを実行すると、機能します。シートに追加する前に価値のない変数を追加するだけで問題が解決するようです:
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()