Word 2007 invoke Error in C#

Y

Yun

I am doing word automation. I need to search the document for certain text
and replace it with other words, however, it fails. Here is my code and the
text it is looking for:



class WdDocument : Microsoft.Office.Interop.Word.ApplicationClass
{
object oSpath;
object oTpath;
object missing = Missing.Value;
public WdDocument(string spath,string opath)
{
this.oSpath = spath;
this.oTpath = opath;

}

public void replacement(string replace, string value)
{
object rWords = replace;
object vWords = value;
object repType = WdReplace.wdReplaceAll;
Document myDoc = this.Documents.Open(ref this.oSpath, 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);

myDoc.Select();


Selection.Find.Text = replace; <------ Exception Thrown while
Word 2007
Selection.Find.Replacement.ClearFormatting();
Selection.Find.Replacement.Text = value;
Selection.Find.Execute(ref rWords,
ref missing,ref missing,ref missing,
ref missing,ref missing,ref missing,
ref missing,ref missing,ref vWords,
ref repType,ref missing,ref missing,
ref missing,ref missing
); <------ Exception Thrown while Word
2007

myDoc.SaveAs(ref this.oTpath,
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);

myDoc.Close(ref missing, ref missing, ref missing);

this.Quit(ref missing, ref missing, ref missing);
}
}


This codesworks well in word 2003 environment , but when using word 2007, an
Exception has been thrownL:

An unhandled exception of type 'System.Tuntime.InteropServices.COMException'
Additional information: The Find What text contains a Pattern Match
expression which is not valid.

Is threre any difference here between 2003 and 2007 ? Is there any other way
that can implement what i want ? Thanks!!!!
 
D

Dian D. Chapman, MVP

Try posting this dev question to one of the Word VBA groups where more
developers hang out...who may be able to help you better.



Dian D. Chapman
Technical Consultant, Microsoft MVP
MOS Certified Instructor, Editor/TechTrax Ezine
https://mvp.support.microsoft.com/profile/Dian.Chapman

Free PC Tutorials: http://www.mousetrax.com/techtrax
Free Word Tricks eBook: http://www.mousetrax.com/books.html
Optimize your business docs: http://www.mousetrax.com/consulting
Learn VBA the easy way: http://www.mousetrax.com/techcourses.html
 

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