Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

I have added both SENDKEYS and Direct VB, using SENDKEYS where there is no alternative. Here is the Hybrid Hybrid Script (tongue). Note: The idea behind "DUPLEXSENDKEYS" is to send it when not using a double sided default printer. If you have a single sided printer and wish to use "DUPLEXFLIPON" you must also set DUPLEXSENDKEYS to true, but if you have a double sided default printer, then you can JUST set "DUPLEXFLIPON=Short" to adjust default (assumption is default is "long").

<!-- : Begin batch script
@ECHO OFF
CLS

cscript //nologo "%~f0?.wsf" "FILEPATH=C:\tmp\test.docx" "PAGERANGE=1-3" "COPIES=1" "ORIENTATION=Portrait" "TWOPAGESONONE=False" "DUPLEXSENDKEYS=TRUe" "DUPLEXFLIPON=Short" //job:WORD
exit /b
PAUSE
----- Begin wsf script --->
<package>
  <job id="WORD">
    <script language="VBScript">
        Dim ObjArgs
        Set ObjArgs = wscript.arguments

        Dim objShell
        Set objShell = WScript.CreateObject ("WScript.shell")  

        Dim DUPLEXSENDKEYS
        DUPLEXSENDKEYS= Null 
        Dim DUPLEXFLIPON
        DUPLEXFLIPON = Null
        Dim PAGERANGE          
        PAGERANGE = Null
        Dim COPIES
        COPIES = Null
        Dim ORIENTATION
        ORIENTATION = Null
        Dim TWOPAGESONONE
        TWOPAGESONONE = False

        Dim objDictionary
        Set objDictionary = CreateObject("Scripting.Dictionary")
        For I = 0 to objArgs.Count-1
        objDictionary.CompareMode = vbTextCompare
        objDictionary.Add Split(objArgs(I), "=")(0), Split(objArgs(I), "=")(1)
        Next

        'Open Document
        Dim ObjWord
        Set ObjWord = CreateObject("Word.Application")
        Dim ObjDoc
        'https://docs.microsoft.com/en-us/office/vba/api/word.documents.open
        '.Open (FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
        DUPLEXSENDKEYS = StrComp(ObjDictionary("DUPLEXSENDKEYS"), "TRUE", 1)
        DUPLEXFLIPON = StrComp(ObjDictionary("DUPLEXFLIPON"), "SHORT", 1)
        If DUPLEXSENDKEYS = 0 or DUPLEXFLIPON = 0 Then
         ObjWord.Visible = True
         Set ObjDoc = ObjWord.Documents.Open(objDictionary("FILEPATH") , ,TRUE)
        ELSE  
         Set ObjDoc = ObjWord.Documents.Open(objDictionary("FILEPATH"), ,TRUE, , , , , , , , ,FALSE)    
        End If
        
        'Orientation
        'https://docs.microsoft.com/en-us/office/vba/api/word.pagesetup.orientation
        ORIENTATION = StrComp(objDictionary("ORIENTATION"), "LANDSCAPE", 1)
        If ORIENTATION = 0 Then
         ObjDoc.PageSetup.Orientation = 1
        End If
        ORIENTATION = StrComp(objDictionary("ORIENTATION"), "Portrait", 1)
        If ORIENTATION = 0 Then
         ObjDoc.PageSetup.Orientation = 0          
        End If 
        
        'TWOPAGESONONE
        'https://docs.microsoft.com/en-us/office/vba/api/word.pagesetup.orientation
        TWOPAGESONONE = StrComp(objDictionary("TWOPAGESONONE"), "TRUE", 1)
        If TWOPAGESONONE = 0 Then
         ObjDoc.PageSetup.TwoPagesOnOne = True
        End If

        WScript.Sleep 1000         
        ObjShell.AppActivate "Word"
        WScript.Sleep 1000   
        If DUPLEXSENDKEYS = 0 Then 
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "^p", TRUE ' CTRL + P [Print Screen]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "%p", TRUE ' Alt + P [Print Options]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "%d", TRUE ' Alt + d [Print Duplex Options]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "{DOWN}", TRUE ' Down [Print Duplex Default Long]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "{ENTER}", TRUE
        End If
        If DUPLEXFLIPON = 0 Then
          WScript.Sleep 500       
          ObjShell.SENDKEYS "^p", TRUE ' CTRL + P [Print Screen]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "%p", TRUE ' Alt + P [Print Options]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "%d", TRUE ' Alt + d [Print Duplex Options]
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "{DOWN}", TRUE ' Down [Print Duplex Short] 
          WScript.Sleep 1000       
          ObjShell.SENDKEYS "{ENTER}", TRUE
        End If
        
        'Copies & PageRange
        ObjDoc.Activate
        'https://docs.microsoft.com/en-us/office/vba/api/word.application.printout
        'https://documentation.help/MS-Office-Word-VB/womthPrintOut1.htm
        'https://docs.microsoft.com/en-us/office/vba/api/word.wdprintoutrange
        'https://stackoverflow.com/questions/63442594/pages-printout-word-vbs/63442654#63442654
        '.PrintOut (Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)  
        If Not objDictionary("COPIES") = "" and Not objDictionary("PAGERANGE") = "" THEN
          Call ObjDoc.PrintOut(FALSE, ,"4", , , , ,objDictionary("COPIES"),objDictionary("PAGERANGE"), , ,TRUE)   
        ELSEIF Not objDictionary("PAGERANGE") = "" Then
          Call ObjDoc.PrintOut(FALSE, ,"4", , , , , ,objDictionary("PAGERANGE"), , ,TRUE)   
        ELSEIF Not objDictionary("COPIES") = "" Then
          Call ObjDoc.PrintOut(FALSE, , , , , , ,objDictionary("COPIES"), , , ,TRUE)  
        ELSE
          Call ObjDoc.PrintOut(FALSE, , , , , , , , , , ,TRUE)
        End If
              
        objDoc.Saved = TRUE
        objWord.Quit
     
        Dim FPath
        TEMP=ObjShell.ExpandEnvironmentStrings("%TEMP%")
        FPath = TEMP & "\jobrunning.log"

        Dim ObjFS
        Set ObjFS = CreateObject("Scripting.FileSystemObject")

        IF ObjFS.FileExists(FPath) THEN
            ObjFS.DeleteFile FPath
        END IF

        Set ObjShell = Nothing
        Set FPath = Nothing
        Set ObjFS = Nothing
        Set ObjDoc = Nothing
        Set ObjWord = Nothing
        Set ObjArgs = Nothing
        Set objDictionary = Nothing
        Set ObjShell = Nothing
    </script>
  </job>
