Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Word 2007 invoke Error in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yun - 25 Apr 2007 03:34 GMT
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!!!!
Cindy M. - 26 Apr 2007 14:45 GMT
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 :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.