Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Command Line Printing a Word Document


  • Please log in to reply
15 replies to this topic
ehagood
  • Members
  • 51 posts
  • Last active: Nov 02 2010 03:38 PM
  • Joined: 30 Jan 2006
Does anyone know of a way to print a Word Document via a command line. I tried Run, Print /D:LPT1 FILE.doc and all I got was garbage.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Let see...
In the registry, HKCR\.doc points, on my system, to Word.Document.8
HKCR\Word.Document.8 has shell\Print\command with:
"C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" /x /n /dde
No parameter, that's suspect... It looks like we need DDE to provide the filename (I don't know how to do it...).
From the official list of command line options, it seems they don't have made it simple. It is probably used only by shell's context menu.
I found a workaround:
"C:\Program Files\Microsoft Office\Office\winword.exe" "C:\My Documents\doc1.doc" /mFilePrintDefault
Of course, adjust the path (and doc. name!) as needed.
You can probably make your own macro to exit after the operation.

BoBo
  • Guests
  • Last active:
  • Joined: --
[Print Word Documents Automatically] - Might be not interesting anymore, but I think/assume the descriped CMD/CLI tool could be used to send DDE statements in general. Check it out. 8)

ehagood
  • Members
  • 51 posts
  • Last active: Nov 02 2010 03:38 PM
  • Joined: 30 Jan 2006
Thanks for the advice PhilHo and the link Bobo, I will check it out. As it stands, I have Word opening a document (with merge fields), printing, then closing. The only problem is that some of the documents are 20 pages longer than others so, when it closes, if the entire document hasn't been sent to the printer it prompts to stay open til it does. I have a loop catch it and keep sending Enter for the prompt and an ALT+F4 command, which breaks when the window no longer exists.

Seems messy and occasionally I get a stray ALT+F4 command outside Word. Plus, it would be more aesthetically pleasing if Word didn't open at all.

BoBo
  • Guests
  • Last active:
  • Joined: --
PrintDirect Dynamic Link Library
================================

