Word Automation - Word Cannot Fire Event

Discussion in 'Word VBA' started by CK, Jun 12, 2009.

  1. CK

    CK Guest

    Hi, I have been facing this issues in my Word Automation programme.

    We have 2 or more Word documents generated by our windows application
    simutaneously and will be printed out. These documents are actually generated
    from our own Word template, which consists of bookmarks and tables.

    However, whenever the application generates the very first document (add doc
    from template), it throws the "Word Cannot Fire Event" exception". This
    exception is thrown right after the "Application.Documents.Add()" statement.

    The remaining documents generated at the same time will not have this error.

    I have searched the possible solutions like instead of creating the
    Word.Application object with

    object WdApp = new WORD.Application();

    use this:

    object WdApp =
    (WORD._Application)Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));

    However, all the solutions don't seem to work.

    All helps are deeply appreciated. Also, snippet of code are attached at the
    bottom of this thread.

    Thanks.

    -------------------
    Snippet Code
    -------------------

    public void GenerateWordDocument(DataSet.DsPaySlip.EmployeeInfoDataTable dt)
    {
    try
    {
    object savedFilePath = "";

    string currentTemplate = "";
    string currentPrinter = "";
    short currentTrayCode = 0;

    InitializeWord();

    WORDAPP.Visible = false;

    for (int i = 0; i < dt.Rows.Count; i++)
    {
    bool append = false;
    long totalPay = Convert.ToInt64(dt.Rows[dt.TotalPayColumn.ColumnName]);

    // Check if tempalte is different from existing 1
    if (currentTemplate == "" || currentTemplate !=
    dt.Rows[dt.TemplateDocColumn.ColumnName].ToString() ||
    currentPrinter != dt.Rows[dt.PrinterNameColumn.ColumnName].ToString()
    || currentTrayCode !=
    Convert.ToInt16(dt.Rows[dt.PrinterTrayCodeColumn.ColumnName]))
    {
    if (currentTemplate != "")
    {
    SaveDocument(savedFilePath);
    PrintWordDoc(savedFilePath, currentPrinter, currentTrayCode);
    CloseWordDoc(false, savedFilePath);
    WORDDOC = null;
    }

    currentTemplate = dt.Rows[dt.TemplateDocColumn.ColumnName].ToString();
    currentPrinter = dt.Rows[dt.PrinterNameColumn.ColumnName].ToString();
    currentTrayCode =
    Convert.ToInt16(dt.Rows[dt.PrinterTrayCodeColumn.ColumnName]);
    }

    // Set the target file here
    savedFilePath = dt.Rows[dt.SavedDocColumn.ColumnName];

    if (WORDDOC == null)
    {
    object objTemplate =
    dt.Rows[dt.TemplateDocColumn.ColumnName].ToString();
    WORDDOC = WORDAPP.Documents.Add(
    /* ref object Template */ ref objTemplate,
    /* ref object NewTemplate */ ref ObjMissing,
    /* ref object DocumentType */ ref ObjMissing,
    /* ref object Visible */ ref ObjMissing);
    }
    else
    {
    append = true;
    object objStart = WORDDOC.Content.End - 1;
    WORDRANGE = WORDDOC.Range(ref objStart, ref ObjMissing);
    WORDRANGE.InsertBreak(ref ObjPageBreak);
    WORDRANGE.InsertFile(dt.Rows[dt.TemplateDocColumn.ColumnName].ToString(), ref ObjMissing, ref ObjFalse, ref ObjMissing, ref ObjMissing);
    }

    MergeBookmark(true, BKM_REPNAME,
    dt.Rows[dt.RepNameColumn.ColumnName].ToString());
    MergeBookmark(true, BKM_REPNRIC,
    dt.Rows[dt.RepNRICColumn.ColumnName].ToString());
    MergeBookmark(true, BKM_TOTALPAY, String.Format("{0:N0}", totalPay));
    MergeBookmark(true, BKM_RESPONDENTID,
    ConvertToBarcode(dt.Rows[dt.SerialNoColumn.ColumnName].ToString()));
    MergeBookmark(true, BKM_RESPONDENTID_TEXT,
    dt.Rows[dt.SerialNoColumn.ColumnName].ToString());
    //Remove the blank page which is automatically inserted by Ms Word
    if (append)
    {
    WORDDOC.Paragraphs.Last.Range.Delete(ref ObjMissing, ref ObjMissing);
    }
    }
    SaveDocument(savedFilePath);
    PrintWordDoc(savedFilePath, currentPrinter, currentTrayCode);
    CloseWordDoc(false, savedFilePath);
    }
    catch (Exception ex)
    {
    // Set it to visible
    WORDAPP.Visible = true;
    // Throw the exception to caller
    throw (ex);
    }
    }

    public void InitializeWord()
    {
    if (WORDAPP == null)
    {
    WORDAPP = new Microsoft.Office.Interop.Word.Application();
    // Alternative way to create the Word Application instance
    //WORDAPP =
    (Microsoft.Office.Interop.Word._Application)Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
    }
    }

    ---
     
    CK, Jun 12, 2009
    #1
    1. Advertising

  2. CK

    Cindy M. Guest

    Hi CK

    Which version of Word are we dealing with?

    Do any of these template contain macros? I'm thinking
    about, for example, a Document_New procedure in the
    template's ThisDocument module.

    That seems to me the most likely cause of the error message
    (which I've never before encountered, by the way).

    Another possibility could be an AutoExec macro in the
    Normal.dot template or a template being loaded as an Add-in
    (stored in the Startup folder).

    If you start up Word as an end-user, then pick up the
    application instance using GetActiveObject (instead of
    creating a new instance) does your code run without the
    error message? (Just trying to narrow down if it is
    something triggering when Word is loading.)

    Another thing to try would be to start Word in Safe Mode,
    via the Diagnostics.Process namepace (start a new Process
    on the command line, using winword.exe /a. The /a switch
    will start Word in Safe Mode - Normal.dot and other add-in
    templates won't load. That would tell you whether it's
    something in one of those.

    I don't see that you add any event handlers/delegates to
    your project in your code, so I assume there isn't any in
    your project?

    Cindy Meister
    INTER-Solutions, Switzerland
    http://homepage.swissonline.ch/cindymeister (last update
    Jun 17 2005)
    http://www.word.mvps.org

    This reply is posted in the Newsgroup; please post any
    follow question or reply in the newsgroup and not by e-mail
    :)
     
    Cindy M., Jun 12, 2009
    #2
    1. Advertising

  3. CK

    CK Guest

    Hi Cindy,

    First of all, thanks for your the help.

    As suggested, I have removed all template add-ins that are not needed. It
    solves the "Word cannot fire event" error.

    However, another error occurs - "There is insufficient memory. Save the
    document now". Likewise, the way to replicate this error is exactly the same
    as "Word cannot fire event".

    FYI, I am using Word 2003. There is no macro (checked) and no add-ins.

    Also, may I know what kind of event handlers/delegates that are normally
    needed in word automation?

    Thanks again and best regards,

    CK

    "Cindy M." wrote:

    > Hi CK
    >
    > Which version of Word are we dealing with?
    >
    > Do any of these template contain macros? I'm thinking
    > about, for example, a Document_New procedure in the
    > template's ThisDocument module.
    >
    > That seems to me the most likely cause of the error message
    > (which I've never before encountered, by the way).
    >
    > Another possibility could be an AutoExec macro in the
    > Normal.dot template or a template being loaded as an Add-in
    > (stored in the Startup folder).
    >
    > If you start up Word as an end-user, then pick up the
    > application instance using GetActiveObject (instead of
    > creating a new instance) does your code run without the
    > error message? (Just trying to narrow down if it is
    > something triggering when Word is loading.)
    >
    > Another thing to try would be to start Word in Safe Mode,
    > via the Diagnostics.Process namepace (start a new Process
    > on the command line, using winword.exe /a. The /a switch
    > will start Word in Safe Mode - Normal.dot and other add-in
    > templates won't load. That would tell you whether it's
    > something in one of those.
    >
    > I don't see that you add any event handlers/delegates to
    > your project in your code, so I assume there isn't any in
    > your project?
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update
    > Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any
    > follow question or reply in the newsgroup and not by e-mail
    > :)
    >
    >
     
    CK, Jun 15, 2009
    #3
  4. CK

    Cindy M. Guest

    Hi Ck,

    > As suggested, I have removed all template add-ins that are not needed. It
    > solves the "Word cannot fire event" error.
    >
    > However, another error occurs - "There is insufficient memory. Save the
    > document now". Likewise, the way to replicate this error is exactly the same
    > as "Word cannot fire event".
    >
    > FYI, I am using Word 2003. There is no macro (checked) and no add-ins.
    >
    > Also, may I know what kind of event handlers/delegates that are normally
    > needed in word automation?


    This is the first time I've ever encountered this error message under the
    described circumstances. As far as I know, you shouldn't need anything. I have
    the impression that Word is somehow having internal difficulties.

    So, the "insufficient memory" message is happening when you create the first
    document from a template, but not with other ones? And you're seeing this under
    which circumstances:
    - as an end-user, creating a document from the template?
    - While Word is in Safe Mode?
    - Running your project "normally"?

    Is this the code (second statement of the two) that's triggering the error
    message? If yes, could you show us a sample of what the content of the data
    field (line 1) could be?

    object objTemplate =
    dt.Rows[dt.TemplateDocColumn.ColumnName].ToString();
    WORDDOC = WORDAPP.Documents.Add(
    /* ref object Template */ ref objTemplate,
    /* ref object NewTemplate */ ref ObjMissing,
    /* ref object DocumentType */ ref ObjMissing,
    /* ref object Visible */ ref ObjMissing);

    Cindy Meister
    INTER-Solutions, Switzerland
    http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    http://www.word.mvps.org

    This reply is posted in the Newsgroup; please post any follow question or reply
    in the newsgroup and not by e-mail :)
     
    Cindy M., Jun 15, 2009
    #4
  5. CK

    CK Guest

    Hi Cindy,

    The problem happens when >1 Word document generated simultaneously from the
    same template via my project (running "normally").

    The objTemplate is the full path of the template, e.g.
    "D:\MyProject\Template\Word\MyTemplate1.dot".

    When the error is caught in the "catch" block and set to visible mode, i
    noticed that it actually managed to open up the template, just that it
    couldn't proceed any further from there. Could it be the Word instance needs
    to be called at least once to be really available for further action? Or is
    there any latency that causes the programme to proceed without waiting for
    the document to be fully opened?

    All helps are deeply appreciated.

    REgards,
    CK

    "Cindy M." wrote:

    > Hi Ck,
    >
    > > As suggested, I have removed all template add-ins that are not needed. It
    > > solves the "Word cannot fire event" error.
    > >
    > > However, another error occurs - "There is insufficient memory. Save the
    > > document now". Likewise, the way to replicate this error is exactly the same
    > > as "Word cannot fire event".
    > >
    > > FYI, I am using Word 2003. There is no macro (checked) and no add-ins.
    > >
    > > Also, may I know what kind of event handlers/delegates that are normally
    > > needed in word automation?

    >
    > This is the first time I've ever encountered this error message under the
    > described circumstances. As far as I know, you shouldn't need anything. I have
    > the impression that Word is somehow having internal difficulties.
    >
    > So, the "insufficient memory" message is happening when you create the first
    > document from a template, but not with other ones? And you're seeing this under
    > which circumstances:
    > - as an end-user, creating a document from the template?
    > - While Word is in Safe Mode?
    > - Running your project "normally"?
    >
    > Is this the code (second statement of the two) that's triggering the error
    > message? If yes, could you show us a sample of what the content of the data
    > field (line 1) could be?
    >
    > object objTemplate =
    > dt.Rows[dt.TemplateDocColumn.ColumnName].ToString();
    > WORDDOC = WORDAPP.Documents.Add(
    > /* ref object Template */ ref objTemplate,
    > /* ref object NewTemplate */ ref ObjMissing,
    > /* ref object DocumentType */ ref ObjMissing,
    > /* ref object Visible */ ref ObjMissing);
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any follow question or reply
    > in the newsgroup and not by e-mail :)
    >
    >
     
    CK, Jun 16, 2009
    #5
  6. CK

    Cindy M. Guest

    Hi Ck,

    > The problem happens when >1 Word document generated simultaneously from the
    > same template via my project (running "normally").


    Do I understand "Simultaneously" correctly: More than one document is being
    generated at the same time from the template (by more than one instance of
    Word / more than one user)?

    If you're starting Word in Safe Mode does this problem also not occur? If that
    is the case, try renaming the Normal.dot template to Normal.dot.OLD (so that
    Word can't find it). This will cause Word to generate a "clean" Normal.dot
    when it starts. If the problem is then gone when you run normally, then the
    renamed Normal.dot has probably been damaged.

    > Or is
    > there any latency that causes the programme to proceed without waiting for
    > the document to be fully opened?


    This could be happening... You could try building a "sleep time" into your
    code to see if that makes a difference.

    FWIW the "insufficient memory" error usually indicates a scratch file and/or
    an Undo buffer overload. Most often one sees this when doing a lot of
    formatting, or generating tables cell-by-cell. The usual recommendation is to
    save the document and empty the UnDo buffer. This wouldn't seem appropriate in
    your scenario, however.

    Cindy Meister
    INTER-Solutions, Switzerland
    http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    http://www.word.mvps.org

    This reply is posted in the Newsgroup; please post any follow question or
    reply in the newsgroup and not by e-mail :)
     
    Cindy M., Jun 16, 2009
    #6
  7. CK

    CK Guest

    Hi Cindy,

    Simultaneous here means More than one document is being
    generated at the same time from the template (by more than one instance of
    Word [SingleUser]).

    Tried the solutions suggested, but to no avail. I even have clear the Undo
    after every bookmark replacement.

    It always throw the same error whenever the application generates the
    documents for the very first time after reboot.

    Thanks for the help.

    REgards,
    CK

    "Cindy M." wrote:

    > Hi Ck,
    >
    > > The problem happens when >1 Word document generated simultaneously from the
    > > same template via my project (running "normally").

    >
    > Do I understand "Simultaneously" correctly: More than one document is being
    > generated at the same time from the template (by more than one instance of
    > Word / more than one user)?
    >
    > If you're starting Word in Safe Mode does this problem also not occur? If that
    > is the case, try renaming the Normal.dot template to Normal.dot.OLD (so that
    > Word can't find it). This will cause Word to generate a "clean" Normal.dot
    > when it starts. If the problem is then gone when you run normally, then the
    > renamed Normal.dot has probably been damaged.
    >
    > > Or is
    > > there any latency that causes the programme to proceed without waiting for
    > > the document to be fully opened?

    >
    > This could be happening... You could try building a "sleep time" into your
    > code to see if that makes a difference.
    >
    > FWIW the "insufficient memory" error usually indicates a scratch file and/or
    > an Undo buffer overload. Most often one sees this when doing a lot of
    > formatting, or generating tables cell-by-cell. The usual recommendation is to
    > save the document and empty the UnDo buffer. This wouldn't seem appropriate in
    > your scenario, however.
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any follow question or
    > reply in the newsgroup and not by e-mail :)
    >
    >
     
    CK, Jun 17, 2009
    #7
  8. CK

    Cindy M. Guest

    Hi Ck,

    > Simultaneous here means More than one document is being
    > generated at the same time from the template (by more than one instance of
    > Word [SingleUser]).


    I'm sure this can cause problems (due to file locking of the template), but I
    have to admit I'm at a loss as to how you can solve what you're seeing.

    Given the circumstances, in your place I might try immediately closing the
    first document being generated (without saving) and creating another one.
    That will be slow since you'll have to wait for the structured error handling
    to run, but it might be the only way...

    Cindy Meister
    INTER-Solutions, Switzerland
    http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    http://www.word.mvps.org

    This reply is posted in the Newsgroup; please post any follow question or
    reply in the newsgroup and not by e-mail :)
     
    Cindy M., Jun 18, 2009
    #8
  9. CK

    CK Guest

    Hi Cindy,

    Thanks for all helps offered.

    Well, I think I have to heed your advice and feedback to my boss. Will
    update this thread in case if I found a solution (if miracle happens).

    Thanks again.

    REgards,
    CK

    "Cindy M." wrote:

    > Hi Ck,
    >
    > > Simultaneous here means More than one document is being
    > > generated at the same time from the template (by more than one instance of
    > > Word [SingleUser]).

    >
    > I'm sure this can cause problems (due to file locking of the template), but I
    > have to admit I'm at a loss as to how you can solve what you're seeing.
    >
    > Given the circumstances, in your place I might try immediately closing the
    > first document being generated (without saving) and creating another one.
    > That will be slow since you'll have to wait for the structured error handling
    > to run, but it might be the only way...
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any follow question or
    > reply in the newsgroup and not by e-mail :)
    >
    >
     
    CK, Jun 19, 2009
    #9
  10. CK

    CK Guest

    Hi Cindy,

    Just to inform that I have tried another of generating the documents, but
    still encounter the same error.

    Instead of add a new file from a template, I created a .doc. Whenever I need
    to generate a document, I will copy that .doc, paste it in another directory
    and only then open up the .doc for processing. Unfortunately, no luck at all.

    However, I am curious why the file is successfully opened but still throwing
    the error? I have 10GB HDD space, 1.5GB RAM for an XP Pro system.

    Does WORD lock the Normal.dot whenever a saved .doc is opened? Also, Is
    there a way to open WORD in safe mode from

    new Word._Application() or
    Activator.CreateObject(Type.GetType("Word.Application"))

    Thanks for the help.

    Regards,
    CK

    "Cindy M." wrote:

    > Hi Ck,
    >
    > > Simultaneous here means More than one document is being
    > > generated at the same time from the template (by more than one instance of
    > > Word [SingleUser]).

    >
    > I'm sure this can cause problems (due to file locking of the template), but I
    > have to admit I'm at a loss as to how you can solve what you're seeing.
    >
    > Given the circumstances, in your place I might try immediately closing the
    > first document being generated (without saving) and creating another one.
    > That will be slow since you'll have to wait for the structured error handling
    > to run, but it might be the only way...
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any follow question or
    > reply in the newsgroup and not by e-mail :)
    >
    >
     
    CK, Jun 19, 2009
    #10
  11. CK

    Cindy M. Guest

    Hi Ck,

    > Does WORD lock the Normal.dot whenever a saved .doc is opened? Also, Is
    > there a way to open WORD in safe mode from
    >

    Normal.dot will be file-locked whenever Word is open and running because it's
    loaded as a "global template". In any case, though, the users shouldn't be
    *sharing* a single Normal.dot. Is that how your organization is set up?

    > New Word._Application() or
    > Activator.CreateObject(Type.GetType("Word.Application"))


    Not these two ways, no. Only by "shelling" it using a command line command.

    Cindy Meister
    INTER-Solutions, Switzerland
    http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    http://www.word.mvps.org

    This reply is posted in the Newsgroup; please post any follow question or reply
    in the newsgroup and not by e-mail :)
     
    Cindy M., Jun 20, 2009
    #11
  12. CK

    CK Guest

    Hi Cindy,

    Thanks for the help all this while.

    I managed to solve this problem with a workaround solution. Before
    generating my the documents, I created a word instance and open up a blank
    the document. This document is opened until my application quits.

    So, when it comes to new documents generation (simultaneously), it no longer
    throws any error. FYI, all new documents are generated by different WORD
    instances.

    Thanks again.

    Regards,
    CK

    "Cindy M." wrote:

    > Hi Ck,
    >
    > > Does WORD lock the Normal.dot whenever a saved .doc is opened? Also, Is
    > > there a way to open WORD in safe mode from
    > >

    > Normal.dot will be file-locked whenever Word is open and running because it's
    > loaded as a "global template". In any case, though, the users shouldn't be
    > *sharing* a single Normal.dot. Is that how your organization is set up?
    >
    > > New Word._Application() or
    > > Activator.CreateObject(Type.GetType("Word.Application"))

    >
    > Not these two ways, no. Only by "shelling" it using a command line command.
    >
    > Cindy Meister
    > INTER-Solutions, Switzerland
    > http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
    > http://www.word.mvps.org
    >
    > This reply is posted in the Newsgroup; please post any follow question or reply
    > in the newsgroup and not by e-mail :)
    >
    >
     
    CK, Jun 23, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fire Lt. 120
    Replies:
    0
    Views:
    148
    Fire Lt. 120
    Jan 7, 2005
  2. CK
    Replies:
    0
    Views:
    50
  3. Kathryn
    Replies:
    2
    Views:
    114
    Kathryn
    Apr 7, 2004
  4. Jeremy Gollehon
    Replies:
    2
    Views:
    44
    Jeremy Gollehon
    Jul 7, 2006
  5. Marty L
    Replies:
    0
    Views:
    142
    Marty L
    Aug 29, 2006
Loading...

Share This Page