Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Thu, 26 Jun 2008 00:10:01 -0700,    group: microsoft.public.word.oleinterop        back       


Word2007 automation   
I want to do some programmed automation with Office2007, using C#.
For example, a simple search and replace in Word2007. I use this code:

Word2007.Application word = new Word2007.Application();
Word2007.Document doc = new Word2007.Document();

object missing = Type.Missing;

try
{
object fileName = "test.doc";
object varFalse = false;
doc = word.Documents.Open(ref fileName,
ref missing, ref varFalse, 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);

doc.Activate();

foreach (Word2007.Range tmpRange in doc.StoryRanges)
{
if (tmpRange == null) continue;
tmpRange.Find.Text = "old";
tmpRange.Find.Replacement.Text = "new";
tmpRange.Find.Wrap = Word2007.WdFindWrap.wdFindContinue;

replaceAll = Word2007.WdReplace.wdReplaceAll;
tmpRange.Find.Execute(ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing,
ref missing, ref missing);
}

doc.Save();
doc.Close(ref missing, ref missing,
ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
doc.Close(ref missing, ref missing,
ref missing);
word.Application.Quit(ref missing,
ref missing, ref missing);
MessageBox.Show("Error:\n" + ex.Message, "Error message");
}


The code worked fine with Word2003, but with Word2007 the replacement isn't 
done anymore...
I get a runtime error "External component has thrown an exception" on this 
line: tmpRange.Find.Text = "old";

I have added the correct Microsoft.Office.Interop.Word dll to my project 
(version 12), so I don't think that is the problem.

Any thoughts anyone?
date: Thu, 26 Jun 2008 00:10:01 -0700   author:   -kve-

Re: Word2007 automation   
Hi =?Utf-8?B?LWt2ZS0=?=,

> The code worked fine with Word2003, but with Word2007 the replacement isn't 
> done anymore...
> I get a runtime error "External component has thrown an exception" on this 
> line: tmpRange.Find.Text = "old";
>
See if the inforamtion in this KB article is pertinent

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 :-)
date: Thu, 26 Jun 2008 13:56:23 +0200   author:   Cindy M.

Re: Word2007 automation   
Just solved the problem: I had Office 2003 and 2007 installed n my pc. I 
removed 2003, re-installed 2007, and now it works fine...
date: Thu, 26 Jun 2008 05:04:00 -0700   author:   -kve-

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us