Word Interop: Quickly Inserting HTML Files into Word Doc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajk

    Word Interop: Quickly Inserting HTML Files into Word Doc

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



  • Trebek

    #2
    Re: Word Interop: Quickly Inserting HTML Files into Word Doc

    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" <akilgore@spamc op.net> wrote in message
    news:40762ba9$0 $28893$9a6e19ea @news.newshosti ng.com...[color=blue]
    > [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).[/color]
    The[color=blue]
    > 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[/color]
    complete[color=blue]
    > the insert (I've waited up to 5 minutes for some pages to finish[/color]
    inserting).[color=blue]
    >
    > Can someone explain why the insert takes so long? At first I thought it[/color]
    was[color=blue]
    > 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)
    > }
    >
    >
    >[/color]


    Comment

    Working...