We have a situation whereby when adding a table to a MS Word document (admittedly clumisly through use of a bookmark) we are unable to print out the full set of pages.
At run-time we get no pages being printed.
If we put a Thread.Sleep(500) command in then we get one page - however we want all pages (which we can't achieve - short of putting an excessively long Sleep in for - which we do not want to do !!)
My code follows :
private void button1_Click(object sender, System.EventArgs e)
{
Word.Application Word_App = null;
Word.Document Word_doc = null;
Word.Documents Docs = null;
object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object oPrintRange = Word.WdPrintOutRange.wdPrintAllDocument;
object oPrintItems = Word.WdPrintOutItem.wdPrintDocumentContent;
object oPrintCopies = "1";
object oPrintPages = "1";//Word.WdPrintOutPages.wdPrintAllPages;
object oPrintPageType = Word.WdPrintOutPages.wdPrintAllPages;
// Create a word object that we can manipulate
try
{
if (Word_App != null)
{
Word_App = null;
}
Word_App = new Word.Application();
if (Word_doc != null)
{
Word_doc = null;
}
Word_doc = new Word.Document();
}
catch (Exception e1)
{
goto Exit;
}
try
{
if (Docs != null)
{
Docs = null;
}
Docs = Word_App.Documents;
Word._Document Doc1 = (Word._Document)Word_doc;
object oName = @"C:\TESTTable.doc";
// Open the document as indicated for feeding bookmarks to
Doc1 = Docs.Open(ref oName, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
// My BookMark in my test Word document is called 'TableBM'
object obBM_1 = "TableBM";
try
{
Word.Range rngRange1 = Word_App.ActiveDocument.Bookmarks.Item(ref obBM_1).Range;
rngRange1.Tables.AddOld(rngRange1, 200, 2);
Word.Table wrdTable1 = rngRange1.Tables.Item(1);
wrdTable1.Borders.Enable = 0;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 2; j++)
{
string s = "Row " + i.ToString() + " - Col " + j.ToString();
wrdTable1.Cell(i + 1, j + 1).Range.InsertAfter(s);
}
}
}
catch {}
object saveAs = @"C:\TestTable_Loaded.doc";
Doc1.SaveAs(ref saveAs, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Word_App.Visible = true;
Doc1.PrintOut(ref oTrue, ref oFalse, ref oPrintRange,
ref oMissing, ref oMissing, ref oMissing,
ref oPrintItems, ref oPrintCopies, ref oPrintPages,
ref oPrintPageType, ref oFalse,
ref oTrue, ref oMissing, ref oFalse, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
Word_App.Visible = false;
// Sleep for 500 ms (half a second)
Thread.Sleep(500);
// Ensure the document and Word are discarded.
Word_App.Quit(ref oFalse, ref oMissing, ref oMissing);
if (Doc1 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc1);
}
if (Docs != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Docs);
}
if (Word_doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_doc);
}
if (Word_App != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_App);
}
try
{
Doc1 = null;
}
catch {}
try
{
Docs = null;
}
catch {}
try
{
Word_doc = null;
}
catch {}
try
{
Word_App = null;
}
catch {}
}
catch (Exception ex)
{
MessageBox.Show("Failure. " + "\n\n" + ex.Message);
}
Exit : {}
}
We've invested a lot of time trying to work round this but have come up blank - can anyone shed any light on how we can get the full set of pages printed (5 pages in this case).
We basically want the application to ensure that all pages have printed before continuing.
As mentioned, with no sleep we get no pages at run-time, with 500ms we get one page and if we step through the code in the debugger then we get all pages when we hover over the 'PrintOut' command and let it does its thing .....
We're thinking that we might in some way be able to use application events to understand as to when the PrintOut method has completed it's workload - is this anywhere in the right direction with things ?
Any help / pointers would be greatly appreciated.
Thanks in advance
Steve
At run-time we get no pages being printed.
If we put a Thread.Sleep(500) command in then we get one page - however we want all pages (which we can't achieve - short of putting an excessively long Sleep in for - which we do not want to do !!)
My code follows :
private void button1_Click(object sender, System.EventArgs e)
{
Word.Application Word_App = null;
Word.Document Word_doc = null;
Word.Documents Docs = null;
object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object oPrintRange = Word.WdPrintOutRange.wdPrintAllDocument;
object oPrintItems = Word.WdPrintOutItem.wdPrintDocumentContent;
object oPrintCopies = "1";
object oPrintPages = "1";//Word.WdPrintOutPages.wdPrintAllPages;
object oPrintPageType = Word.WdPrintOutPages.wdPrintAllPages;
// Create a word object that we can manipulate
try
{
if (Word_App != null)
{
Word_App = null;
}
Word_App = new Word.Application();
if (Word_doc != null)
{
Word_doc = null;
}
Word_doc = new Word.Document();
}
catch (Exception e1)
{
goto Exit;
}
try
{
if (Docs != null)
{
Docs = null;
}
Docs = Word_App.Documents;
Word._Document Doc1 = (Word._Document)Word_doc;
object oName = @"C:\TESTTable.doc";
// Open the document as indicated for feeding bookmarks to
Doc1 = Docs.Open(ref oName, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
// My BookMark in my test Word document is called 'TableBM'
object obBM_1 = "TableBM";
try
{
Word.Range rngRange1 = Word_App.ActiveDocument.Bookmarks.Item(ref obBM_1).Range;
rngRange1.Tables.AddOld(rngRange1, 200, 2);
Word.Table wrdTable1 = rngRange1.Tables.Item(1);
wrdTable1.Borders.Enable = 0;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 2; j++)
{
string s = "Row " + i.ToString() + " - Col " + j.ToString();
wrdTable1.Cell(i + 1, j + 1).Range.InsertAfter(s);
}
}
}
catch {}
object saveAs = @"C:\TestTable_Loaded.doc";
Doc1.SaveAs(ref saveAs, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Word_App.Visible = true;
Doc1.PrintOut(ref oTrue, ref oFalse, ref oPrintRange,
ref oMissing, ref oMissing, ref oMissing,
ref oPrintItems, ref oPrintCopies, ref oPrintPages,
ref oPrintPageType, ref oFalse,
ref oTrue, ref oMissing, ref oFalse, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
Word_App.Visible = false;
// Sleep for 500 ms (half a second)
Thread.Sleep(500);
// Ensure the document and Word are discarded.
Word_App.Quit(ref oFalse, ref oMissing, ref oMissing);
if (Doc1 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc1);
}
if (Docs != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Docs);
}
if (Word_doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_doc);
}
if (Word_App != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_App);
}
try
{
Doc1 = null;
}
catch {}
try
{
Docs = null;
}
catch {}
try
{
Word_doc = null;
}
catch {}
try
{
Word_App = null;
}
catch {}
}
catch (Exception ex)
{
MessageBox.Show("Failure. " + "\n\n" + ex.Message);
}
Exit : {}
}
We've invested a lot of time trying to work round this but have come up blank - can anyone shed any light on how we can get the full set of pages printed (5 pages in this case).
We basically want the application to ensure that all pages have printed before continuing.
As mentioned, with no sleep we get no pages at run-time, with 500ms we get one page and if we step through the code in the debugger then we get all pages when we hover over the 'PrintOut' command and let it does its thing .....
We're thinking that we might in some way be able to use application events to understand as to when the PrintOut method has completed it's workload - is this anywhere in the right direction with things ?
Any help / pointers would be greatly appreciated.
Thanks in advance
Steve