SQL Server Agent Error - Exception has been thrown by the target of an invocation

0

I am doing a script task in SSIS and scheduling the task using SSIS Agent.

I am able to execute the Script task successfully but I am getting an error when I am trying to run it through SQL Server Agent -

Error

Error: 2020-11-13 12:31:29.64 Code: 0x00000001 Source: Script Task Description: Exception has been thrown by the target of an invocation. End Error

The SSIS script task query is as below -

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.CSharp;
#endregion

namespace ST_24e25f722fa240eba22f16d2b24cc864
{
    
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        
        public void Main()
        {
           
            string filePath = Dts.Variables["varExcelFiles"].Value.ToString();
            //Define Excel File
            #region
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

            Workbook excelWorkbook = excel.Workbooks.Open(filePath);

            _Worksheet excelWorksheet = excelWorkbook.Sheets[1];
            #endregion

            //Modify Excel Files Delete Rows, Unmerge
            #region
            Range range = excelWorksheet.get_Range("A1:A7");
            Range entireRow = range.EntireRow;
            entireRow.Delete(XlDeleteShiftDirection.xlShiftUp);

            Range range2 = excelWorksheet.get_Range("A2");
            Range entireRow2 = range2.EntireRow;
            entireRow2.Delete(XlDeleteShiftDirection.xlShiftUp);

            excelWorksheet.Range["A:Z"].UnMerge();

            Range range3 = excelWorksheet.get_Range("S1");
            Range entireColumn = range3.EntireColumn;
            entireColumn.Delete(XlDeleteShiftDirection.xlShiftToLeft);

            Range range4 = excelWorksheet.get_Range("T1:U1");
            Range entireColumn2 = range4.EntireColumn;
            entireColumn2.Delete(XlDeleteShiftDirection.xlShiftToLeft);

            Range range5 = excelWorksheet.get_Range("V1:Z1");
            Range entireColumn3 = range5.EntireColumn;
            entireColumn3.Delete(XlDeleteShiftDirection.xlShiftToLeft);

            int rowCnt2 = 0;

            rowCnt2 = excelWorksheet.Cells.Find("*", System.Reflection.Missing.Value,
                       System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                       XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious,
                       false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;



            Range range6 = excelWorksheet.get_Range("A" + rowCnt2);
            Range entireRow3 = range6.EntireRow;
            entireRow3.Delete(XlDeleteShiftDirection.xlShiftUp);

            #endregion

            //Save and Close Excle File
            #region
            excel.DisplayAlerts = false;

            excelWorkbook.SaveAs(@"C:\Packages\SourceFile\CGReport\srcChangeGearMatheson.xlsx");
            
            excel.DisplayAlerts = true;
            excelWorkbook.Close();
            excel.Quit();
            #endregion

            Dts.TaskResult = (int)ScriptResults.Success;
        }
        
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };

    }
}
ssis
sql-server-agent
script-task
asked on Stack Overflow Nov 13, 2020 by ashaikh-its

1 Answer

0

The problems is happen to the authority access.

answered on Stack Overflow Jan 28, 2021 by Eng Soon Cheah

User contributions licensed under CC BY-SA 3.0