1. Installation
   Extract the self executable archive to an empty directory.
   Move PRNTDRCT.DLL to the Windows/System (Winnt/System32 on 'NT)
   Using the DLL with the compiled executable the DLL can also be
   placed to the application directory.
   Read this file.
   Load the sample DefTest.vbp VB 5.0 application or edit Deftest.frm with Notepad.
   
2. Distribution
   Only the PRNTDRCT.DLL must be distributed with your applications and installed
   as above.

3. Functions (using VB/VBA declarations)

Declare Function PrintFile Lib "Prntdrct.dll" (ByVal DeviceName As String, ByVal FileToPrint As String) As Long
   Purpose: to print FileToPrint file on DeviceName printer bypassing the printer driver
   Return value: zero upon success, none-zero values are error constants in the WIN32API (ERROR_xxx)
   Restraints: printer must be spooled, raw datatype, no fax driver
   
Declare Function AvailablePrinters Lib "Prntdrct.dll" () As Integer
   Purpose: returns the number of available printers
   Return value: The number of printers available
   
Declare Function PrinterDeviceName Lib "Prntdrct.dll" (index As Integer) As String
   Purpose: retrieving the exact name of a printer
   Parameters: index is an integer between zero and the number of available printers minus one 
   Return value: the name of the printer
   
Declare Function DefaultPrinter Lib "Prntdrct.dll" () As String
   Purpose: retrieving the name of the default printer
   Return value: the name of the default printer

Declare Sub SetDefaultPrinter Lib "Prntdrct.dll" (ByVal pDeviceName As String)
   Purpose: setting the system default printer
   Parameters: pDeviceName is the device name of the printer to be set as default
   Note: the change of the default printer might not be shown on the Printers panel
         but the change will be made
  
Declare Function IsWindowsNT Lib "Prntdrct.dll" () As Integer
   Purpose: retrieving information of the operation system
   Return value: zero for Windows'95 or '98
                 -1 for 'NT

Declare Sub Linefeed Lib "Prntdrct.dll" (ByVal lfeed As String)
   Purpose: setting ESC sequence that will automatically being inserted
            after LPrint
   Parameters: lfeed normally Chr$(13) + Chr$(10), the default is an empty
               string

Declare Sub Formfeed Lib "Prntdrct.dll" (ByVal ffeed As String)
   Purpose: setting ESC sequence that will automatically being inserted
            at the end of the document when LPrintEnd called
   Parameters: ffeed normally Chr$(12), the default is an empty
               string

Declare Sub LPrint Lib "Prntdrct.dll" (ByVal txt As String)
   Purpose: allowing writing directly to the printer line by line as was in old
            DOS Basic
   Parameters: txt can be any string value that will be printed as one line

Declare Function LPrintEnd Lib "Prntdrct.dll" (ByVal DeviceName As String) As Long
    Purpose: finishing editing and starting the printing on the printer passed as
             DeviceName. When DeviceName is not found the printing fails.              
    Return value: zero upon success, none-zero values are error constants in the WIN32API (ERROR_xxx)

Declare Sub License Lib "Prntdrct.dll" (Password As Long)
   Purpose: passing license information
            available upon registration 

Declare Sub Orientation Lib "Prntdrct.dll" (newOrientation As Integer, oldOrientation As Integer, ByVal escLandscape As String, ByVal escPortrait As String)
   Purpose: changing the orientation of the printer
   Note:    normally the printed document has Escape sequences that includes the orientation
            use this function to provide it when it is missing from the document
            The function allows of passing any string as formatting in escLandscape and in
            escPortrait. You can combine formatting for compressed printing, font that will
            effect the whole document.
            Always use escPortrait to reinstate the original settings.
   Parameters: newOrientation, pass 0 for Portrait and none zero for Landscape
               oldOrientation returns the former setting, same as newOrientation
               escLandscape, the escape sequence for the printer for Lanscape orientation
                             CHR$(27) +  CHR$(38) + CHR$(108) + CHR$(49) + CHR$(79) will
                             be used as default when an empty string passed
               escPortrait, the escape sequence for the printer for Portrait orientation
                             CHR$(27) +  CHR$(38) + CHR$(108) + CHR$(48) + CHR$(79) will
                             be used as default when an empty string passed

Declare Sub SetAutoformfeed Lib "Prntdrct.dll" (setting As Integer)
   Purpose: issuing a formfeed automatically when the document does not have it at the end
   Parameters: setting - 0 = Off
                         None zero = On   

Declare FUNCTION CreateShortcut (ByVal hWnd AS LONG, ByVal pszPath AS String, ByVal pszName AS String, ByVal lWhere AS LONG) AS LONG
   Purpose: creating a Windows shortcut (.lnk) file (this is a bonus 
            function for registered users
   Parameters: hWnd    - the hWnd value of the calling window, or zero
               pszPath - the complete path to the file that the 
                         shortlink will refer to
               pszName - The name that will appear on the shortlink
               lWhere  - a value that tells where the shortlink will be 
                         created, as follows:
                         &H10 - on the desktop
                         &H6  - in favorites
                         &H5  - in personal folder
                         &H2  - in Start Menu programs folder
                         &H8  - in recent documents list
                         &H9  - in Send To menu
                         &HB  - on top of the Start Menu
                         &H7  - in Startup group 
Special note: Using the DLL from PowerBasic might require aliasing.
              
Declare Function PrintBMP Lib "Prntdrct.dll" (ByVal DeviceName As String, ByVal FileToPrint As String, ByVal iLeft As Double, ByVal iTop As Double, ByVal iWidth As Double, ByVal iHeight As Double) As Long
   Purpose: to print a bitmap file on DeviceName printer using the printer driver
            in the positions and size passed with the iLeft, iTop, iWidth and iHeight parameters 
   Return value: zero upon success, none-zero values are error constants in the WIN32API (ERROR_xxx)
   Restraints: file must be a valid BMP file with .bmp file extension
Shareware/116634Kb/2004-06-15 --- [Download]

James
  • Guests
  • Last active:
  • Joined: --
I found a site with a list of Word command line switches
Word Startup Command Line Switches
http://winhlp.com/Wo...ineSwitches.htm


The /m switch allows you to run a macro. In Word macros include the list of 'word commands' (Toos | Macros | Macros | Macros in: Word Commands) with things such as FilePrintDefault (already mentioned) and FileExit.

The /m switch can be used multiple times on the same command line.

The stumbling block, as already pointed out, is that printing runs in the background so you don't know when it is finished. However, if you go to Tools | Options | Print you can disable this by unchecking the box next to "Background printing".

Once you have disabled background printing, you can then use the print and exit commands as follows:

"C:\Program Files\Microsoft Office\Office\WINWORD.EXE" test.rtf /q /n /mFilePrintDefault /mFileExit

The /q option tells Word not to display the splash screen and /n tells it not to load a new default document.

BoBo
  • Guests
  • Last active:
  • Joined: --
Thanks for sharing this! Much appreciated. 8)

Navmiester
  • Guests
  • Last active:
  • Joined: --
Is is possible to print a document in Microsoft Word Viewer via command line or VB script?

quatermass
  • Members
  • 220 posts
  • Last active: Dec 16 2013 09:00 PM
  • Joined: 14 Dec 2005

Is is possible to print a document in Microsoft Word Viewer via command line or VB script?



I'd like to do this too. I've a 2003 Server which I don't want to waste a license installing Office on when its already got Word Viewer 2003.


After searching Google I found a free cmd line app called Classexec which allows Word to open a file, print it and then quit again.

<!-- m -->http://fvue.nl/wiki/ClassExec<!-- m -->

I've set up PDFCreater as the default printer to auto-save and this classExec does the business on my XP PC. ClassExec says Wordview can't be a made to print via a cmd line as this function was removed.
Nice one Microsoft!

So looks like I've got to use a real copy of Word, I'd try OO but the resultant .docs it generates never look the same as Word 2003 does.
Which is kind of the whole point of this excerise. :roll:

I can't macro control Wordview with AHK as I won't be logged in when the script runs.

Any suggestions are welcome.

totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
Hi guys.

Thanks for posting this, saved me aeons of time :)

