Excel 2007 VSTO plugin exception when setting up Font.Color

I am working on an Excel 2007 VSTO plugin that throws COM exceptions on the client, but not when debugging on my development machine.

What the plugin does is to capture an Excel launch event, define a custom style, and then add an event handler to the SheetChange event. At any time, when the value changes on the sheet, the cell is set to a new style. All this is to give users the opportunity to see the cells that they have changed. The 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;
        }

dev, . , , , VSTO. , , COM-, , , Style.Font.Color.

:

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

:

, [0]:

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

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

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

TriQuint.DemandPlanning.Workbook.ThisWorkbook.ThisWorkbook_Startup ( , EventArgs e)

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

TriQuint.DemandPlanning.Workbook.ThisWorkbook.FinishInitialization()

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

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

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

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

- - ? , .NET, VSTO Interop, Excel 2007 ..

!

+3
2

, , . , . , , ...

, : - (, , ..) , .

, :

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. .

+1

Excel 56- . , ( , 56 , btw). Font.Color Font.ColorIndex . . , , , , . , , , .

0

Source: https://habr.com/ru/post/1738590/


All Articles