Question : Error attempting to set conditional formatting in Excel via VB.NET

I am attempting to set conditional formatting in Excel using VB.NET and get an error.  It looks like I am not properly identifying the worksheet.  Please check out the error details below and code below that.  Any and all assistance is appreciated!

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146827284
  Message="Exception from HRESULT: 0x800A03EC"
  Source="Microsoft.Office.Interop.Excel"
  StackTrace:
       at Microsoft.Office.Interop.Excel._Worksheet.get_Range(Object Cell1, Object Cell2)
       at Invoices.BSRReader.ExportMOToExcel(Int32 iPYMonth) in C:\Documents and Settings\jason_forlines\My Documents\Visual Studio 2008\Projects\Invoices\Invoices\Objects\BSRReader.vb:line 391
       at Invoices.formBSR.linkExportMO_LinkClicked(Object sender, LinkLabelLinkClickedEventArgs e) in C:\Documents and Settings\jason_forlines\My Documents\Visual Studio 2008\Projects\Invoices\Invoices\Forms\formBSR.vb:line 69
       at System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs e)
       at System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Label.WndProc(Message& m)
       at System.Windows.Forms.LinkLabel.WndProc(Message& msg)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Invoices.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Dim oExcel As New Excel.Application
        Dim oWorkbook As Excel.Workbook = oExcel.Workbooks.Add
        Dim oWorksheet As Excel.Worksheet = CType(oWorkbook.Worksheets(1),  _
            Excel.Worksheet)
        Dim lExcelRow As Long
        Dim sRangeText As String
        Dim oRange As Excel.Range
        Dim oFormatCond1 As Excel.FormatCondition
        Dim oFormatCond2 As Excel.FormatCondition
        Dim oFormatCond3 As Excel.FormatCondition
...
            oRange = CType(.Range("P2:P" & lExcelRow), Excel.Range)
            oRange.Select()
            oRange.FormatConditions.Delete()
 
'*********Code bombs on next line***************
            oFormatCond1 = CType(oWorksheet.Range(oRange).FormatConditions.Add( _
                Type:=Excel.XlFormatConditionType.xlCellValue, _
                Operator:=Excel.XlFormatConditionOperator.xlBetween, Formula1:="0.49", Formula2:="0.66"), Excel.FormatCondition)
            CType(oFormatCond1.Interior, Excel.Interior).ColorIndex = 50
 
            oFormatCond2 = CType(oRange.FormatConditions.Add( _
                Type:=Excel.XlFormatConditionType.xlCellValue, _
                Operator:=Excel.XlFormatConditionOperator.xlLess, _
                Formula1:="0.49"), Excel.FormatCondition)
            CType(oFormatCond2.Interior, Excel.Interior).ColorIndex = 15
 
            oFormatCond3 = CType(oRange.FormatConditions.Add( _
                Type:=Excel.XlFormatConditionType.xlCellValue, _
                Operator:=Excel.XlFormatConditionOperator.xlGreater, _
                Formula1:="1"), Excel.FormatCondition)
            CType(oFormatCond2.Interior, Excel.Interior).ColorIndex = 15
...

Answer : Error attempting to set conditional formatting in Excel via VB.NET

If you are programming against 2003, you want the Interop 11 library not 12 (which is for 2007). You should only need the Interop library I believe.
Random Solutions  
 
programming4us programming4us