设置Font.Color当Excel 2007 VSTO插件例外插件、Color、Font、Excel

2023-09-07 04:37:51 作者:生活就要屁颠屁颠滴过

我工作的一个Excel 2007 VSTO插件,抛出COM异常在客户端上却没有关于我的开发机器调试时。

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的Startup事件,定义了一个专门的风格,那么事件处理程序添加到SheetChange子事件。任何时候一个值被在片材改变,该单元被设置为新的风格​​。所有这一切都为用户提供一种方式来看看它们改变了我的细胞。 code是如下:

What the plugin does is capture Excel's Startup event, define a specialized style, then add an event handler to the SheetChange event. 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;
        }

在这个运行在开发,运行完美。然而,当它运行在客户机上,我得到这个异​​常详细信息一旦VSTO插件加载。有趣的部分是它似乎在第一个COM相互作用,这恰好是树立了一个Style.Font.Color属性失败。

When this runs in dev, it runs perfectly. However when it's run on a client machine, I get this exception detail once the VSTO plugin loads. The interesting part is it seems to fail on the first COM interaction, which happens to be setting a Style.Font.Color property.

下面是异常详细信息:

System.Runtime.InteropServices.COMException   (0x800A03EC):从HRESULT异常:   0x800A03EC

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

服务器堆栈跟踪:

例外重新抛出的[0]:

Exception rethrown at [0]:

在   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天   reqMsg,即时聊天retMsg)

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

在   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培;   MSGDATA,的Int32型)

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

在   Microsoft.Office.Interop.Excel.Font.set_Color(对象   )

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

在   TriQuint.DemandPlanning.Workbook.ThisWorkbook.ThisWorkbook_Startup(对象   发件人,EventArgs五)

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

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

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

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

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

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

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

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

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

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

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

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

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

有没有人见过这样的事?我已经做了相当多的验证,如确保.NET,VSTO互操作,Excel 2007中,等等等等。

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.

在此先感谢您的任何建议! 吉姆

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.

下面是code,做的伎俩:

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是Workbook.SheetChange事件的事件处理程序。只需将Range对象上存在的样式属性。不要设置Range.Style对象的样式属性。如果你这样做,这将改变在Excel默认的样式,并导致所有的细胞使用该样式改变为好。

ThisWorkbook_SheetChange is an event handler of the Workbook.SheetChange event. Simply set the style properties that exist on the Range object. DO NOT set the style properties on the Range.Style object. 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.

我想写作这种方式也将工作,但我还没有测试了这一点:

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

由于code4life您有关ColorIndex职位。您的信息帮了不少忙。

Thanks to code4life for your post about ColorIndex. Your info helped quite a bit.