2013-04-02 34 views
0

所以我有一個解決我的問題的方法,但我並不真正瞭解這個問題,而且我的解決方法很粗糙。我有一個文件層次的定製,可以從不包括定製的文檔在其他文檔中插入表:VSTO文檔自定義:缺少控制工作表參考除非首先採取其他操作

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 questionthis one相關。

雖然我覺得這個問題對我來說有點不同。首先,我試圖部署的所有機器都安裝了VSTO Tools for Office。其次,如果我運行一個腳本來調用自定義文檔中的一個命名錶格,它就可以工作。簡單地增加一個毫無價值的變量之前我曾經在添加紙張似乎來解決這個問題:

Dim currentcount As Int32 = Globals.HistologyLaborSummaryWorksheet.Cells(2, 11).value 

但是,如果我把那個叫板材已添加後不要緊就會失敗。我的簡單解決方法是將其添加到LabReportTemplateAdder分,但我仍不明白爲什麼它會失敗,以及爲什麼會修復它。很明顯,這些工作表是存在的,但我不知道它是否與修改工作表索引有關,或者工作表是否需要在類似於ROT problem I ran into earlier的地方註冊。

我正在尋找一個更好的解決方案,如果有一個,並解釋什麼是真的在這裏失敗。
謝謝。

編輯: 我現在在更多的地方遇到了這個問題,再次,工作變得很粗糙。這是一個完整的錯誤:基於這樣的事實,你是襯紙

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() 

回答

1
+0

我也看到了。我也許應該將其添加到討論中。強制添加的表單放置在文檔自定義中的所有元素之後,似乎比添加虛擬變量要粗糙得多,而且我仍然不知道發生了什麼問題。此外,一切都完全是最新的。 –

+0

此處發佈的解決方案似乎並不總能奏效。隨着包含的錯誤代碼,它不起作用。 –