Solved

Using Microsoft Word 11.0 Object Library In Deployments

Posted on 2004-09-16
15
2,404 Views
Last Modified: 2010-03-19
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.
0
Comment
Question by:johns00
  • 8
  • 6
15 Comments
 

Author Comment

by:johns00
ID: 12078172
I should clarify my original post by noting that the error occurs when I run the app after having deployed it on another machine, not during deployment.
0
 

Author Comment

by:johns00
ID: 12078338
I just tried to run it on a machine that has Word 2003 and I got the same error.
0
 

Author Comment

by:johns00
ID: 12078460
Here is the stack trace:

Microsoft.Office.Interop.Word.Documents.Open(Object& 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)
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12078718
How are you deploying the app? Through an MSI or xcopy?
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12078745
"When I create a deployment package and run it on" should have told me that you were using an msi.
0
 

Author Comment

by:johns00
ID: 12078751
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(lblPath.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.Word.Application thisApplication = new Microsoft.Office.Interop.Word.Application();
                              Microsoft.Office.Interop.Word.Document 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.Word.WdSaveOptions.wdDoNotSaveChanges;
                              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;Initial 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(strProjectCode.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.Document) thisApplication.ActiveDocument;  // Reference the active document.
                                          
                                          //Reference the first table.
                                          Microsoft.Office.Interop.Word.Table thisTable = thisDocument.Tables[1];
                                          Microsoft.Office.Interop.Word.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);
                  }
            }
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12078944
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:johns00
ID: 12079020
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?
0
 

Author Comment

by:johns00
ID: 12079038
Actually scratch that.  The user I thought had 2003 reverted to 2002.  So it's some sort of backwards compatibility issue.
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12079067
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
0
 

Author Comment

by:johns00
ID: 12079105
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?
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 150 total points
ID: 12079115
0
 

Author Comment

by:johns00
ID: 12079331
Those 2 links had what I needed, thanks a lot.
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12079390
glad to help. good luck.
0
 
LVL 23

Expert Comment

by:apresto
ID: 27902959
Note : the links are dead
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
C# TextBox 11 30
C# replace string in void with dR["myData"].ToString() 3 42
What .NET URL re-routing tool did I use? 2 38
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now