Printing Option

S

satish

we are unable to set color property for printing using C#.Net for MSWord
Document through coding.How to set color property through code.
 
S

satish

hi,
Thanks for your response.
we are using microsoft word as document.we are using bookmarks to place
data in those documens.we have checkboxes to print which type of document we
want.i am sending sample code for 1 checkbox.

if (checkBox2.Checked == true)
{
object myTrue = true;
object myFalse = false;
object missingValue = Type.Missing;
object range =
Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
object items =
Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
object copies = "1";
object pages = "10";
object pageType =
Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
try
{


int j = 0;

for (; intClientId == 0 || j <
dsCompleteClient.Tables[2].Rows.Count; j++)
{


object oTemplate = arrDocuments[4];

oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);

for (int i = 0;intClientId!=0 && i <
dsCompleteClient.Tables[2].Columns.Count; i++)
{

oBookmarkName = "A" + (i + 1);

wrdRng = oDoc.Bookmarks.get_Item(ref
oBookmarkName).Range;
if (i == 7)
{
decimal al;

decimal.TryParse(dsCompleteClient.Tables[2].Rows[j].ToString(), out al);
string st = string.Format("{0:c2}", al);
wrdRng.InsertAfter(st);

}
else
{


wrdRng.InsertAfter(dsCompleteClient.Tables[2].Rows[j].ToString());
}
}

filename = arrDocuments[5];
if (j > 0)
{
filename =
filename.ToString().Replace("AnnuityInventoryWorksheet.doc", "Page" +
j.ToString() + ".doc");
}

oDoc.SaveAs(ref filename, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);

if (intClientId == 0 ||
dsCompleteClient.Tables[2].Rows.Count <= 1)
{
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();

object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter = prnName.ToString();
object
Background=printDialog1.PrinterSettings.DefaultPageSettings.Color;

//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
ref missing, ref missing,
ref missing,
ref items, ref copies,
ref pages, ref pageType, ref myFalse,
ref myTrue, ref missing,
ref myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
// ref missing, ref
missing, ref missing,
// ref items, ref copies,
ref pages, ref pageType, ref myFalse,
// ref myTrue, ref
missing, ref myTrue);
//}

if (oWord.BackgroundPrintingStatus > 0)
{
System.Threading.Thread.Sleep(1000);
}
}
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}
oDoc.Close(ref missing, ref missing, ref missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intClientId == 0)
{
break;
}

}
if (j > 1)
{
object oTemplate = arrDocuments[5];
filename = arrDocuments[5];
oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);
Word.Selection selection1 = oWord.Selection;
try
{
DirectoryInfo dir = new
DirectoryInfo(strcopyTemplate);
FileInfo[] fn = dir.GetFiles("Page*.doc");
for (int i = 0; i < fn.Length; i++)
{
if (File.Exists(fn.FullName.ToString()))
{

selection1.InsertFile(fn.FullName.ToString(), ref missing, ref missing,
ref missing, ref missing);
selection1.InsertBreak(ref pageBreak);
fn.Delete();
}
}
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
catch
{
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();
object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter = prnName.ToString();

printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
object Background =
printDialog1.PrinterSettings.DefaultPageSettings.Color;

//printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
ref missing, ref missing, ref
missing,
ref items, ref copies, ref
pages, ref pageType, ref myFalse,
ref myTrue, ref missing, ref
myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
// ref missing, ref missing,
ref missing,
// ref items, ref copies, ref
pages, ref pageType, ref myFalse,
// ref myTrue, ref missing,
ref myTrue);
//}
if (oWord.BackgroundPrintingStatus > 0)
{
System.Threading.Thread.Sleep(1000);
}
}


oDoc.Close(ref missing, ref missing, ref missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}


}
catch (Exception ex)
{
CommonModule.proc_ErrorMessage(ex.Message.ToString() +
"Error in Annuity data");
}
}
 
S

Shauna Kelly

Hi Satish

See the following, which present a comprehensive review of all
operations relating to printing in Word:

See Jonathan West's articles on controlling the printer
Controlling the Printer from Word VBA
Part 1: Using VBA to Select the Paper Tray
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=101

Controlling the Printer from Word VBA
Part 2: Using VBA to control Duplex, Color Mode and Print Quality
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116

Controlling the Printer from Word VBA
Part 3: Dealing with Different Paper Sizes
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=132

Controlling the Printer from Word VBA
Part 4: Getting printer driver details
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=183

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



satish said:
hi,
Thanks for your response.
we are using microsoft word as document.we are using bookmarks to
place
data in those documens.we have checkboxes to print which type of
document we
want.i am sending sample code for 1 checkbox.

