简体   繁体   English

设置Font.Color时出现Excel 2007 VSTO插件异常

[英]Excel 2007 VSTO plugin exception when setting Font.Color

I am working on an Excel 2007 VSTO plugin that is throwing COM exceptions on the client but not when debugging on my development machine. 我正在研究一个Excel 2007 VSTO插件,它在客户端上抛出COM异常,但在我的开发机器上进行调试时却没有。

What the plugin does is capture Excel's Startup event, define a specialized style, then add an event handler to the SheetChange event. 该插件的作用是捕获Excel的Startup事件,定义专用样式,然后向SheetChange事件添加事件处理程序。 Anytime a value is changed in the sheet, the cell is set to the new style. 无论何时在工作表中更改值,单元格都将设置为新样式。 All of this is to provide users a way to see the cells they've changed. 所有这一切都是为了向用户提供一种查看他们已经改变的细胞的方法。 Code is as follows: 代码如下:

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;
        }

When this runs in dev, it runs perfectly. 当它在dev中运行时,它运行完美。 However when it's run on a client machine, I get this exception detail once the VSTO plugin loads. 但是,当它在客户端计算机上运行时,我会在加载VSTO插件后获得此异常详细信息。 The interesting part is it seems to fail on the first COM interaction, which happens to be setting a Style.Font.Color property. 有趣的是它似乎在第一次COM交互时失败,这恰好是设置Style.Font.Color属性。

Here are the exception details: 以下是异常详细信息:

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC

Server stack trace: 服务器堆栈跟踪:

Exception rethrown at [0]: 在[0]处重新抛出异常:

at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)

at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32 type)

at Microsoft.Office.Interop.Excel.Font.set_Color(Object ) 在Microsoft.Office.Interop.Excel.Font.set_Color(Object)

at TriQuint.DemandPlanning.Workbook.ThisWorkbook.ThisWorkbook_Startup(Object sender, EventArgs e) 在TriQuint.DemandPlanning.Workbook.ThisWorkbook.ThisWorkbook_Startup(Object sender,EventArgs e)

at Microsoft.Office.Tools.Excel.Workbook.OnStartup() 在Microsoft.Office.Tools.Excel.Workbook.OnStartup()

at TriQuint.DemandPlanning.Workbook.ThisWorkbook.FinishInitialization() 在TriQuint.DemandPlanning.Workbook.ThisWorkbook.FinishInitialization()

at Microsoft.VisualStudio.Tools.Office.EntryPointComponentBase.Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint.FinishInitialization() 在Microsoft.VisualStudio.Tools.Office.EntryPointComponentBase.Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint.FinishInitialization()

at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.ExecutePhase(ExecutionPhases executionPhases) 在Microsoft.VisualStudio.Tools.Applications.AddInAdapter.ExecutePhase(ExecutionPhases executionPhases)

at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.CompleteInitialization() 在Microsoft.VisualStudio.Tools.Applications.AddInAdapter.CompleteInitialization()

at Microsoft.VisualStudio.Tools.Office.Internal.OfficeAddInAdapterBase.ExecuteEntryPointsHelper() 在Microsoft.VisualStudio.Tools.Office.Internal.OfficeAddInAdapterBase.ExecuteEntryPointsHelper()

Has anyone ever seen anything like this? 有没有人见过这样的东西? I've done quite a few validations, such as ensuring the proper versions of .NET, VSTO Interop, Excel 2007, etc etc. 我已经做了很多验证,比如确保.NET的正确版本,VSTO Interop,Excel 2007等等。

Thanks in advance for any advice! 提前感谢任何建议! Jim 吉姆

To potentially save others from the pain of many lost hours, I thought I would post my solution to this. 为了让其他人免受许多失去时间的痛苦,我想我会发布我的解决方案。 It's so ridiculously simple that it's making me re-think my life as a developer. 这太简单了,它让我重新思考我作为开发人员的生活。 Ok, not really, but still... 好吧,不是真的,但还是......

So to re-state the desired functionality: the goal is to change a cell's style (background, font, borders, etc) whenever a user edits a cell. 因此,要重新声明所需的功能:目标是在用户编辑单元格时更改单元格的样式(背景,字体,边框等)。

Here is the code that does the trick: 这是诀窍的代码:

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 is an event handler of the Workbook.SheetChange event. ThisWorkbook_SheetChange是Workbook.SheetChange事件的事件处理程序。 Simply set the style properties that exist on the Range object. 只需设置Range对象上存在的样式属性即可。 DO NOT set the style properties on the Range.Style object. 不要在Range.Style对象上设置样式属性。 If you do, this will change the default style in Excel, and cause all of your cells that use that style to change as well. 如果这样做,这将更改Excel中的默认样式,并导致使用该样式的所有单元格也发生更改。

I imagine writing it this way will also work, but I have not tested this out: 我想用这种方式写它也会起作用,但我还没有测试过:

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;
            }

Thanks to code4life for your post about ColorIndex. 感谢code4life为您的关于ColorIndex的帖子。 Your info helped quite a bit. 你的信息帮了很多忙。

You get a 56-color palette in Excel to play with. 您可以在Excel中使用56色调色板进行播放。 The color palette will be different from one desktop to another, especially if you aren't using a custom template to base your worksheets from (templates can be distributed from computer to computer, thus allowing the 56 colors to be customized on one machine, stored to the template, and shared btw machines). 调色板从一个桌面到另一个桌面会有所不同,特别是如果您不使用自定义模板来设置工作表(模板可以从计算机分发到计算机,因此允许在一台机器上自定义56种颜色,存储到模板,共享btw机器)。 Instead of setting the Font.Color property, set the Font.ColorIndex to a number within this range. 而不是设置Font.Color属性,将Font.ColorIndex设置为此范围内的数字。 That should eliminate the exception being thrown. 这应该可以消除被抛出的异常。 However, this will probably uncover the next issue, which is that the color palette btw the client machines and your desktop are different. 但是,这可能会揭示下一个问题,即客户端计算机和桌面上的调色板是不同的。 To resolve that, you'll need to override the default color palette to match what is on your desktop. 要解决此问题,您需要覆盖默认调色板以匹配桌面上的调色板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM