473,832 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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".Applica tion.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.Applicatio nClass WordApp = new Word.Applicatio nClass();
WordApp.Display Alerts = Word.WdAlertLev el.wdAlertsNone ;
WordApp.Visible = false;

//make word doc and optimize
wrdDoc = WordApp.Documen ts.Add(ref template, ref newTemplate, ref docType,
ref isVisible);
wrdDoc.Activate ();
WordApp.ActiveW indow.ActivePan e.View.Type = Word.WdViewType .wdNormalView;

//add sFile to word doc
try
{
//insert file into wrdDoc
wrdDoc.Applicat ion.Selection.I nsertFile(sFile , ref docrange, ref
conversions, ref links, ref attaches);

//insert pagebreak
wrdDoc.Applicat ion.Selection.I nsertBreak(ref breaktype);
}
catch (System.Excepti on e)
{
//error trapping (not included for brevity)
}

Nov 16 '05 #1
1 10565
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.WdBreakTyp e.wdPageBreak;

Word._Applicati on word = new Word.Applicatio n();
Word._Document dc = new Word.Document() ;

dc.Bookmarks.It em(ref oEndOfDoc).Rang e.InsertFile<st ring path to inserted
file>,ref nRef,ref falseRef,ref nRef,ref nRef);

dc.Bookmarks.It em(ref oEndOfDoc).Rang e.InsertBreak(r ef 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******@spamc op.net> wrote in message
news:40******** *************** @news.newshosti ng.com...
[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".Applica tion.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.Applicatio nClass WordApp = new Word.Applicatio nClass();
WordApp.Display Alerts = Word.WdAlertLev el.wdAlertsNone ;
WordApp.Visible = false;

//make word doc and optimize
wrdDoc = WordApp.Documen ts.Add(ref template, ref newTemplate, ref docType,
ref isVisible);
wrdDoc.Activate ();
WordApp.ActiveW indow.ActivePan e.View.Type = Word.WdViewType .wdNormalView;

//add sFile to word doc
try
{
//insert file into wrdDoc
wrdDoc.Applicat ion.Selection.I nsertFile(sFile , ref docrange, ref
conversions, ref links, ref attaches);

//insert pagebreak
wrdDoc.Applicat ion.Selection.I nsertBreak(ref breaktype);
}
catch (System.Excepti on 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
1026
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 have MS Office 2003. However I don't know which file I need to reference for the MS Word 11.0 Object Library. Or what else do I need to install/download from MS to be able to create MS Office 2003 Projects.
6
3861
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 this file within the word plugin within IE. I am not even sure how I can get this to work with a .doc file. I appreciate any help you can give......Thanks, Jason
0
2044
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 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...
1
615
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 snails pace. Here are the detais: I wrote a C# program that creates a new Word doc and then loops through a
2
2626
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 course) which will merge the contents of files which I will point ie. 1.doc and 2.doc into one file: "12.doc" I've just tried to find smth in MSDN help, and in google archive of programing groups but still I don't know how to do it:(
56
3456
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
8209
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 it. Specifically, when I try to call the Quit() method on the application object, VB gives me the following error: 'Quit' is ambiguous across the inherited interfaces 'Microsoft.Office.Interop.Word._Application' and...
5
2998
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 = word.Documents.Open(WordFileName.ToString) doc.Activate() word.Visible = True This code works on the development machine, but does not work on a test machine. The test machine is set up to use Office 2003, same as the development one.
4
1460
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 file in a such configuration. Or do I have to pass by a commercial component. That is to say for me, acquire a library and put it in /bin directory and call the right Namespace. Someone know a component wich allow to manipulate word file and...
0
2023
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: =============================================================================================== Error de servidor en la aplicación '/APPWebApp'. -------------------------------------------------------------------------------- ?
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10781
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4421
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.