jeudi 6 juillet 2017

How to set Paper size during printing in word document using reflection?

I want to set paper size in MSWORD document before sending to printing. For that i use interop services and reflection. Following are my code snippet.

var wordType = Type.GetTypeFromProgID("Word.Application");
var application = Activator.CreateInstance(wordType);
Type type = application.GetType();
object activeDocument = type.InvokeMember("ActiveDocument",BindingFlags.GetProperty, null, application, new object[] { });
object pageSetup = type.InvokeMember("PageSetup", BindingFlags.GetProperty, null, activeDocument, new object[] { });
type = pageSetup.GetType();
type.InvokeMember("PaperSize", BindingFlags.SetProperty, null, pageSetup, new object[] { 1 });//Letter 8 1/2 x 11 in --> Here i got the error explain below
type.InvokeMember("PrintOut", BindingFlags.InvokeMethod, null, activeDocument , new object[] { });

During setting the "PaperSize" i got the target invocation exception saying "Requested PaperSize is not available on the currently selected printer." But when i do the normal printing from ms word application it takes Letter 8 1/2 x 11 in and print the document. So how it will be possible from there and not from reflection? What i am missing? Help me to resolve this issue.





Aucun commentaire:

Enregistrer un commentaire