if (checkBox2.Checked == true)
{
object myTrue = true;
object myFalse = false;
object missingValue = Type.Missing;
object range =
Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
object items =
Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
object copies = "1";
object pages = "10";
object pageType =
Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
try
{


int j = 0;

for (; intClientId == 0 || j <
dsCompleteClient.Tables[2].Rows.Count; j++)
{


object oTemplate = arrDocuments[4];

oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);

for (int i = 0;intClientId!=0 && i <
dsCompleteClient.Tables[2].Columns.Count; i++)
{

oBookmarkName = "A" + (i + 1);

wrdRng = oDoc.Bookmarks.get_Item(ref
oBookmarkName).Range;
if (i == 7)
{
decimal al;

decimal.TryParse(dsCompleteClient.Tables[2].Rows[j].ToString(), out
al);
string st = string.Format("{0:c2}",
al);
wrdRng.InsertAfter(st);

}
else
{


wrdRng.InsertAfter(dsCompleteClient.Tables[2].Rows[j].ToString());
}
}

filename = arrDocuments[5];
if (j > 0)
{
filename =
filename.ToString().Replace("AnnuityInventoryWorksheet.doc", "Page" +
j.ToString() + ".doc");
}

oDoc.SaveAs(ref filename, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);

if (intClientId == 0 ||
dsCompleteClient.Tables[2].Rows.Count <= 1)
{
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();

object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter =
prnName.ToString();
object
Background=printDialog1.PrinterSettings.DefaultPageSettings.Color;

//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background,
ref
myFalse, ref range,
ref missing, ref
missing,
ref missing,
ref items, ref
copies,
ref pages, ref pageType, ref myFalse,
ref myTrue, ref
missing,
ref myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background,
ref
myFalse, ref range,
// ref missing, ref
missing, ref missing,
// ref items, ref
copies,
ref pages, ref pageType, ref myFalse,
// ref myTrue, ref
missing, ref myTrue);
//}

if (oWord.BackgroundPrintingStatus > 0)
{

System.Threading.Thread.Sleep(1000);
}
}
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}
oDoc.Close(ref missing, ref missing, ref
missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intClientId == 0)
{
break;
}

}
if (j > 1)
{
object oTemplate = arrDocuments[5];
filename = arrDocuments[5];
oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);
Word.Selection selection1 = oWord.Selection;
try
{
DirectoryInfo dir = new
DirectoryInfo(strcopyTemplate);
FileInfo[] fn = dir.GetFiles("Page*.doc");
for (int i = 0; i < fn.Length; i++)
{
if
(File.Exists(fn.FullName.ToString()))
{

selection1.InsertFile(fn.FullName.ToString(), ref missing, ref
missing,
ref missing, ref missing);
selection1.InsertBreak(ref
pageBreak);
fn.Delete();
}
}
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
catch
{
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();
object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter = prnName.ToString();

printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
object Background =
printDialog1.PrinterSettings.DefaultPageSettings.Color;

//printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
ref missing, ref
missing, ref
missing,
ref items, ref copies,
ref
pages, ref pageType, ref myFalse,
ref myTrue, ref missing,
ref
myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
// ref missing, ref
missing,
ref missing,
// ref items, ref copies,
ref
pages, ref pageType, ref myFalse,
// ref myTrue, ref
missing,
ref myTrue);
//}
if (oWord.BackgroundPrintingStatus > 0)
{
System.Threading.Thread.Sleep(1000);
}
}


oDoc.Close(ref missing, ref missing, ref
missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}


}
catch (Exception ex)
{

CommonModule.proc_ErrorMessage(ex.Message.ToString() +
"Error in Annuity data");
}
}



Shauna Kelly said:
Hi Satish

For what kind of object are you setting the Color property? Do you
have
any sample code?

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
S

satish

Hi Shauna Thank you very much.I will check with the links and get back to you.

Shauna Kelly said:
Hi Satish

See the following, which present a comprehensive review of all
operations relating to printing in Word:

See Jonathan West's articles on controlling the printer
Controlling the Printer from Word VBA
Part 1: Using VBA to Select the Paper Tray
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=101

Controlling the Printer from Word VBA
Part 2: Using VBA to control Duplex, Color Mode and Print Quality
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116

Controlling the Printer from Word VBA
Part 3: Dealing with Different Paper Sizes
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=132

Controlling the Printer from Word VBA
Part 4: Getting printer driver details
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=183

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



satish said:
hi,
Thanks for your response.
we are using microsoft word as document.we are using bookmarks to
place
data in those documens.we have checkboxes to print which type of
document we
want.i am sending sample code for 1 checkbox.