</package>


I'm leaving below using SENDKEYS, but there is no need w/ Office Products it can be directly printed via VB and that is preferred.

Use VB:

<!-- : Begin batch script
@ECHO OFF
CLS

cscript //nologo "%~f0?.wsf" "FILEPATH=C:\tmp\test.docx" "PAGERANGE=" "COPIES=" "ORIENTATION=Landscape" "TWOPAGESONONE=True" //job:WORD
exit /b
PAUSE
----- Begin wsf script --->
<package>
  <job id="WORD">
    <script language="VBScript">
        Dim ObjArgs
        Set ObjArgs = wscript.arguments

        Dim PAGERANGE          
        PAGERANGE = Null
        Dim COPIES
        COPIES = Null
        Dim ORIENTATION
        ORIENTATION = Null
        Dim TWOPAGESONONE
        TWOPAGESONONE = False

        Dim objDictionary
        Set objDictionary = CreateObject("Scripting.Dictionary")
        For I = 0 to objArgs.Count-1
        objDictionary.CompareMode = vbTextCompare
        objDictionary.Add Split(objArgs(I), "=")(0), Split(objArgs(I), "=")(1)
        Next
       
        Dim ObjWord
        Set ObjWord = CreateObject("Word.Application")
        ObjWord.Visible = True
        
        'Open Document
        Dim ObjDoc
        'https://docs.microsoft.com/en-us/office/vba/api/word.documents.open
        '.Open (FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
        Set ObjDoc = ObjWord.Documents.Open(objDictionary("FILEPATH"), ,TRUE, , , , , , , , ,TRUE)
        'Set ObjDoc = ObjWord.Documents.Open(objDictionary("FILEPATH"), ,TRUE, , , , , , , , ,FALSE)    

        'Orientation
        'https://docs.microsoft.com/en-us/office/vba/api/word.pagesetup.orientation
        ORIENTATION = StrComp(objDictionary("ORIENTATION"), "LANDSCAPE", 1)
        If ORIENTATION = 0 Then
         ObjDoc.PageSetup.Orientation = 1
        End If
        ORIENTATION = StrComp(objDictionary("ORIENTATION"), "Portrait", 1)
        If ORIENTATION = 0 Then
         ObjDoc.PageSetup.Orientation = 0          
        End If 
        
        'TWOPAGESONONE
        'https://docs.microsoft.com/en-us/office/vba/api/word.pagesetup.orientation
        TWOPAGESONONE = StrComp(objDictionary("TWOPAGESONONE"), "TRUE", 1)
        If TWOPAGESONONE = 0 Then
         ObjDoc.PageSetup.TwoPagesOnOne = True
        End If
        
        'Copies & PageRange
        'https://docs.microsoft.com/en-us/office/vba/api/word.application.printout
        'https://documentation.help/MS-Office-Word-VB/womthPrintOut1.htm
        'https://docs.microsoft.com/en-us/office/vba/api/word.wdprintoutrange
        'https://stackoverflow.com/questions/63442594/pages-printout-word-vbs/63442654#63442654
        '.PrintOut (Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)  
        Dim ObjPrint
        If Not objDictionary("COPIES") = "" and Not objDictionary("PAGERANGE") = "" THEN
         ObjPrint = ObjDoc.PrintOut(FALSE, ,"4", , , , ,objDictionary("COPIES"),objDictionary("PAGERANGE"), , ,TRUE)   
        ELSEIF Not objDictionary("PAGERANGE") = "" Then
         ObjPrint = ObjDoc.PrintOut(FALSE, ,"4", , , , , ,objDictionary("PAGERANGE"), , ,TRUE)   
        ELSEIF Not objDictionary("COPIES") = "" Then
         ObjPrint = ObjDoc.PrintOut(FALSE, , , , , , ,objDictionary("COPIES"), , , ,TRUE)  
        ELSE
         ObjPrint = ObjDoc.PrintOut(FALSE, , , , , , , , , , ,TRUE)
        End If
              
        objDoc.Saved = TRUE
        objWord.Quit

        Dim objShell
        Set objShell = WScript.CreateObject ("WScript.shell")        
        Dim FPath
        TEMP=ObjShell.ExpandEnvironmentStrings("%TEMP%")
        FPath = TEMP & "\jobrunning.log"

        Dim ObjFS
        Set ObjFS = CreateObject("Scripting.FileSystemObject")

        IF ObjFS.FileExists(FPath) THEN
            ObjFS.DeleteFile FPath
        END IF

        Set ObjShell = Nothing
        Set FPath = Nothing
        Set ObjFS = Nothing
        Set ObjDoc = Nothing
        Set ObjWord = Nothing
        Set ObjArgs = Nothing
        Set objDictionary = Nothing
        Set ObjShell = Nothing
    </script>
  </job>
