开发者

Very slow interop with Word in c# application

开发者 https://www.devze.com 2023-01-27 23:14 出处:网络
I use VS2005 and I need to create many .doc files. My computer(Intel c2d6600 with 2gb RAM) can convert with 10 files/minute it is very slow for me.

I use VS2005 and I need to create many .doc files. My computer(Intel c2d6600 with 2gb RAM) can convert with 10 files/minute it is very slow for me. What should I do to improve perfomance?

My code:


oWord = new Word.Application();
oMissing = System.Reflection.Missing.Value;
oDoc = this._oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

...
//Do something
...

//save rtf
object fileName = this.FileRtf;
object fileFormat = Word.WdSaveFormat.wdFormatRTF;
object savechanges = false;
oDoc.SaveAs(ref fileNa开发者_开发技巧me, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref savechanges, ref oMissing, ref oMissing);


My psychic debugging powers tell me that you need to optimize the below code:

...
//Do something
...


Try to reduce number of calls to

oWord = new Word.Application(); 
//and 
oWord.Quit(ref savechanges, ref oMissing, ref oMissing); 

In other words call them once and use one instance of Word for multiple files.


First, you should profile your application to find out, where the time is spent.

For is list of profilers, see this SO question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号