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 :



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!!!!
 
C

Cindy M.

Hi =?Utf-8?B?WXVu?=,
This codesworks well in word 2003 environment , but when using word 2007, an
Exception has been thrownL:
See if the information in this KB article applies

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

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 

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