BUG: NWFREADME:Word Interoperability Sample Generates An Exception Error Using the Word Find Object (313104)



The information in this article applies to:

  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft .NET Framework SDK 1.0

This article was previously published under Q313104

SYMPTOMS

When you build and run the Word interoperability sample that is provided with the Microsoft .NET Framework Software Development Kit (SDK), you may receive the following error message when you call the Find object (the Console.WriteLine(myFind.Text); code line):
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in [YourAppName.exe]
Additional information: Exception from HRESULT: 0xC0000005.

CAUSE

Word reuses Globally Unique Identifiers (GUIDs) that Microsoft Excel 95 originally used. If the Excel 95 type library is registered after the Word type library, you will experience this problem.

RESOLUTION

To work around this problem, do either of the following:
  • Use late binding for the Find object.NOTE: This is the recommended solution.

    To use late binding for the Find object, change the following code in the Word interoperability sample
    Word.Find myFind = mySelection.Find;
    
    Console.WriteLine(myFind.Text);
    object findText = "alow";
    object replaceText ="allow";
    
    // Find "alow" and replace with "allow"
    try
    {
       myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
    ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);
    
    }
    					
    to:
    object myFind = mySelection.Find;
    
    Console.WriteLine(myFind.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, myFind, null));
    object findText = "alow";
    object replaceText ="allow";
    
    // Find "alow" and replace with "allow"
    try
    {
       object[] Parameters;
       Parameters = new object[15];
       Parameters[0] = findText;
       Parameters[1] = missing;
       Parameters[2] = missing;
       Parameters[3] = missing;
       Parameters[4] = missing;
       Parameters[5] = missing;
       Parameters[6] = missing;
       Parameters[7] = missing;
       Parameters[8] = missing;
       Parameters[9] = replaceText;
       Parameters[10] = missing;
       Parameters[11] = missing;
       Parameters[12] = missing;
       Parameters[13] = missing;
       Parameters[14] = missing;
    
       myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters );
    }
    					
  • If it is not feasible to use late binding to resolve the problem, you can also reregister the Word type library. To do this, follow these steps:
    1. Find the appropriate type library for the version of Word that is installed on the system.
    2. From the command line, use Regtlib.exe with the full path to the type library to reregister the library for COM.
    NOTE: Although this method is initially easier, the application may fail later on, because the problem recurs if the Excel 95 type library is reregistered by another application or a service pack.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

REFERENCES

For additional information about this problem and its resolutions, click the article numbers below to view the articles in the Microsoft Knowledge Base:

292744 BUG: Automation Client Receives Error or Crashes Calling Word's Find Object

302902 HOWTO: Binding for Office Automation Servers with Visual C# .NET


Modification Type:MajorLast Reviewed:12/12/2003
Keywords:kbAutomation kbbug kbreadme KB313104