C
CK
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"));
}
}
---
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"));
}
}
---