2012-09-04 61 views
4

我正在開發使用C#(.NET 4.0)的Excel 2007的應用程序級AddIn。與此類似的問題已經被以前問>>獲取C#中NamedRange控件的句柄

https://stackoverflow.com/questions/11374023/resizing-namedrange-throws-a-controlnotfoundexception

C# excel addin - Accessing Controls

,但他們的反應(其中有任何)並沒有爲我工作。所以我想詳細說明我想要做什麼,以及當我嘗試去做時會發生什麼。

簡而言之,我的問題是,我無法獲得NamedRange控件對象的修改,因爲它們已經在代碼中的其他地方被實例化和初始化。

請仔細閱讀以獲得更完整的描述。

當外接程序啓動時,它會創建一個NamedRange控件並將它附加到某些細胞在特定的工作表>>

Worksheet worksheet = Globals.Factory.GetVstoObject(
       Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet); 
Excel.Range cell = worksheet.Range["A1"]; 
_configParams.HeaderNamedRange = worksheet.Controls.AddNamedRange(cell, "HeaderCells"); 
_configParams.HeaderNamedRange.RefersTo = "=" + worksheet.Name + "!$A$1:$A$5"; 

外接程序啓動後,我想允許用戶重新指定這個NamedRange控件可以指向的單元格。爲了做到這一點,我讓用戶選擇一系列單元格,然後在合適的事件處理程序方法中獲取所選單元格的位置(作爲字符串)。

然後,我打算以編程方式重新指定上述NamedRange控件指向的單元格。我嘗試了以下方法,但都沒有工作。

[方法1]

Worksheet controlWorksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet); 
((NamedRange)(controlWorksheet.Controls["HeaderCells"])).RefersTo = newHeaderCellsLocation; 

在運行上面的代碼,它將引發ControlNotFoundException,我得到以下信息:

Microsoft.VisualStudio.Tools.Applications.Runtime.ControlNotFoundException was unhandled by user code 
    Message=This document might not function as expected because the following control is missing: PortfolioHeaderCells. 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. 
    Source=Microsoft.Office.Tools.Excel.Implementation 
    StackTrace: 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.GetObjects() 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.EnsureProxy() 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.get_Proxy() 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.AttachSupport() 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.ResetControl() 
     at Microsoft.Office.Tools.Excel.NamedRangeImpl.set_RefersTo(String value) 
     at ExcelListenerAddIn.Configuration.ListenerConfigManager.ActivateListenerConfiguration(ListenerConfigParams newConfiguration, Boolean cameFromConfigFile) in C:\Users\jag201\Documents\Visual Studio 2010\Projects\PoC\ExcelListenerAddIn\Configuration\ListenerConfigManager.cs:line 97 
     at ExcelListenerAddIn.TaskPanes.ListenerConfigurator._activateNewConfigButton_Click(Object sender, EventArgs e) in C:\Users\jag201\Documents\Visual Studio 2010\Projects\PoC\ExcelListenerAddIn\TaskPanes\ListenerConfigurator.cs:line 131 
     at System.EventHandler.Invoke(Object sender, EventArgs e) 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    InnerException: System.Runtime.InteropServices.COMException 
     Message=Exception from HRESULT: 0x800A03EC 
     Source=Microsoft.VisualStudio.Tools.Office.Runtime 
     ErrorCode=-2146827284 
     StackTrace: 
      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.NamedRangeImpl.GetObjects() 
     InnerException: 

[方法2]

我然後看到Set Excel Named Ranges via C#?等試圖用回答中描述的方法有:

Worksheet controlWorksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet); 
controlWorksheet.Names.Item("HeaderCells", Type.Missing, Type.Missing).RefersTo = newHeaderCellsLocation; 

在運行上面的代碼,我得到以下異常和消息:

