Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用SSIS运行Excel宏_Vb.net_Excel_Vba_Ssis - Fatal编程技术网

Vb.net 使用SSIS运行Excel宏

Vb.net 使用SSIS运行Excel宏,vb.net,excel,vba,ssis,Vb.net,Excel,Vba,Ssis,我有一个SSIS包,它运行一组SSRS报告,并将它们保存在一个文件夹中,作为.xlsx文件。我还有一个Excel文件,其中包含一个宏,该宏遍历保存的每个.xlsx文件,将其合并到一个文件中,格式化并保存到另一个文件夹中 现在这是一个两步的过程,所以我的目标是让它只需要一步。为此,我尝试在SSIS包的末尾添加一个脚本任务,用宏打开Excel文件并运行它 我在网络上搜寻解决方案,发现许多似乎适合人们,但不适合我。具体来说,我使用的代码来自 现在,当我填充代码时,将我对Microsoft Excel

我有一个SSIS包,它运行一组SSRS报告,并将它们保存在一个文件夹中,作为
.xlsx
文件。我还有一个Excel文件,其中包含一个宏,该宏遍历保存的每个
.xlsx
文件,将其合并到一个文件中,格式化并保存到另一个文件夹中

现在这是一个两步的过程,所以我的目标是让它只需要一步。为此,我尝试在SSIS包的末尾添加一个脚本任务,用宏打开Excel文件并运行它

我在网络上搜寻解决方案,发现许多似乎适合人们,但不适合我。具体来说,我使用的代码来自

现在,当我填充代码时,将我对Microsoft Excel 15.0对象库的引用添加到
中,并将
导入Microsoft.Office.Interop
添加到脚本顶部,我发现一些代码有错误。请参见下面的屏幕截图:

我得到的错误是:

当类的程序集使用非PIA模式链接时,不允许引用类“ApplicationClass”。

我发现有人似乎有类似的问题,但这对我的问题没有帮助

剧本里有什么地方我做错了吗?请参见下面的脚本本身

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.

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.ApplicationClass = Nothing
        Dim oBook As Excel.WorkbookClass = 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("StrFilePath").Value.ToString()) ' Change your variable name here.


            'Run the macros.
            oExcel.Run("Format") ' 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

非常感谢您的任何帮助/建议

错误消息几乎说明了这一切。。。使用应用程序,而不是ApplicationClass。或者关闭属性中的嵌入互操作类型以引用Excel库。谢谢!这样就消除了错误!然而,现在我明白了:无法加载脚本执行。我发现了一个问题,其他人在哪里有这个问题,他们只是通过重新创建脚本任务来修复它,但我尝试了一下,但没有成功。对这个有什么想法吗?如果有必要,我可以提出一个新问题不知道,我的专业领域是办公室方面的事情。。。最好打开一个新问题:-)错误消息几乎说明了一切。。。使用应用程序,而不是ApplicationClass。或者关闭属性中的嵌入互操作类型以引用Excel库。谢谢!这样就消除了错误!然而,现在我明白了:无法加载脚本执行。我发现了一个问题,其他人在哪里有这个问题,他们只是通过重新创建脚本任务来修复它,但我尝试了一下,但没有成功。对这个有什么想法吗?如果有必要,我可以提出一个新问题不知道,我的专业领域是办公室方面的事情。。。最好打开一个新问题:-)