PbPrintStyle out of range (C#)

M

Mason

I would like to print a document with the
PbPrintStyle.pbPrintStyleBookletSideFold style but it raises an exception
that the enum is out of range.

Here is my call:

//This always prints as a single page.
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
myDoc.printStyle);

//This doesn't work and throws an exception.
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
PbPrintStyle.pbPrintStyleBookletSideFold );

Any ideas?
 
E

Ed Bennett

Mason said:
Yeah. I thought the same thing. So, I tried:
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
(PbPrintStyle)5);

But, I still get the same error. It is out of range.

I'm assuming that you are doing something like
using Microsoft.Office.Interop.Publisher;
(or whatever the C# equivalent is); otherwise your reference to
PbPrintStyle isn't properly qualified.

Do any of the other members of the enumeration function properly?
 
M

Mason

Do any of the other members of the enumeration function properly?

That's right. I am just doing using Microsoft.Office.Interop.Publisher;

I did a quiick spot check of the other enum values (pbPrintStyleDefault,
pbPrintStyleOnePagePerSheet, pbPrintStyleTiled) and only pbPrintStyleDefault
works. It is printing one page per sheet. So, that must be my default or the
document's default.

Thanks for your help.
 
M

Mason

Does anyone know who might be able to answer this question or where to post
this question? I am still stuck on it. Thanks.
 
E

Ed Bennett

Mason said:
Does anyone know who might be able to answer this question or where to post
this question? I am still stuck on it. Thanks.

If you try the same thing on the same document from Publisher VBA, does
it work? (i.e. Is the problem with the COM interop or with the object
model?)
 
M

Miguel Gonzalez

Your first call will use the default print style for the document, which is
probably pbPrintStyleOnePagePerSheet from what you are describing.

In the second call, the message in the exception is confusing, but it
basically tries to tell you that the print style is not available for the
document you are using.

Can you check if your document can be printed as a booklet side fold through
the UI? (File | Print Setup then check the available styles in the Print
Options list - you should be able to use the enum values for the print
options that are displayed there in your code.)

Hope this helps,
Miguel
 
M

Miguel Gonzalez

Interesting. This is a bug in Publisher. If I run that code on the immediate
window of the VB Editor after opening the Print Setup dialog, the code works
as expected. However, if I don't open the dialog, I get an error like the one
you are getting. That is what threw me off. The workaround for this is kind
of too overkill, but here it is in case you want to try it. I can avoid
hitting this exception if I do this in C#:

static void Main(string[] args)
{
//Create the instance
ApplicationClass pubApp = new ApplicationClass();
Document myDoc = pubApp.ActiveDocument;
PageSizes pageSizes = myDoc.PageSetup.AvailablePageSizes;
myDoc.ActiveWindow.Visible = true;

//Find a booklet page size
foreach(PageSize ps in pageSizes)
{
if (ps.Name == "1/2 A4 Booklet")
{
//Switch my default document to something that supports
booklet side fold
myDoc.PageSetup.PageSize = ps;
break;
}
}
//Bring up and close the print setup dialog to be able to
//print to other styles
pubApp.ActiveDocument.ActiveWindow.Activate();
System.Windows.Forms.SendKeys.SendWait("%(fr)");
System.Windows.Forms.SendKeys.SendWait("{ESC}");
//Now print this to my default printer using
pbPrintStyleBookletSideFold
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
PbPrintStyle.pbPrintStyleBookletSideFold);

pubApp.Quit();
}

Hope this helps,
-Miguel
 
E

Ed Bennett

Miguel said:
Interesting. This is a bug in Publisher. If I run that code on the immediate
window of the VB Editor after opening the Print Setup dialog, the code works
as expected. However, if I don't open the dialog, I get an error like the one
you are getting. That is what threw me off. The workaround for this is kind
of too overkill, but here it is in case you want to try it. I can avoid
hitting this exception if I do this in C#:

Wow, that is some brilliant research, thanks Miguel!

(I do hope you'll stick around :) )
 
M

Miguel Gonzalez

Actually this might not be a bug after all: the exception you are getting is
related to the page size in the printer and not in the document. So this
probably means that default page size in your printer is letter, so
PrintOutEx throws an exception since letter doesn't support Booklet Side
Fold.

The reason why this works after opening the page size dialog is because that
dialog changes your default page size from Letter to A4 when you open it.

What you'll need to do to make your code work is to ensure that the paper
size in your printer is set to something that supports the
pbPrintStyleBookletSideFold. To do that you can just add this code before you
print:

//Find the active printer
//Printer and myPrinter are Microsoft.Office.Interop.Publisher.Printer
foreach (Printer printer in pubApp.InstalledPrinters)
{
if (printer.IsActivePrinter)
{
myPrinter = printer;
break;
}
}
//Set the printer page size and orientation to the same size and orientation
that you use when you print from the UI (in my case that would be landscape,
8.5" x 14")

myPrinter.PaperWidth = Convert.ToInt32(pubApp.InchesToPoints(8.5f));
myPrinter.PaperHeight = Convert.ToInt32(pubApp.InchesToPoints(14f));
myPrinter.PaperOrientation = PbOrientationType.pbOrientationLandscape;

//Now print this the default printer using pbPrintStyleBookletSideFold
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
PbPrintStyle.pbPrintStyleBookletSideFold);

With this you should be able to print your file without hitting the exception.

-Miguel

Miguel Gonzalez said:
Interesting. This is a bug in Publisher. If I run that code on the immediate
window of the VB Editor after opening the Print Setup dialog, the code works
as expected. However, if I don't open the dialog, I get an error like the one
you are getting. That is what threw me off. The workaround for this is kind
of too overkill, but here it is in case you want to try it. I can avoid
hitting this exception if I do this in C#:

static void Main(string[] args)
{
//Create the instance
ApplicationClass pubApp = new ApplicationClass();
Document myDoc = pubApp.ActiveDocument;
PageSizes pageSizes = myDoc.PageSetup.AvailablePageSizes;
myDoc.ActiveWindow.Visible = true;

//Find a booklet page size
foreach(PageSize ps in pageSizes)
{
if (ps.Name == "1/2 A4 Booklet")
{
//Switch my default document to something that supports
booklet side fold
myDoc.PageSetup.PageSize = ps;
break;
}
}
//Bring up and close the print setup dialog to be able to
//print to other styles
pubApp.ActiveDocument.ActiveWindow.Activate();
System.Windows.Forms.SendKeys.SendWait("%(fr)");
System.Windows.Forms.SendKeys.SendWait("{ESC}");
//Now print this to my default printer using
pbPrintStyleBookletSideFold
myDoc.PrintOutEx(1, myDoc.Pages.Count, String.Empty, 1, false,
PbPrintStyle.pbPrintStyleBookletSideFold);

pubApp.Quit();
}

Hope this helps,
-Miguel

Mason said:
Yes, I can print the same publisher document as a booklet from Publisher.
 

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