C# Find.Execute problems in Word

T

tyla

I'm having trouble done a simple "Find" against a Word document. Seems
it should be simple from all the various UseNet messages and MSDN
postings, but it's failing here for reasons that must be so obvious I'm
missing them entirely.
Let's assume a Word 2003 document with two simple sentences in it, the
first one containing the word "jump". Here's my boiled down code:

// =============

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Text;

namespace whatever
{
class Program
{
static void Main(string[] args)
{

Word.Application wordApp = new Word.Application();
wordApp.Visible = true;

try
{
object missing = Type.Missing;
object sFileName = @"C:\Temp\jump.doc";
Word.Document wordDoc = wordApp.Documents.Open(ref
sFileName,
ref missing, ref missing, ref
missing, ref missing, ref missing,
ref missing, ref missing, ref
missing, ref missing, ref missing,
ref missing, ref missing, ref
missing, ref missing, ref missing);

Word.Range rng = wordDoc.Sentences[1];
Console.WriteLine(rng.Sentences.Count.ToString());

object s = "jump";

if (rng.Find.Execute(ref s, ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing) )
{
MessageBox.Show("Yup!");
}
else
{
MessageBox.Show("Nope!");
}

}
catch (Exception ex)
{
Console.WriteLine("+++++++++++++++");
Console.WriteLine(ex.ToString());
Console.WriteLine("+++++++++++++++");
MessageBox.Show("No way, Jose");
}
finally
{
MessageBox.Show("Done!");
}
}
}
}


==============
It consistently throws an Exception on the "Find.Execute" line for
reasons unknown. The ex.ToString() reads:
"System.Runtime.InteropServices.COMException (0x800706F7): The stub
received bad data. (Exception from HRESULT: 0x800706F7)
at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, Object&
MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object&
MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object&
Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object&
MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object&
MatchControl)
at whatever.Program.Main(String[] args) in
C:\UDApps\ConsoleApplication5\ConsoleApplication5\Program.cs:line xx"

which, somehow, isn't terribly informative to this hacker. Any help
here would be very much appreciated.

= Tyla =
 
C

Cindy M -WordMVP-

I'm having trouble done a simple "Find" against a Word document. Seems
it should be simple from all the various UseNet messages and MSDN
postings, but it's failing here for reasons that must be so obvious I'm
missing them entirely.
There's been a couple of recent postings mentioning a KB article that
describes how Word's Find/Replace and an old Excel95 library have the
same GUID. sounds as if this is what you could be running into. The only
workaround is to use late-binding to automate Word (or at least this
part).

http://support.microsoft.com/default.aspx?scid=kb;en-us;313104

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

tyla

Cindy,

That was just the ticket! I would have been about a million years
before I guessed that MS re-used GUIDs (!) Thanks so much for your
timely help!

= Tyla =
 
E

Evan Stone

There's been a couple of recent postings mentioning a KB article that
describes how Word's Find/Replace and an old Excel95 library have the
same GUID. sounds as if this is what you could be running into. The only
workaround is to use late-binding to automate Word (or at least this
part).

I will second this... late binding is *definitely* the way to go when using
Find.Execute.

evan k. stone | software engineer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top