0

How can i print a Word document on both sides using code in Delphi xe 3?

Below is how the word document is created:

  try
  { Open the document}
   WrdApp := CreateOLEObject('Word.Application');
   wrdDoc := WrdApp.Documents.Open(document);
  // Wrddoc.SaveAs(SaveToFile);
  except
   on E: Exception do
    begin
     E.Message := 'Could Not Execute MS WORD!';
     raise;
    end;
  end;

Any help would be greatly appreciated thank you

user3271392
  • 149
  • 2
  • 12
  • ShellExecute won't get you anywhere there. You'll need to query the printer and do the printing in you program rather than shelling to another. How slick do you want this to be? Sounds like you aren't writing professional level software and don't mind if it is a bit rough around the edges. – David Heffernan Feb 23 '14 at 08:07
  • Why do you export it from Word to PDF to print it? You can print it directly from Word, and duplex it from there. – Ken White Feb 23 '14 at 08:10
  • @Davidheffernan Well, what do you mean by slick? I dont mind it being rough around the edges, but clients will be using it. so it does need to work properly... – user3271392 Feb 23 '14 at 08:10
  • @kenWhite How? I have tried for days to do it, with no avail. PDF printing is my last resort – user3271392 Feb 23 '14 at 08:11
  • If you are prepared to consider shelling to another app then it doesn't sound like you need it to be slick because that's not slick. There must be many good pdf libraries that handle printing. But if you are already automating word it seems crazy not to print from there. Although automating word doesn't sound very slick either. Why can't you just make a doc and print it from your code? – David Heffernan Feb 23 '14 at 08:14
  • @Davidheffernan Shelling is kind of my last resort. I cannot find a way to print it directly from Word in duplex. If it is possible, that would be ideal. – user3271392 Feb 23 '14 at 08:16
  • What do you mean print it from code? @DavidHeffernan – user3271392 Feb 23 '14 at 08:16
  • I can't answer that, because you haven't shown how you're creating (before exporting) the Word document to PDF. – Ken White Feb 23 '14 at 08:21
  • I mean print directly from your app so that you can control printer settings. Or at least let the user control them. Surely it should be up to the user whether or not to duplex. – David Heffernan Feb 23 '14 at 08:23
  • @Kenwhite I hope the code above answers your question. – user3271392 Feb 23 '14 at 08:26
  • @davidHeffernan the client have asked me to set it up, so that it always prints directly to the printed and always in the same format. They do not want to have to change settings everytime. – user3271392 Feb 23 '14 at 08:26
  • Your users don't need to specify duplex everytime. They just need to learn how to configure their printer driver to have duplex as default. It's always easier for users to ask developers to modify the program than it is for the user to learn how to use their computer. – David Heffernan Feb 23 '14 at 08:46
  • ok but @davidheffernan before we get way off topic, is it possible to print duplex either from pdf or directly from word? If it is, that would realy be ideal... They cant set the printer to print duplex as default, then all the other people who dont use my software will strougle – user3271392 Feb 23 '14 at 08:52
  • You can surely print duplex from Word. – David Heffernan Feb 23 '14 at 09:04
  • One option I use occasionally when I need to have specific printing options in a specific case is to install the printer twice - the second one will then be called (f.ex.) "Brother Printer - Duplex Printing" and then set that one as having duplex printing as default. You can then allow the user to select the printer they wish to use from your application and then they simply select the (non-Windows-default) printer "Brother Printer - Duplex Printing" in your application. Your application will then print duplex without intervention, and other programs (using Windows default printer) won't. – HeartWare Feb 23 '14 at 09:08
  • @Heartware I want to leave that as a last resort, because i do not want every computer using my software to be set up to print like that. preferably, i would like to manage everything from code. – user3271392 Feb 23 '14 at 09:13
  • @user3271392: and what if a user does not have a printer that supports duplex printing? – whosrdaddy Feb 23 '14 at 09:25
  • See also [`How to print a word document duplex using OLE autimation with delphi xe3`](http://stackoverflow.com/q/20948460/576719). – LU RD Feb 23 '14 at 09:27
  • @whosrdaddy The chances of that is slim to none, the documents that the application prints are specifically for duplex printing. – user3271392 Feb 23 '14 at 09:27
  • @LURD that does not have an answer – user3271392 Feb 23 '14 at 09:28
  • How to print from Word? Don't expect an answer to that given the question you asked about PDF. – David Heffernan Feb 23 '14 at 09:55
  • @DavidHeffernan The question right for you know? – user3271392 Feb 23 '14 at 14:07
  • Yes thank you, better now – David Heffernan Feb 23 '14 at 16:20
  • I think your best bet is to print to a file (there is a save to file setting in the PrintOut function), change the printer settings using the [DocumentProperties](http://msdn.microsoft.com/en-us/library/windows/desktop/dd183576%28v=vs.85%29.aspx) windows api call, as illustrated here - [How To Modify Printer Settings with the DocumentProperties() Function](http://support.microsoft.com/kb/167345), then when you have set the duplex mode, print the file by sending it directly to the printer port. – Anders E. Andersen Feb 23 '14 at 17:47

1 Answers1

1

KB 194306 demonstrates printing duplex using Word automation. The key is to insert a field, and then enter the printer-specific escape code to tell the printer to print in duplex mode. In Delphi, it might go something like this:

wrddoc.Selection.Fields.Add(wrddoc.Selection.Range, -1, 'PRINT 27 &l1S');

Then you can call Printout as normal to have Word print the document.

The article mentions that the escape code shown here is for HP printer drivers.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 2
    I'm hoping that something better is available in the 9 years that have elapsed since that article – David Heffernan Feb 23 '14 at 16:21
  • 1
    There is more "up to date" article here, [`HOWTO: Set Duplex Printing for Word Automation`](http://support.microsoft.com/kb/230743). Published January 24, 2007. – LU RD Feb 23 '14 at 19:05
  • @Davidheffernan i have decided to follow your advice and set up a printer, rather than trying it the way i wanted to at first. Doing it all from code is a nightmare! thank you for the help – user3271392 Feb 23 '14 at 21:47
  • The code @Lurd links to uses a VBA macro to set the duplex option on the printer driver *within Word*. You could add such a macro to your document, or to the template you use to create the document, and then call it from your Delphi code. You could inject the instructions into the Word VBA interpreter at run time, but that would be tedious to code. – Rob Kennedy Feb 23 '14 at 21:52