使用SSIS(Visual Studio)在Excel文件中执行VBAmacros

我想创build一个SSIS包,使我能够打开Excel文件(.xls),然后使用脚本任务(或更好的方法,如果有人知道),我想打开Excel文件,从我的macros运行PersonalWorkbook.xlsb并保存该excel文件并closures它。

到目前为止,我发现使用的代码是这样的:

`

Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Runtime Imports Microsoft.Office.Interop _ _ 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 ' The execution engine calls this method when the task executes. ' To access the object model, use the Dts property. Connections, variables, events, ' and logging features are available as members of the Dts property as shown in the following examples. ' ' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value ' To post a log entry, call Dts.Log("This is my log text", 999, Nothing) ' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True) ' ' To use the connections collection use something like the following: ' ConnectionManager cm = Dts.Connections.Add("OLEDB") ' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;" ' ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. ' ' To open Help, press F1. Public Sub Main() Dim oExcel As Excel.Application = Nothing Dim oBook As Excel.Workbook = Nothing Dim oBooks As Excel.Workbooks = Nothing Try 'Start Excel and open the workbook. oExcel = CreateObject("Excel.Application") oExcel.Visible = False oBooks = oExcel.Workbooks oBook = oBooks.Open(Dts.Variables("Det_Sign_DestFileName").Value.ToString()) ' Change your variable name here. 'Run the macros. 'oExcel.Run("PERSONAL.XLSB!TriangleDetail") ' Change the name of your Macro here. 'Clean-up: Close the workbook and quit Excel. oBook.Save() oExcel.Quit() Dts.TaskResult = ScriptResults.Success Finally If oBook IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook) If oBooks IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks) If oExcel IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) oBook = Nothing oBooks = Nothing oExcel = Nothing End Try End Sub End Class 

`

当我closuresVSTA时没有错误,但是,当我在Visual Studio中执行ssis包时,我在这里得到这个错误:

“DTS脚本任务遇到用户代码中的exception:

项目名称:ST_bf96275780f942c49789ab24ddab12da

无法加载脚本执行。

(有一个空白框不显示任何错误)“

我可以请有这种帮助,或者可以有人build议我一个完全简单的方法来从我的PersonalWorkbook.xlsb在其他excel文件中使用ssis包中的脚本任务(或其他)运行VBAmacros。

我正在使用Visual Studio 2012,SQL Server 2012和Microsoft Excel 2013。