Word Interop: Quickly Inserting HTML Files into Word Doc

A

ajk

[This is a second post. My original did not include a reply address and I
kinda need replies! I apologize for the breach in ettiquette].

Hi, All:

I know how to insert files into a Word doc using C#. However, the program
I've written to do this runs much too slowly. The
"myObj".Application.Selection.InsertFile method executes at a snails pace.
Here are the detais:

I wrote a C# program that creates a new Word doc and then loops through a
list of HTML files to insert them into the new doc (sample code below). The
purpose of the program is to make one Word doc out of all of these Html
files (for archival purposes).

As each file is inserted, the instance of Word takes a long time to complete
the insert (I've waited up to 5 minutes for some pages to finish inserting).

Can someone explain why the insert takes so long? At first I thought it was
graphic sizes (some HTML files have .jpg or .gif files in them as big as
58kb). But Word even inserts the HTML files that don't contain graphics
fairly slowly. If someone knows that I am doing wrong or knows of a more
efficient way to accomplish this task, I'd appreciate hearing about it.
Thanks in advance for any help you all provide.

Sample code follows:

//set stub objects (left out for brevity)

//make word app and optimize
Word.ApplicationClass WordApp = new Word.ApplicationClass();
WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
WordApp.Visible = false;

//make word doc and optimize
wrdDoc = WordApp.Documents.Add(ref template, ref newTemplate, ref docType,
ref isVisible);
wrdDoc.Activate();
WordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdNormalView;

//add sFile to word doc
try
{
//insert file into wrdDoc
wrdDoc.Application.Selection.InsertFile(sFile, ref docrange, ref
conversions, ref links, ref attaches);

//insert pagebreak
wrdDoc.Application.Selection.InsertBreak(ref breaktype);
}
catch (System.Exception e)
{
//error trapping (not included for brevity)
}
 
T

Trebek

Although speed isn't great in proceeding example either, I believe you will
see some performance gains by working directly with the *Range* object when
inserting a file.

For example, if you wanted to insert a file at the end(or beginning if there
is no current content in the Word doc) of a Word doc, you can use the
following (note -- not complete! from memory):

object falseRef = false;

object trueRef = true;

object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

object brk = Word.WdBreakType.wdPageBreak;

Word._Application word = new Word.Application();
Word._Document dc = new Word.Document();

dc.Bookmarks.Item(ref oEndOfDoc).Range.InsertFile<string path to inserted
file>,ref nRef,ref falseRef,ref nRef,ref nRef);

dc.Bookmarks.Item(ref oEndOfDoc).Range.InsertBreak(ref brk);

I use this code in one of my assemblies for building *unattended* word docs
in the background and, while still not the best performance, it does seem to
perform better than using the Selection object. I append around 15-20
different files (some HTML, some are other Word/text docs) and even with
retrieveing some of these files from a webservice, I am able to complete the
'append' operation in less than 35 seconds though I doubt this would bridge
the *5 minutes* you are referring to in your post.

HTH,

Alex

ajk said:
[This is a second post. My original did not include a reply address and I
kinda need replies! I apologize for the breach in ettiquette].

Hi, All:

I know how to insert files into a Word doc using C#. However, the program
I've written to do this runs much too slowly. The
"myObj".Application.Selection.InsertFile method executes at a snails pace.
Here are the detais:

I wrote a C# program that creates a new Word doc and then loops through a
list of HTML files to insert them into the new doc (sample code below). The
purpose of the program is to make one Word doc out of all of these Html
files (for archival purposes).

As each file is inserted, the instance of Word takes a long time to complete
the insert (I've waited up to 5 minutes for some pages to finish inserting).

Can someone explain why the insert takes so long? At first I thought it was
graphic sizes (some HTML files have .jpg or .gif files in them as big as
58kb). But Word even inserts the HTML files that don't contain graphics
fairly slowly. If someone knows that I am doing wrong or knows of a more
efficient way to accomplish this task, I'd appreciate hearing about it.
Thanks in advance for any help you all provide.

Sample code follows:

//set stub objects (left out for brevity)

//make word app and optimize
Word.ApplicationClass WordApp = new Word.ApplicationClass();
WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
WordApp.Visible = false;

//make word doc and optimize
wrdDoc = WordApp.Documents.Add(ref template, ref newTemplate, ref docType,
ref isVisible);
wrdDoc.Activate();
WordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdNormalView;

//add sFile to word doc
try
{
//insert file into wrdDoc
wrdDoc.Application.Selection.InsertFile(sFile, ref docrange, ref
conversions, ref links, ref attaches);

//insert pagebreak
wrdDoc.Application.Selection.InsertBreak(ref breaktype);
}
catch (System.Exception e)
{
//error trapping (not included for brevity)
}
 

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

Similar Threads


Top