System.Runtime.InteropServices.COMException was unhandled by user code 
    Message=Exception from HRESULT: 0x800A03EC 
    Source="" 
    ErrorCode=-2146827284 
    StackTrace: 
     at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) 
     at Microsoft.Office.Interop.Excel.Names.Item(Object Index, Object IndexLocal, Object RefersTo) 
     at ExcelListenerAddIn.Configuration.ListenerConfigManager.ActivateListenerConfiguration(ListenerConfigParams newConfiguration, Boolean cameFromConfigFile) in C:\Users\jag201\Documents\Visual Studio 2010\Projects\PoC\ExcelListenerAddIn\Configuration\ListenerConfigManager.cs:line 97 
     at ExcelListenerAddIn.TaskPanes.ListenerConfigurator._activateNewConfigButton_Click(Object sender, EventArgs e) in C:\Users\jag201\Documents\Visual Studio 2010\Projects\PoC\ExcelListenerAddIn\TaskPanes\ListenerConfigurator.cs:line 131 
     at System.EventHandler.Invoke(Object sender, EventArgs e) 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    InnerException: 

因此,在這兩種方法,從HRESULT異常是一樣的:0x800A03EC。另外,在我試圖獲得NamedRange控件並重新指定它指向的單元格的行中拋出異常。

我已經遍歷了我的代碼,並且NamedRange控件確實存在於/當我嘗試修改NamedRange對象時。

我然後使用以下代碼>>

Worksheet controlWorksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet); 
System.Windows.Forms.MessageBox.Show((controlWorksheet.Controls["HeaderCells"]).ToString()); 

這導致含有「Microsoft.Office.Tools.Excel.NamedRangeImpl」一個MessageBox檢查controlWorksheet.Controls["HeaderCells"]對象的類型。如果我沒有弄錯,這個類型不會被Excel對象模型暴露出來,所以我不知道這個問題是否是由於我將一個NamedRangeImpl對象轉換爲一個NamedRange控件對象而引起的。

非常感謝您的幫助。如果有任何我可以提供的進一步信息,請告訴我。提前致謝!

+1

如果你只是更新範圍內的單元格,這個答案有幫助嗎? http://stackoverflow.com/a/9792388/2258 –

+0

@RichardMorgan:我必須使用NamedRange控件,而不是爲Excel範圍創建一個命名別名。 NamedRange控件的事件將通過事件處理程序方法進行連接。因此,這裏的問題與將預先存在的命名別名附加到另一組單元格的問題不同。 –

回答

0

返回的對象類型沒有問題。 「Microsoft.Office.Tools.Excel.NamedRangeImp」類型旨在通過「Microsoft.Office.Tools.Excel.NamedRange」接口訪問。

0x800A03EC的COM異常向我建議,excel不允許你執行請求的操作(這也是你會得到的錯誤代碼,例如,如果你嘗試發送一個字符串值給一個會超過字符限制)。

是沒有問題的設置引用關聯屬性幾次,當我通過運行下面的代碼進行驗證:

 Microsoft.Office.Tools.Excel.Worksheet worksheet = Globals.Factory.GetVstoObject(
      Globals.MercuryAddIn.Application.ActiveWorkbook.ActiveSheet); 
     Excel.Range cell = worksheet.Range["A1"]; 
     Microsoft.Office.Tools.Excel.NamedRange r = worksheet.Controls.AddNamedRange(cell, "HeaderCells"); 
     r.RefersTo = "=" + worksheet.Name + "!$A$1:$A$5"; 

     r.RefersTo = "=" + worksheet.Name + "!$C$1:$C$5"; 

所以,問題在於必須包含在你的「newHeaderCellsLocation」變量中的字符串中。 RefersTo必須設置爲範圍的有效公式。你可以仔細檢查一下字符串的格式是否正確?

+0

嗨Franchesca。這已經很晚了,但是謝謝你的回覆。當我提出這個問題時,我並沒有在我所處的角色/職位上工作。但再次感謝您對此進行調查並回答我的問題:)。 –