Microsoft Office Forums


Reply
Thread Tools Display Modes

Word Automation - Word Cannot Fire Event

 
 
CK
Guest
Posts: n/a
 
      06-12-2009, 09:12 AM
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.G etTypeFromProgID("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.EmployeeInf oDataTable 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[i][dt.TotalPayColumn.ColumnName]);

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

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

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

if (WORDDOC == null)
{
object objTemplate =
dt.Rows[i][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[i][dt.TemplateDocColumn.ColumnName].ToString(), ref ObjMissing, ref ObjFalse, ref ObjMissing, ref ObjMissing);
}

MergeBookmark(true, BKM_REPNAME,
dt.Rows[i][dt.RepNameColumn.ColumnName].ToString());
MergeBookmark(true, BKM_REPNRIC,
dt.Rows[i][dt.RepNRICColumn.ColumnName].ToString());
MergeBookmark(true, BKM_TOTALPAY, String.Format("{0:N0}", totalPay));
MergeBookmark(true, BKM_RESPONDENTID,
ConvertToBarcode(dt.Rows[i][dt.SerialNoColumn.ColumnName].ToString()));
MergeBookmark(true, BKM_RESPONDENTID_TEXT,
dt.Rows[i][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)Activa tor.CreateInstance(Type.GetTypeFromProgID("Word.Ap plication"));
}
}

---

 
Reply With Quote
 
 
 
 
Cindy M.
Guest
Posts: n/a
 
      06-12-2009, 09:40 AM
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
:-)

 
Reply With Quote
 
 
 
 
CK
Guest
Posts: n/a
 
      06-15-2009, 08:26 AM
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
> :-)
>
>

 
Reply With Quote
 
Cindy M.
Guest
Posts: n/a
 
      06-15-2009, 10:38 AM
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[i][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 :-)

 
Reply With Quote
 
CK
Guest
Posts: n/a
 
      06-16-2009, 01:02 AM
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[i][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 :-)
>
>

 
Reply With Quote
 
Cindy M.
Guest
Posts: n/a
 
      06-16-2009, 09:07 AM
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 :-)

 
Reply With Quote
 
CK
Guest
Posts: n/a
 
      06-17-2009, 03:21 PM
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 :-)
>
>

 
Reply With Quote
 
Cindy M.
Guest
Posts: n/a
 
      06-18-2009, 01:02 PM
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 :-)

 
Reply With Quote
 
CK
Guest
Posts: n/a
 
      06-19-2009, 02:50 PM
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 :-)
>
>

 
Reply With Quote
 
CK
Guest
Posts: n/a
 
      06-19-2009, 02:59 PM
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.Applicat ion"))

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Automation - Word Cannot Fire Event CK Word Documents 0 06-12-2009 08:13 AM
EXIT EVENT doesn't fire until I exit the form. Shouldn't it fire on exit of control? Marty L VBA Developer 0 08-29-2006 01:43 PM
Opening Excel workbook via automation doesn't fire event procedures? Jeremy Gollehon Excel Programming 2 07-07-2006 03:50 PM
Need Fire Department Symbols for Pre-fire Plans(hydrants, f.d.c, . Fire Lt. 120 Visio General 0 01-07-2005 03:05 PM
Event Procedures: Event on Worksheet to fire Event on another Worksheet Kathryn Excel Programming 2 04-07-2004 06:35 PM


All times are GMT. The time now is 01:32 PM.
Microsoft Office Forums is not affiliated with Microsoft Corporation.


Welcome!
Welcome to the Microsoft Office Forums
 


Latest Threads
Mail Merging Content With High Quality
oleander (04-17-2014, 11:59 PM)

Aargh. What is this?
PeteJ (04-17-2014, 12:10 PM)

Word 2013 Insert Symbol (Not Responding)
tpthrshr (04-17-2014, 02:49 AM)

Can't save a large Word document
Josh (04-17-2014, 01:03 AM)

Remote help?
newman200 (04-13-2014, 12:54 PM)