Script task fails with Excel range error
Hi everyone, I know there are a lot of similar posts but after digging through many of them, I was unable to find an answer so I apologize if there is a duplicate post buried somewhere. This is the first SSIS package I've ever put together and the first time I've ever used the Interop libraries so there's a decent chance I've just done something wrong. I have a simple SSIS package that I developed in VS2010 that consists of one Execute SQL Task and one Script Task. The SQL runs flawlessly, dumping the data into an existing Excel template worksheet. The script task will run independently of the SSIS package within VS but when I try to run it as a part of the SSIS package, it fails with this error: Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC at Microsoft.Office.Interop.Excel._Worksheet.get_Range(Object Cell1, Object Cell2) at ST_72e220d48c7e46c5ab80687a53ba356e.vbproj.ScriptMain.Main() Here is the code I'm working with: Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Runtime Imports Excel = Microsoft.Office.Interop.Excel <System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _ <System.CLSCompliantAttribute(False)> _ Partial Public Class ScriptMain Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase Enum ScriptResults Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure End Enum Public Sub Main() Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorksheet As Excel.Worksheet Dim rowCount As Integer 'Count of all used rows in the worksheet Dim tempRowCount As New Integer 'Temporary row count for looping through Dim cellContents As Integer 'Variable to store cell contents for conversion to Integers Dim getDate As Date = DateTime.Today 'Insert date into variable Dim month As Integer = getDate.AddMonths(-1).Month() 'Variable to store current month for title Dim year As Integer = getDate.Year() 'Variable to store current year for title Dim day As Integer = getDate.Day() 'Variable to store current day Dim lastDay As Integer = getDate.AddDays(-day).Day() 'Variable to store last day of last month 'Instantiate and call spreadsheet xlApp = New Excel.ApplicationClass xlWorkBook = xlApp.Workbooks.Open("C:\Fully\Qualified\Path\Workbook.xls") xlWorksheet = xlWorkBook.Worksheets("Year") 'Prep work for report formatting xlWorksheet.Cells(3, 2) = "Counts from " & month & "/1/" & year & " thru " & month & "/" & lastDay & "/" & year 'Fill in dates for header xlWorksheet.Range("B5:D" & rowCount).Font.Bold = False 'Remove Bold text from all rows below title caused by SQL dump rowCount = xlWorksheet.UsedRange.Rows.Count() 'Declaring rowCount to equal all rows containing data tempRowCount = 5 'Set tempRowCount to first row we want to modify (below heading) Do While tempRowCount <= rowCount + 1 'Since the rowCount only includes rows with data, the first row (which is blank) isn't counted so I added 1 to make up for it xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlLineStyleNone xlWorksheet.Range("B" & tempRowCount & ":D" & tempRowCount).Borders(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlLineStyleNone cellContents = xlWorksheet.Range("B" & tempRowCount).Value 'Convert branch numbers to Integer xlWorksheet.Range("B" & tempRowCount).Value = CInt(cellContents) cellContents = xlWorksheet.Range("D" & tempRowCount).Value 'Convert suborder count to Integer xlWorksheet.Range("D" & tempRowCount).Value = CInt(cellContents) tempRowCount = tempRowCount + 1 'If tempRowCount = 123 Then 'Test to make sure the application isn't running past the desired rows 'MsgBox("End of report") 'End If Loop 'Calculate percentages of branch SubOrder volume tempRowCount = rowCount + 2 'Set tempRowCount to the row below the end of the results xlWorksheet.Cells(5, 5) = "=D5/$D$" & tempRowCount 'Calculate percentage of first branch row xlWorksheet.Range("E5").AutoFill(xlWorksheet.Range("E5:E" & rowCount + 1)) 'Auto-fill the percentages column xlWorksheet.Range("E5:E" & tempRowCount).NumberFormat = "0.00%" 'Format the percentages column to actually be percentages 'Calculate totals for and format the "Totals" row at the end of the report: xlWorksheet.Cells(tempRowCount, 3) = "TOTALS:" 'Beginning of "Totals" row xlWorksheet.Cells(tempRowCount, 4) = "=SUM(D5:$D$" & rowCount + 1 & ")" 'Get SubOrder total xlWorksheet.Cells(tempRowCount, 5) = "=SUM(E5:$E$" & rowCount + 1 & ")" 'Get total percentage (should always add up to %100) xlWorksheet.Range("B" & tempRowCount & ":E" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous 'Add top border to Totals row xlWorksheet.Range("B" & tempRowCount & ":E" & tempRowCount).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble 'Add bottom border to Totals row xlWorksheet.Range("A" & tempRowCount & ":E" & tempRowCount).Font.Bold = True 'Bold Totals row xlWorksheet.Range("C" & tempRowCount).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter 'Align "Total" label xlWorksheet.Range("D" & tempRowCount).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft 'Align final SubOrder total 'xlWorksheet.Range("A1").Activate() xlApp.Goto(Reference:=xlWorksheet.Range("A1"), Scroll:=True) 'MsgBox("Done!") 'Message box to confirm everything completed successfully 'Cleanup xlWorkBook.Save() xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorksheet) End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try Dts.TaskResult = ScriptResults.Success End Sub End Class
September 21st, 2012 4:24pm

I do not see the Get_range called in your code, which makes me think it is a canned office interop dll/call, thus a hotfix mentioned in http://www.pcreview.co.uk/forums/adam-user-setoption-setpassword-having-exception-hresult-0x80005008-t1361422.html may be relevant.Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
September 21st, 2012 8:15pm

Thanks for the like ArthurZ but what I ended up doing (since there isn't a link to the hotfix in that post) was just deleting and re-creating the script task in VS/BIDS. I used the exact same code so I'm not sure where the issue was exactly but re-creating the task itself seemed to fix the issue.
September 27th, 2012 3:09pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics