我正在开发一个 Excel 2007 VSTO 插件,该插件会在客户端引发 COM 异常,但在我的开发机器上进行调试时不会。
该插件的作用是捕获 Excel 的 Startup 事件,定义专门的样式,然后将事件处理程序添加到 SheetChange 事件。任何时候在工作表中更改值时,单元格都会设置为新样式。所有这一切都是为了向用户提供一种查看他们已更改的单元格的方法。代码如下:
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
this.BeforeSave += new Microsoft.Office.Interop.Excel.WorkbookEvents_BeforeSaveEventHandler(ThisWorkbook_BeforeSave);
this.SheetChange += new Microsoft.Office.Interop.Excel.WorkbookEvents_SheetChangeEventHandler(ThisWorkbook_SheetChange);
cfStyle = Globals.ThisWorkbook.Styles.Add("CFStyle", missing);
cfStyle.Font.Color = Excel.XlRgbColor.rgbOrange;
cfStyle.Font.Bold = true;
cfStyle.Interior.Color = Excel.XlRgbColor.rgbLightGray;
cfStyle.Interior.TintAndShade = 0.8;
cfStyle.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
cfStyle.Borders.Weight = Excel.XlBorderWeight.xlThin;
cfStyle.Borders.Color = Excel.XlRgbColor.rgbDarkSlateGray;
cfStyle.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
cfStyle.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
}
当它在开发中运行时,它运行完美。但是,当它在客户端计算机上运行时,一旦加载 VSTO 插件,我就会获得此异常详细信息。有趣的是,它似乎在第一次 COM 交互时失败,这恰好是设置 Style.Font.Color 属性。
以下是异常详细信息:
System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
Server stack trace:
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Microsoft.Office.Interop.Excel.Font.set_Color(Object )
at TriQuint.DemandPlanning.Workbook.ThisWorkbook.ThisWorkbook_Startup(Object sender, EventArgs e)
at Microsoft.Office.Tools.Excel.Workbook.OnStartup()
at TriQuint.DemandPlanning.Workbook.ThisWorkbook.FinishInitialization()
at Microsoft.VisualStudio.Tools.Office.EntryPointComponentBase.Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint.FinishInitialization()
at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.ExecutePhase(ExecutionPhases executionPhases)
at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.CompleteInitialization()
at Microsoft.VisualStudio.Tools.Office.Internal.OfficeAddInAdapterBase.ExecuteEntryPointsHelper()
有没有人见过这样的事情?我已经做了很多验证,例如确保 .NET、VSTO Interop、Excel 2007 等的正确版本。
提前感谢您的任何建议! 吉姆
最佳答案
为了潜在地使其他人免于浪费许多时间的痛苦,我想我会发布我的解决方案。它是如此的简单到让我重新思考我作为开发者的生活。好吧,不是真的,但仍然......
因此,重申一下所需的功能:目标是在用户编辑单元格时更改单元格的样式(背景、字体、边框等)。
下面是实现该技巧的代码:
void ThisWorkbook_SheetChange(object Sh, Microsoft.Office.Interop.Excel.Range Target)
{
foreach (Excel.Range range in Target.Cells)
{
Excel.Range cellRange = range.Cells[1, 1] as Excel.Range;
cellRange.Borders.ColorIndex = 10;
cellRange.Interior.ColorIndex = 43;
cellRange.Font.Bold = true;
}
}
ThisWorkbook_SheetChange 是 Workbook.SheetChange 事件的事件处理程序。只需设置存在于 Range 对象上的样式属性。不要在 Range.Style 对象上设置样式属性。如果这样做,这将更改 Excel 中的默认样式,并导致使用该样式的所有单元格也发生更改。
我想这样写也行,但我还没有测试过:
void ThisWorkbook_SheetChange(object Sh, Microsoft.Office.Interop.Excel.Range Target)
{
Target.Cells.Borders.ColorIndex = 10;
Target.Cells.Interior.ColorIndex = 43;
Target.Cells.Font.Bold = true;
}
感谢 code4life 关于 ColorIndex 的帖子。您的信息帮了大忙。
关于c# - 设置 Font.Color 时 Excel 2007 VSTO 插件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520513/