</package>

Via VBScript/Batch Hybrid to manipulate Print Options Only configured for "pages", but you can easily adapt. But, SENDKEYS is notoriously not a great approach. I generally have a check outside both scripts to see if any argument was passed and use either WINWORD.EXE or Hybrid if needed.

<!-- : Begin batch script
@ECHO OFF
CLS

cscript //nologo "%~f0?.wsf" "PROGPATH=C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE" "FILEPATH=C:\tmp\test.docx" "DUPLEXSENDKEYS=TrUe" "PAGERANGE=" "COPIES=" "ORIENTATION=" "PAGEPERSHEET=2" //job:WORD
exit /b
PAUSE
----- Begin wsf script --->
<package>
  <job id="WORD">
    <script language="VBScript">
        Dim ObjArgs
        Set ObjArgs = wscript.arguments

        Dim DUPLEXSENDKEYS
        DUPLEXSENDKEYS = Null 
        Dim PAGERANGE          
        PAGERANGE = Null
        Dim COPIES
        COPIES = Null
        Dim ORIENTATION
        ORIENTATION = Null
        Dim PAGEPERSHEET
        PAGEPERSHEET = Null

        Dim objDictionary
        Set objDictionary = CreateObject("Scripting.Dictionary")
        For I = 0 to objArgs.Count-1
        objDictionary.CompareMode = vbTextCompare
        objDictionary.Add Split(objArgs(I), "=")(0), Split(objArgs(I), "=")(1)
        Next

        Dim objShell
        Set objShell = WScript.CreateObject ("WScript.shell")
        cmd = objDictionary("PROGPATH") & " /q /n /mfileprint " & objDictionary("FILEPATH")
        objshell.exec(cmd)
        WScript.Sleep 5000

        objShell.AppActivate "Print"
        objShell.SendKeys "{TAB}", TRUE ' More "Bring Focus ...SendKeys = Hacky
        WScript.Sleep 500
        objShell.SendKeys "%p", TRUE ' Alt + g [Properties]
        WScript.Sleep 1000         
        objShell.SendKeys "%b", TRUE ' Alt + b [Print On Both Sides]
        WScript.Sleep 500
        objShell.SendKeys "{DOWN}", TRUE ' Open Menu
        WScript.Sleep 500
        objShell.SendKeys "{UP}{UP}{UP}", TRUE ' Select NONE
        WScript.Sleep 500
        DUPLEXSENDKEYS = StrComp(objDictionary("DUPLEXSENDKEYS"), "TRUE", 1)
        If DUPLEXSENDKEYS = 0 Then
          objShell.SendKeys "{DOWN}", TRUE ' Select Flip On Long Edge
          WScript.Sleep 1000               
        End If
        WScript.Sleep 1000    
        objShell.SendKeys "{ENTER}", TRUE '     
        objShell.SendKeys "%o", TRUE ' Alt + o [Orientation]
        objShell.SendKeys "{DOWN}", TRUE ' Open Menu
        objShell.SendKeys "{Up}", TRUE ' Default Portrait
        ORIENTATION = StrComp(objDictionary("ORIENTATION"), "LANDSCAPE", 1)
        If ORIENTATION = 0 Then
           objShell.SendKeys "{DOWN}", TRUE ' Landscape ORIENTATION
        End If      
        objShell.SendKeys "{ENTER}{ENTER}", TRUE ' Back To Print Screen
        WScript.Sleep 1000

        If Not objDictionary("PAGERANGE") = "" Then
         objShell.AppActivate "Print"
         objShell.SendKeys "{TAB}", TRUE ' More "Bring Focus ...SendKeys = Hacky
         WScript.Sleep 1000
         objShell.SendKeys "%g", TRUE ' Alt + g [Pages]
         objShell.SendKeys objDictionary("PAGERANGE"), TRUE ' Start and End Page
         WScript.Sleep 500
        End If

        If Not objDictionary("COPIES") = "" Then
         objShell.AppActivate "Print"
         objShell.SendKeys "{TAB}", TRUE ' More "Bring Focus ...SendKeys = Hacky
         WScript.Sleep 1000
         objShell.SendKeys "%c", TRUE ' Alt + g [COPIES]
         objShell.SendKeys objDictionary("COPIES"), TRUE ' Start and End Page
         WScript.Sleep 500
        End If

        If Not objDictionary("PAGEPERSHEET") = "" Then
         objShell.AppActivate "Print"
         objShell.SendKeys "{TAB}", TRUE ' More "Bring Focus ...SendKeys = Hacky
         WScript.Sleep 1000
         objShell.SendKeys "%h", TRUE ' Alt + h [Pages/Sheet]
         If objDictionary("PAGEPERSHEET") = "1" THEN
          objShell.SendKeys "{ENTER}", TRUE ' Default, just hit "ENTER" to Select
         End If
         If objDictionary("PAGEPERSHEET") = "2" THEN
          objShell.SendKeys "{DOWN}", TRUE ' Move Down Menu
          objShell.SendKeys "{ENTER}", TRUE ' Make Selection
         End If
         If objDictionary("PAGEPERSHEET") = "4" THEN
          objShell.SendKeys "{DOWN}{DOWN}", TRUE ' Move Down Menu
          objShell.SendKeys "{ENTER}", TRUE ' Make Selection
         End If
         If objDictionary("PAGEPERSHEET") = "6" THEN
          objShell.SendKeys "{DOWN}{DOWN}{DOWN}", TRUE ' Move Down Menu
          objShell.SendKeys "{ENTER}", TRUE ' Make Selection
         End If
         If objDictionary("PAGEPERSHEET") = "8" THEN
          objShell.SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}", TRUE ' Move Down Menu
          objShell.SendKeys "{ENTER}", TRUE ' Make Selection
         End If
         If objDictionary("PAGEPERSHEET") = "16" THEN
          objShell.SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}", TRUE ' Move Down Menu
          objShell.SendKeys "{ENTER}", TRUE ' Make Selection
         End If
         WScript.Sleep 500
        End If
                 
        'Done Setup
        WScript.Sleep 500
        objShell.SendKeys "{ENTER}", TRUE ' Print!
        WScript.Sleep 2000
        objShell.SendKeys "%{f4}", TRUE ' Alt + F4 = Exit Word
        WScript.Sleep 2000

        Dim FPath
        TEMP=ObjShell.ExpandEnvironmentStrings("%TEMP%")
        FPath = TEMP & "\jobrunning.log"
        Dim ObjFS
        Set ObjFS = CreateObject("Scripting.FileSystemObject")
        IF ObjFS.FileExists(FPath) THEN
            ObjFS.DeleteFile FPath
        END IF

        Set FPath = Nothing
        Set ObjFS = Nothing
        Set ObjArgs = Nothing
        Set objDictionary = Nothing
        Set ObjShell = Nothing
    </script>
  </job>
