469,267 Members | 958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Word Interop: Quickly Inserting HTML Files into Word Doc

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)
}

Nov 16 '05 #1
1 9712
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" <ak******@spamcop.net> wrote in message
news:40***********************@news.newshosting.co m...
[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)
}

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jason Steeves | last post: by
2 posts views Thread by Radek | last post: by
56 posts views Thread by Richard Grene | last post: by
2 posts views Thread by Steve | last post: by
4 posts views Thread by fabrice | last post: by
reply views Thread by SergioT | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.