Was wondering if there was a way of defining how many to print?

totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
Nobody knows?

totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
Still have not find a solution to this.. any ideas? :)

quatermass
  • Members
  • 220 posts
  • Last active: Dec 16 2013 09:00 PM
  • Joined: 14 Dec 2005
Do the routine X times? :lol:

Or set up an extra Printer, preset to do 2 or 4 copies, etc. and call that Printer up? You can do that in PDFCreator Printing properties.


I've had to end up using the Open Office method as I can't justify spending money on using a whole MS license just to print a few odd pages.

OK, the end result means more pages. But then the customer never saw the original Word .doc so they'll not notice the extra pages. :lol:


If you really want to use Word to print I guess you need to make a Word Macro and then run that macro from the command line.

This took me 5 minutes to work out.... :-)


Sub printtemp()
'
' printtemp Macro
' Macro recorded 15/10/2008 by Stuart Halliday
'
    ActivePrinter = "PDFCreator"
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=5, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=False, Collate:=False, Background:=True, PrintToFile:= _
        False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
End Sub

From http://support.microsoft.com/kb/210565
I got this :

Winword.exe /m<macroname> ?
Like \OFFICE11\Winword.exe /mprinttemp


I take it you can work out how to get it to print X copies from this?
:p

NotYet
  • Guests
  • Last active:
  • Joined: --
Hi!

I'm also looking for a good command line, to convert anything to PDF. (with PDFCreator)

This is a sample C++ code, but the command line are visible:

WinExec("C:\\Program Files\\Irfanview\\i_view32.exe \"C:\\tiftemp\\test1.tif\" /print=PDFCreator /ini=G:\\install\\AstrA\\Gepard\\Tiff2Pdf", SW_SHOWNORMAL);
WinExec("G:\\Tiff2Pdf\\wordpad\\wordpad.exe /pt \"C:\\tiftemp\\test2.rtf\" PDFCreator", SW_SHOWNORMAL);
WinExec("C:\\Program Files\\Irfanview\\i_view32.exe \"C:\\tiftemp\\test3.JPG\" /print=PDFCreator /ini=G:\\install\\AstrA\\Gepard\\Tiff2Pdf", SW_SHOWNORMAL);
WinExec("G:\\Tiff2Pdf\\wordpad\\wordpad.exe /pt \"C:\\tiftemp\\test4.doc\" PDFCreator", SW_SHOWNORMAL);
WinExec("G:\\Tiff2Pdf\\wordpad\\wordpad.exe /pt \"C:\\tiftemp\\test5.txt\" PDFCreator", SW_SHOWNORMAL);

CString m_sTemp;
m_sTemp+= "\"C:\\tiftemp\\__test1.pdf\" ";
m_sTemp+= "\"C:\\tiftemp\\test2.pdf\" ";
m_sTemp+= "\"C:\\tiftemp\\__test3.pdf\" ";
m_sTemp+= "\"C:\\tiftemp\\test4.pdf\" ";
m_sTemp+= "\"C:\\tiftemp\\test5.pdf\" ";
m_sTemp+= "cat output \"C:\\tiftemp\\joined.pdf\"";
WinExec("G:\\install\\AstrA\\Gepard\\Tiff2Pdf\\pdftk.exe "+ m_sTemp, SW_SHOWNORMAL);

Last line is a packing, all the converted PDFs into one.

I've used wordpad, cos' it is more easier to parameterize, than winword, and the location is fix.

I hope it helps.

Good work

Attila

  • Guests
  • Last active:
  • Joined: --
Thanks