如何在SSIS中运行Excel宏时修复System.NullReferenceException错误?

如何在SSIS中运行Excel宏时修复System.NullReferenceException错误?,ssis,Ssis,我在SSIS 2008上设置了一个包,其中包含一个运行excel宏的vb脚本。它在Business Intelligence Development Studio(BIDS)2008上运行,但仍然出现以下错误: Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceExcept

我在SSIS 2008上设置了一个包,其中包含一个运行excel宏的vb脚本。它在Business Intelligence Development Studio(BIDS)2008上运行,但仍然出现以下错误:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
at ST_e916156b0e6449b58e21905bd635ecf0.vbproj.ScriptMain.Main() 
--- End of inner exception stack trace --- 
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) 
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture) 
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() 
Executed as user: 'myusername'. 
Code: 0xFFFFFFFF 
Source: Run macro script excel macro Description: Microsoft Excel cannot access the file 'book1.XLS'. 
There are several possible reasons: 
? The file name or path does not exist. 
? The file is being used by another program. 
? The workbook you are trying to save has the same name as a currently open workbook. 
End Error 
DTExec: The package execution returned DTSER_SUCCESS (0). 
Started: 11:18:15 Finished: 11:18:19 
Elapsed: 3.775 seconds. 
The package executed successfully. The step succeeded. 
另外,当我使用SQL agent运行包时,作业成功,但宏未执行,并且出现以下错误:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
at ST_e916156b0e6449b58e21905bd635ecf0.vbproj.ScriptMain.Main() 
--- End of inner exception stack trace --- 
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) 
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture) 
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() 
Executed as user: 'myusername'. 
Code: 0xFFFFFFFF 
Source: Run macro script excel macro Description: Microsoft Excel cannot access the file 'book1.XLS'. 
There are several possible reasons: 
? The file name or path does not exist. 
? The file is being used by another program. 
? The workbook you are trying to save has the same name as a currently open workbook. 
End Error 
DTExec: The package execution returned DTSER_SUCCESS (0). 
Started: 11:18:15 Finished: 11:18:19 
Elapsed: 3.775 seconds. 
The package executed successfully. The step succeeded. 
但是,我可以完全访问本地服务器和网络,并且尝试在32位和64位上运行,但没有成功

如何解决此问题?

您可以尝试的事项: 根据我个人的经验,Excel在使用SSI时总是很棘手的。从错误消息中可以看到,请验证是否可以排除与问题相关的以下信息

  • 检查运行SQL Server代理服务的用户帐户。您可以通过导航到
    Windows开始
    \
    控制面板
    \
    管理工具
    \
    服务
    来完成此操作。查找名为SQL Server Agent()的服务

  • 确保该帐户可以访问存储Excel文件的文件夹

  • 确认您没有打开Excel文件

  • 在SQL Server代理作业上,单击执行包的步骤。如果您正在SQL Server Integration Services包的类型下运行该包,请确保选中了“执行选项”选项卡上的“用户32位运行时”框

  • 也可以手动双击包文件(.dtsx)。这将带来执行包的DTEXEC实用程序。如果您使用的是配置文件,请在“配置”选项卡上附加配置文件。运行包。如果它在凭据下的DTEXEC实用程序中运行,而不是在SQLServer代理下运行,我通常会发现这与权限问题有关


这看起来像是脚本任务中的异常。看看你的任务代码。查看脚本任务时,请参阅以获取想法。我已经查看了脚本,但看不到脚本中的异常来自何处..Public Sub Main()Dim宏\u名称作为字符串Dim文件\u名称作为字符串Dim文件\u名称2作为字符串Dim ExcelObject作为新的Microsoft.Office.Interop.Excel.Application ExcelObject.Visible=True ExcelObject.DisplayAlerts=False ExcelObject.UserControl=False尝试Dim OBOK作为Microsoft.Office.Interop.Excel.Workbook的wb Dim oBooks为Microsoft.Office.Interop.Excel.Workbooks Dim pbCancel为Boolean=False宏\u name=“Macro001”文件\u name=“c\book1.xls”文件\u name=“c\book2.xls”ExcelObject=CType(CreateObject(“Excel.Application”),Microsoft.Office.Interop.Excel.Application)ExcelObject.Visible=True ExcelObject.UserControl=False ExcelObject.DisplayAlerts=False oBooks=ExcelObject.Workbooks oBook=CType(oBooks.Open(文件名2),Microsoft.Office.Interop.Excel.WorkbookClass)oBook=CType(oBooks.Open(文件名)、Microsoft.Office.Interop.Excel.WorkbookClass)ExcelObject.Run(宏名)ExcelObject.DisplayAlerts=TrueExcelObject=Nothing Dim FireReach As Boolean=True Catch ex As Exception Dts.Events.FireError(-1,“脚本Excel宏”,ex.Message,String.Empty,0)Dts.TaskResult=ScriptResults.Failure End Try ExcelObject.Application.Quit()Dts.TaskResult=ScriptResults.Success End Sub-End ClassIt也通过DTEXEC实用程序工作,并得到相同的错误。如何检查SQL代理权限?