</package>

Via WORD.EXE

The way it seems to work is you have to print to the default printer, so what I created where CMD's which set the default printer to desired printer, printed document (as argument) and set back default (if it needed changing).

Eg: My deafult printer prints 'Single Sided" but the best/only way to script double sided is to setup a separate printer w/ double sided as default, so this is my CMD to print a doc double sided

@ECHO OFF
CLS

call "Double_Sided.cmd"

::ECHO "C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE" /q /n /mfileprintdefault /mFileCloseOrExit "%~1"
::PAUSE
"C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE" /q /n /mfileprintdefault /mFileCloseOrExit "%~1"
TIMEOUT 5

call "Single_Sided.cmd" 

Setting Default Printer:

@ECHO OFF
CLS

set PRINTUI=RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n 
set SERVER=
set SHARE=

echo "Setting Default Printer to HP LaserJet Document Printer Double-Sided"
%PRINTUI% "\\%SERVER%\%SHARE%"

Calling:

SET WORD_DS_P=call "Print_Word_Double_Sided.cmd"
SET PWD=\\SERVER\SHARE\Folder Space\
SET F1="%PWD%HR Package.docx"
%WORD_DS_P% %F1%

WINWORD.EXE Switchs:

Switch and parameter

Description

/safe

Starts Word in Safe Mode.

