473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 10471
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Helene Day | last post by:
I am trying to access the Word Objects from a .NET project. I have some sample from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/WordObject.asp and I...
6
by: Jason Steeves | last post by:
I am hoping someone can tell me how, or point me in the right direction for the following problem. I need to have word open a specified file, .mht, as a plug-in inside of IE so that I can edit...
0
by: ajk | last post by:
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...
1
by: ajk | last post by:
. 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...
2
by: Radek | last post by:
Hi, I have got such problem: in my directory "C:\folder" I have 3 files in MS WORD (having tables, images etc), these are: "1.doc", "2.doc", "3.doc". I want to write an application (C# of...
56
by: Richard Grene | last post by:
using Word automation from vb.net how can I get to the bottom of the document? Thanks, Richard
2
by: Steve | last post by:
I'm using Visual Studio .NET (not 2003), and am developing a class that works with Word theough the Office PIAs (Interop). I can open word and do things with it programatically, but I can't close...
5
by: Bob | last post by:
I'm getting error 8007007e when executing the following code. Dim word As New Microsoft.Office.Interop.Word.Application Dim doc As Microsoft.Office.Interop.Word.Document doc =...
4
by: fabrice | last post by:
Hello I m working with Framework 1.1. I d like to interact with word files (.doc or .dot) from the server. But Neither office or word are installed on it. There is a way for manipulating word...
0
by: SergioT | last post by:
hi I m getting an error when I run my application on the server, that error doesn't ocur when I run the application on the development computer the error is:...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.