if (checkBox2.Checked == true)
{
object myTrue = true;
object myFalse = false;
object missingValue = Type.Missing;
object range =
Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
object items =
Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
object copies = "1";
object pages = "10";
object pageType =
Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
try
{


int j = 0;

for (; intClientId == 0 || j <
dsCompleteClient.Tables[2].Rows.Count; j++)
{


object oTemplate = arrDocuments[4];

oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);

for (int i = 0;intClientId!=0 && i <
dsCompleteClient.Tables[2].Columns.Count; i++)
{

oBookmarkName = "A" + (i + 1);

wrdRng = oDoc.Bookmarks.get_Item(ref
oBookmarkName).Range;
if (i == 7)
{
decimal al;

decimal.TryParse(dsCompleteClient.Tables[2].Rows[j].ToString(), out
al);
string st = string.Format("{0:c2}",
al);
wrdRng.InsertAfter(st);

}
else
{


wrdRng.InsertAfter(dsCompleteClient.Tables[2].Rows[j].ToString());
}
}

filename = arrDocuments[5];
if (j > 0)
{
filename =
filename.ToString().Replace("AnnuityInventoryWorksheet.doc", "Page" +
j.ToString() + ".doc");
}

oDoc.SaveAs(ref filename, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);

if (intClientId == 0 ||
dsCompleteClient.Tables[2].Rows.Count <= 1)
{
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();

object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter =
prnName.ToString();
object
Background=printDialog1.PrinterSettings.DefaultPageSettings.Color;

//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background,
ref
myFalse, ref range,
ref missing, ref
missing,
ref missing,
ref items, ref
copies,
ref pages, ref pageType, ref myFalse,
ref myTrue, ref
missing,
ref myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background,
ref
myFalse, ref range,
// ref missing, ref
missing, ref missing,
// ref items, ref
copies,
ref pages, ref pageType, ref myFalse,
// ref myTrue, ref
missing, ref myTrue);
//}

if (oWord.BackgroundPrintingStatus > 0)
{

System.Threading.Thread.Sleep(1000);
}
}
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}
oDoc.Close(ref missing, ref missing, ref
missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intClientId == 0)
{
break;
}

}
if (j > 1)
{
object oTemplate = arrDocuments[5];
filename = arrDocuments[5];
oDoc = oWord.Documents.Add(ref oTemplate, ref
missing, ref missing, ref missing);
Word.Selection selection1 = oWord.Selection;
try
{
DirectoryInfo dir = new
DirectoryInfo(strcopyTemplate);
FileInfo[] fn = dir.GetFiles("Page*.doc");
for (int i = 0; i < fn.Length; i++)
{
if
(File.Exists(fn.FullName.ToString()))
{

selection1.InsertFile(fn.FullName.ToString(), ref missing, ref
missing,
ref missing, ref missing);
selection1.InsertBreak(ref
pageBreak);
fn.Delete();
}
}
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
catch
{
oDoc.SaveAs(ref filename, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing
, ref missing, ref missing);
}
if (bolPrinterSelect == true)
{
copies =
printDialog1.PrinterSettings.Copies.ToString();
object prnName =
printDialog1.PrinterSettings.PrinterName.ToString();
oWord.ActivePrinter = prnName.ToString();

printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
object Background =
printDialog1.PrinterSettings.DefaultPageSettings.Color;

//printDialog1.PrinterSettings.DefaultPageSettings.Color = true;
//if
(printDialog1.PrinterSettings.Duplex.ToString() == "Simplex")
//{
oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
ref missing, ref
missing, ref
missing,
ref items, ref copies,
ref
pages, ref pageType, ref myFalse,
ref myTrue, ref missing,
ref
myFalse);
//}
//else
//{
// oDoc.PrintOutOld(ref Background, ref
myFalse, ref range,
// ref missing, ref
missing,
ref missing,
// ref items, ref copies,
ref
pages, ref pageType, ref myFalse,
// ref myTrue, ref
missing,
ref myTrue);
//}
if (oWord.BackgroundPrintingStatus > 0)
{
System.Threading.Thread.Sleep(1000);
}
}


oDoc.Close(ref missing, ref missing, ref
missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (intSavePrint == 1)
{
fileDocuments.Add(filename);
}
}


}
catch (Exception ex)
{

CommonModule.proc_ErrorMessage(ex.Message.ToString() +
"Error in Annuity data");
}
}



Shauna Kelly said:
Hi Satish

For what kind of object are you setting the Color property? Do you
have
any sample code?

Shauna
 
S

satish

Hi shauna,
I could not get any useful information from the links u
sent.let me explain my problem exactly.We are using printoutold method for
printing.In that Printout method we dont have property for color.We are not
able to change the default settings of the printer with C# coding.In the
printdiaolog box even if we change settings for color it is not being
affected since it is not contained in printout method.so i want the code to
set default printer settings programatically.can you give me the solution.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top