/q

Starts Word without displaying the Word splash screen.

/ttemplatename

Starts Word with a new document based on a template other than the Normal template.

Example    To start Word with a document based on a template called Myfax.dotx, stored on the C drive, type the following at the command prompt:

/tc:\Myfax.dotx

Note: Do not include a space between the switch and the name of the template file.

Security Note: Because templates can store macro viruses, be careful about opening them or creating files based on new templates. Take the following precautions: run up-to-date antivirus software on your computer, set your macro security level to high, clear the Trust all installed add-ins and templates check box, use digital signatures, and maintain a list of trusted sources.

/t filename

Starts Word and opens an existing file.

Example    To start Word and open the template file Myfax.dotx, stored on the C drive, type the following at the command prompt:

/t c:\Myfax.dotx

Example    To start Word and open multiple files, such as MyFile.docx and MyFile2.docx, each stored on the C drive, type the following at the command prompt:

/t c:\MyFile.docx c:\MyFile2.docx

/f filename

Starts Word with a new document based on an existing file.

Example    To start Word and create a new document based on file MyFile.docx, stored on the desktop, type the following at the command prompt:

/f "c:\Documents and Settings\All Users\Desktop\MyFile.docx"

/h http://filename

Starts Word and opens a read-only copy of a document that is stored on a Microsoft Windows SharePoint Services site. The site must be on a computer that is running Word 2007 or later or Windows SharePoint Services 2.0 or later.

