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