I have a package with a C# Script Task. When I run the package on my PC it runs fine. When I run it from a job on the 2008 R2 SQL Server, I get an error:
System.NullReferenceException: Object reference not set to an instance of an object.
Here is the code from the task:
/* Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using Microsoft.Office.Interop.Word; using System.IO; namespace ST_81404bfc4f6042a690e05f2203277b8c.csproj { [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { #region VSTA generated code enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; #endregion /* 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, null); 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 void Main() { object objNull = System.Reflection.Missing.Value; object objTrue = true; object objFalse = false; object objOpenFormat = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatAuto; object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; string strDocFolder = "\\\\server01\\shared$\\Docs\\"; string strFileName = "Document_4"; object strTemplatePath = strDocFolder + strFileName + ".TEMPLATE.docx"; string strCreatedDate = (string)Dts.Variables["User::decCreatedDate"].Value; string strPdfFolder = "\\\\server01\\shared$\\Docs\\Storage\\" + strCreatedDate + "\\"; object strPdfFile = strPdfFolder + strFileName + ".pdf"; Microsoft.Office.Interop.Word._Application wrdApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document wrdDoc; Microsoft.Office.Interop.Word.MailMerge wrdMailMerge; if (!Directory.Exists(strPdfFolder)) { Directory.CreateDirectory(strPdfFolder); } wrdDoc = wrdApp.Documents.Open(ref strTemplatePath, ref objFalse, ref objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objFalse, ref objNull, ref objNull, ref objNull, ref objNull); wrdApp.Visible = false; wrdDoc.Activate(); wrdMailMerge = wrdDoc.MailMerge; wrdMailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument; wrdMailMerge.Execute(ref objFalse); //wrdDoc.Close(ref objTrue, ref objNull, ref objNull); wrdApp.ActiveDocument.SaveAs(ref strPdfFile, ref wdFormat, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull); wrdApp.Documents.Close(ref doNotSaveChanges, ref objNull, ref objNull); wrdApp.Quit(ref objFalse,ref objNull,ref objNull); if (wrdDoc != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdDoc); if (wrdApp != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdApp); wrdMailMerge = null; wrdDoc = null; wrdApp = null; Dts.TaskResult = (int)ScriptResults.Success; } } }
According to the DBA, Word is installed on the server.
The Microsoft.Office.Interop.Word version is 12.
When I changed it to 14, it through an error that it could not be loaded.