Microsoft KB Archive/313104

= BUG: NWFREADME:Word Interoperability Sample Generates An Exception Error Using the Word Find Object =

Article ID: 313104

Article Last Modified on 10/23/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2

-



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 = &quot;alow&quot;; object replaceText =&quot;allow&quot;;

// Find &quot;alow&quot; and replace with &quot;allow&quot; 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(&quot;Text&quot;, BindingFlags.GetProperty, null, myFind, null)); object findText = &quot;alow&quot;; object replaceText =&quot;allow&quot;;

// Find &quot;alow&quot; and replace with &quot;allow&quot; 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(&quot;Execute&quot;, 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:  Find the appropriate type library for the version of Word that is installed on the system. 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.

<div class="status_section">

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

<div class="references_section">