Example    To start Word and open a copy of file MyFile.docx, stored in a document library at the URL http://MySite/Documents, type the following at the command prompt:

/h http://MySite/Documents/MyFile.docx

Note: If the document is checked out to you, the /h switch has no effect. Word opens the file so that you can edit it.

/pxslt

Starts Word and opens an existing XML document based on the specified Extensible Stylesheet Language Transformation (XSLT).

Example    To start Word and apply the XSLT MyTransform, stored on the C drive, to the XML file Data.xml, also stored on the C drive, type the following at the command prompt:

/pc:\MyTransform.xsl c:\Data.xml

/a

Starts Word and prevents add-ins and global templates (including the Normal template) from being loaded automatically. The /a switch also locks the setting files.

/ladd-in

Starts Word and then loads a specific Word add-in.

Example    To start Word and then load the add-in Sales.dll, stored on the C drive, type the following at the command prompt:

/lc:\Sales.dll

Note: Do not include a space between the switch and the add-in name.

Security Note: Use caution when running executable files or code in macros or applications. Executable files or code can be used to carry out actions that might compromise the security of your computer and data.

/m

Starts Word without running any AutoExec macros.

/mmacroname

Starts Word and then runs a specific macro. The /m switch also prevents Word from running any AutoExec macros.

Example    To start Word and then run the macro Salelead, type the following at the command prompt:

/mSalelead

Note: Do not include a space between the switch and the macro name.

Because macros can contain viruses, be careful about running them. Take the following precautions: run up-to-date antivirus software on your computer; set your macro security level to high; clear the Trust all installed add-ins and templates check box; use digital signatures; maintain a list of trusted publishers.

/n

Starts a new instance of Word with no document open. Documents opened in each instance of Word will not appear as choices in the Switch Windows list of other instances.

/w

Starts a new instance of Word with a blank document. Documents opened in each instance of Word will not appear as choices in the Switch Windows list of the other instances.

/r

Re-registers Word in the Windows registry. This switch starts Word, runs Office Setup, updates the Windows registry, and then closes.

/x

Starts Word from the operating system shell so that Word responds to only one Dynamic Data Exchange (DDE) request (for example, to print a document programmatically).

/ztemplatename

Visibly behaves exactly like the /t switch. However, you can use the /z switch with Word to generate both a Startup and a New event, whereas the /t switch generates only a Startup event.

  • No labels