johns00
asked on
Using Microsoft Word 11.0 Object Library In Deployments
I created a simple windows application using C# that will open Word documents and parse certain text out of them and store in a database. It works perfectly on my machine. I have Microsoft Word 2003 set up on my pc. When I create a deployment package and run it on another box that has Word 2002 I get "An error occured: Object reference not set to an instance of an
object." I'm not sure if it has to do with the Word versions or the object library I'm using or something else entirely.
object." I'm not sure if it has to do with the Word versions or the object library I'm using or something else entirely.
ASKER
I just tried to run it on a machine that has Word 2003 and I got the same error.
ASKER
Here is the stack trace:
Microsoft.Office.Interop.W ord.Docume nts.Open(O bject& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog, Object& XMLTransform)
Microsoft.Office.Interop.W
How are you deploying the app? Through an MSI or xcopy?
"When I create a deployment package and run it on" should have told me that you were using an msi.
ASKER
Both just in case one method might've been the problem. Below is the code that contains the documents.open method throwing up the error:
private void btnParse_Click(object sender, System.EventArgs e)
{
try
{
if(Directory.Exists(lblPat h.Text)) // Verify the chosen directory exists.
{
// Excellent reference for the Word object model http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/wordobject.asp
Microsoft.Office.Interop.W ord.Applic ation thisApplication = new Microsoft.Office.Interop.W ord.Applic ation();
Microsoft.Office.Interop.W ord.Docume nt thisDocument;
// C# doesn't allow optional parameters so these are declared for the open method.
Object filename = @"C:\JustParse.doc";
Object confirmConversions = Type.Missing;
Object readOnly = Type.Missing;
Object addToRecentFiles = Type.Missing;
Object passwordDocument = Type.Missing;
Object passwordTemplate = Type.Missing;
Object revert = Type.Missing;
Object writePasswordDocument = Type.Missing;
Object writePasswordTemplate = Type.Missing;
Object format = Type.Missing;
Object encoding = Type.Missing;
Object visible = Type.Missing;
Object openConflictDocument = Type.Missing;
Object openAndRepair = Type.Missing;
Object documentDirection = Type.Missing;
Object noEncodingDialog = Type.Missing;
Object XMLTransform = Type.Missing;
// C# doesn't allow optional parameters so these are declared for the close and quit methods.
Object saveChanges = Microsoft.Office.Interop.W ord.WdSave Options.wd DoNotSaveC hanges;
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
string strProjectCode;
// Create db connection to xxx(production).
string strSQL;
SqlConnection conMain = new SqlConnection ("User ID=xxx;Password=xxx;Initia l Catalog=globalfinance;Data Source=xxx;Connect Timeout=720;Network Library=dbmssocn");
SqlCommand cmdMain;
conMain.Open();
DirectoryInfo di = new DirectoryInfo(lblPath.Text ); // Create a reference to the current directory.
FileInfo[] fi = di.GetFiles(); // Create an array representing the files in the current directory.
foreach(FileInfo fiTemp in fi) // Iterate through the files in the selected folder.
{
if(fiTemp.Extension == ".doc") // Separate those with .doc extensions.
{
filename = fiTemp.FullName;
strProjectCode = fiTemp.Name.ToString();
strProjectCode = strProjectCode.Remove(strP rojectCode .Length - 4, 4);
// Open the document.
thisApplication.Documents. Open(ref filename,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible,
ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref XMLTransform);
thisDocument = (Microsoft.Office.Interop. Word.Docum ent) thisApplication.ActiveDocu ment; // Reference the active document.
//Reference the first table.
Microsoft.Office.Interop.W ord.Table thisTable = thisDocument.Tables[1];
Microsoft.Office.Interop.W ord.Range thisRange = thisTable.Cell(1, 1).Range;
//Add the justification text and project code (inferred from the file name) to the database table.
strSQL = @"INSERT INTO ARSC11574751 (ProjectCode, Justification) VALUES ('" + strProjectCode + @"', '" + thisRange.Text + @"')";
cmdMain = new SqlCommand(strSQL, conMain);
cmdMain.ExecuteNonQuery();
thisApplication.Documents. Close(ref saveChanges, ref originalFormat, ref routeDocument); //Close all documents.
}
}
thisApplication.Quit(ref saveChanges, ref originalFormat, ref routeDocument); //Close the application.
conMain.Close();
MessageBox.Show("All files parsed.");
this.Close(); // Close application.
}
else {MessageBox.Show("The chosen directory doesn't exist.");}
}
catch(Exception ex)
{
MessageBox.Show(@"An error occurred:
Message: " + ex.Message + @"
Source: " + ex.Source + @"
Stack Trace: " + ex.StackTrace);
}
}
private void btnParse_Click(object sender, System.EventArgs e)
{
try
{
if(Directory.Exists(lblPat
{
// Excellent reference for the Word object model http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/wordobject.asp
Microsoft.Office.Interop.W
Microsoft.Office.Interop.W
// C# doesn't allow optional parameters so these are declared for the open method.
Object filename = @"C:\JustParse.doc";
Object confirmConversions = Type.Missing;
Object readOnly = Type.Missing;
Object addToRecentFiles = Type.Missing;
Object passwordDocument = Type.Missing;
Object passwordTemplate = Type.Missing;
Object revert = Type.Missing;
Object writePasswordDocument = Type.Missing;
Object writePasswordTemplate = Type.Missing;
Object format = Type.Missing;
Object encoding = Type.Missing;
Object visible = Type.Missing;
Object openConflictDocument = Type.Missing;
Object openAndRepair = Type.Missing;
Object documentDirection = Type.Missing;
Object noEncodingDialog = Type.Missing;
Object XMLTransform = Type.Missing;
// C# doesn't allow optional parameters so these are declared for the close and quit methods.
Object saveChanges = Microsoft.Office.Interop.W
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
string strProjectCode;
// Create db connection to xxx(production).
string strSQL;
SqlConnection conMain = new SqlConnection ("User ID=xxx;Password=xxx;Initia
SqlCommand cmdMain;
conMain.Open();
DirectoryInfo di = new DirectoryInfo(lblPath.Text
FileInfo[] fi = di.GetFiles(); // Create an array representing the files in the current directory.
foreach(FileInfo fiTemp in fi) // Iterate through the files in the selected folder.
{
if(fiTemp.Extension == ".doc") // Separate those with .doc extensions.
{
filename = fiTemp.FullName;
strProjectCode = fiTemp.Name.ToString();
strProjectCode = strProjectCode.Remove(strP
// Open the document.
thisApplication.Documents.
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible,
ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref XMLTransform);
thisDocument = (Microsoft.Office.Interop.
//Reference the first table.
Microsoft.Office.Interop.W
Microsoft.Office.Interop.W
//Add the justification text and project code (inferred from the file name) to the database table.
strSQL = @"INSERT INTO ARSC11574751 (ProjectCode, Justification) VALUES ('" + strProjectCode + @"', '" + thisRange.Text + @"')";
cmdMain = new SqlCommand(strSQL, conMain);
cmdMain.ExecuteNonQuery();
thisApplication.Documents.
}
}
thisApplication.Quit(ref saveChanges, ref originalFormat, ref routeDocument); //Close the application.
conMain.Close();
MessageBox.Show("All files parsed.");
this.Close(); // Close application.
}
else {MessageBox.Show("The chosen directory doesn't exist.");}
}
catch(Exception ex)
{
MessageBox.Show(@"An error occurred:
Message: " + ex.Message + @"
Source: " + ex.Source + @"
Stack Trace: " + ex.StackTrace);
}
}
could it be that the install is not shipping all needed dll´s...
are you creating the msi by adding project output -> primary output?
and does the resulting msi package install all of the needed interop dll´s?
are you creating the msi by adding project output -> primary output?
and does the resulting msi package install all of the needed interop dll´s?
ASKER
Definitely not that. It chokes on that method, not on the creation of the Word Application object which is what would happen if the dependency was missing. I have noticed that in my code it requires 16 arguments for Documents.Open() but for the previous Word object library it would require 15. That suggests there might be some sort of compatibility issue but then how to explain the same error on a user's computer with Word 2003?
ASKER
Actually scratch that. The user I thought had 2003 reverted to 2002. So it's some sort of backwards compatibility issue.
sounds that way... if you don´t need any specific 2003 functionality couldn´t you try to create the project with say a word 2000 reference and see if that would work with 2003 and 2002
ASKER
I can't because I only have the object library for the version of Office installed on my machine. Shouldn't there be a way to make it backwards compatible using the 2003 library?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Those 2 links had what I needed, thanks a lot.
glad to help. good luck.
Note : the links are dead
ASKER