@ECHO OFF :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Localize variables and enable delayed variable expansion SETLOCAL ENABLEDELAYEDEXPANSION :: Check command line arguments IF "%~1"=="" GOTO Syntax IF NOT "%~3"=="" GOTO Syntax ECHO.%* | FINDSTR /R /C:"[/?]" >NUL && GOTO Syntax IF NOT EXIST "%~1" ( ECHO. ECHO ERROR: File not found. ECHO. GOTO Syntax ) IF /I NOT "%~x1"==".doc" IF /I NOT "%~x1"==".docx" ( ECHO. ECHO ERROR: Invalid file type specified. ECHO. GOTO Syntax ) IF NOT "%~2"=="" ( SET Valid=0 START /WAIT REGEDIT /E "%Temp%.\~%~n0.tmp" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers" FOR /F "tokens=7 delims=\]" %%A IN ('TYPE "%Temp%.\~%~n0.tmp" ^| FINDSTR /R /B /I /C:"\[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Print\\Printers\\[^\\]*\]"') DO ( IF /I "%%~A"=="%~2" SET Valid=1 ) DEL "%Temp%.\~%~n0.tmp" IF !Valid! NEQ 1 ( ECHO. ECHO ERROR: Invalid printer specified. ECHO. GOTO Syntax ) ) :: Check if CMCDDE is available, prompt to download it if not CMCDDE 2>NUL | FIND /I "copyright" >NUL IF ERRORLEVEL 1 ( ECHO This batch file requires CMCDDE by Commence Corp. SET /P Answer=Do you want to download it now? [y/N] IF /I "!Answer!"=="Y" ( ECHO. ECHO Download CMCDDE.zip, unpack it to the current ECHO directory or any directory in the PATH and try again. START http://www.istri.fr/IMG/zip/CMCDDE.zip ECHO. ) ENDLOCAL GOTO:EOF ) :: Get the full (short) path to Microsoft Word Set Exec= FOR /F "tokens=1* delims==" %%A IN ('ASSOC .doc') DO SET DocType=%%B FOR /F "tokens=1* delims==" %%A IN ('FTYPE %DocType%') DO SET OpenCmd=%%B FOR %%A IN (%OpenCmd%) DO (IF NOT DEFINED Exec SET Exec=%%~sfA) ECHO.%Exec% | FIND /I "\winword.exe" >NUL IF ERRORLEVEL 1 ( ECHO. ECHO ERROR: .doc files aren't associated with Microsoft Word. ECHO. GOTO Syntax ) :: Open Microsoft Word START /MIN "Word" %Exec% /x /n /dde :: Wait 5 seconds, to allow Word to get started; modify the delay for slow computers PING 127.0.0.1 -n 6 >NUL :: Send the DDE commands to the opened Word session IF "%~2"=="" ( CMCDDE WinWord System "[REM _DDE_Minimize][FileOpen(\"%~1\")][t=IsDocumentDirty()][FilePrint 0][SetDocumentDirty t][DocClose]" ) ELSE ( CMCDDE WinWord System "[REM _DDE_Minimize][FileOpen(\"%~1\")][FilePrintSetup \"%~2 on p\",.DoNotSetAsSysDefault=1][FilePrint 0][DocClose 2][FilePrintSetup \"\"]" ) :: Done ENDLOCAL GOTO:EOF :Syntax ECHO. ECHO PrintDoc.bat, Version 1.00 for Windows 2000 and later ECHO Print a Word document using Microsoft Word ECHO. ECHO Usage: PRINTDOC document [ printer ] ECHO. ECHO Where: document is the Word document to be printed ECHO printer is the name of the printer (default printer if not specified) ECHO. ECHO Notes: This batch file requires Microsoft Word to be associated to .DOC files. ECHO This batch file also requires CMCDDE by Commence Corp. ECHO If CMCDDE is not available, you will be prompted to download it. ECHO This batch file opens Microsoft Word, but does not close it. ECHO The DDE commands used are valid for MS Office 2007, and were retrieved ECHO by GetDDE.vbs: http://www.robvanderwoude.com/wshexamples_g.html#GetDDE ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com IF "%OS%"=="Windows_NT" ENDLOCAL