gpt4 book ai didi

c# - 在 C# 中获取 NamedRange 控件的句柄

转载 作者:行者123 更新时间:2023-11-30 13:03:38 25 4
gpt4 key购买 nike

我正在使用 C# (.NET 4.0) 为 Excel 2007 开发应用程序级加载项。以前有人问过类似的问题>>

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

C# excel addin - Accessing Controls

但对他们的回应(如果有的话)对我不起作用。所以我想详细描述一下我正在尝试做什么,以及当我尝试这样做时会发生什么。

简而言之,我的问题是在代码的其他地方实例化和初始化之后,我无法获取 NamedRange 控件对象来修改它们。

请继续阅读以获得更完整的描述。

当 AddIn 启动时,它会创建一个 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 控件指向的单元格。我尝试了以下方法,但均无效。

[方法一]

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:

[方法二]

然后我看到了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 控件确实存在。

然后我使用以下代码检查了 controlWorksheet.Controls["HeaderCells"] 对象的类型 >>

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。如果我没记错的话,这个类型没有被 Excel 对象模型公开,所以我想知道问题是否是由我将 NamedRangeImpl 对象转换为 NamedRange 控制对象引起的。

非常感谢您的帮助。如果我可以提供任何进一步的信息,请告诉我。提前致谢!

最佳答案

返回的对象类型没有问题。 “Microsoft.Office.Tools.Excel.NamedRangeImp”类型旨在通过“Microsoft.Office.Tools.Excel.NamedRange”接口(interface)访问。

0x800A03EC 的 COM 异常向我表明 excel 将不允许您执行请求的操作(这也是您将收到的错误代码,例如,如果您尝试将字符串值发送到将超过字符限制)。

多次设置 RefersTo 属性没有问题,我通过运行以下代码验证:

        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 必须设置为范围的有效公式。你能仔细检查字符串的格式是否正确吗?

关于c# - 在 C# 中获取 NamedRange 控件的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com