Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printdoc.bat

(view source code of printdoc.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables and enable delayed variable expansion
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check command line arguments
  9. IF     "%~1"=="" GOTO Syntax
  10. IF NOT "%~3"=="" GOTO Syntax
  11. ECHO.%* | FINDSTR /R /C:"[/?]" >NUL && GOTO Syntax
  12.  
  13. IF NOT EXIST "%~1" (
  14. 	ECHO.
  15. 	ECHO ERROR: File not found.
  16. 	ECHO.
  17. 	GOTO Syntax
  18. )
  19.  
  20. IF /I NOT "%~x1"==".doc" IF /I NOT "%~x1"==".docx" (
  21. 	ECHO.
  22. 	ECHO ERROR: Invalid file type specified.
  23. 	ECHO.
  24. 	GOTO Syntax
  25. )
  26.  
  27. IF NOT "%~2"=="" (
  28. 	SET Valid=0
  29. 	START /WAIT REGEDIT /E "%Temp%.\~%~n0.tmp" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers"
  30. 	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 (
  31. 		IF /I "%%~A"=="%~2" SET Valid=1
  32. 	)
  33. 	DEL "%Temp%.\~%~n0.tmp"
  34. 	IF !Valid! NEQ 1 (
  35. 		ECHO.
  36. 		ECHO ERROR: Invalid printer specified.
  37. 		ECHO.
  38. 		GOTO Syntax
  39. 	)
  40. )
  41.  
  42. :: Check if CMCDDE is available, prompt to download it if not
  43. CMCDDE 2>NUL | FIND /I "copyright" >NUL
  44. IF ERRORLEVEL 1 (
  45. 	ECHO This batch file requires CMCDDE by Commence Corp.
  46. 	SET /P Answer=Do you want to download it now? [y/N] 
  47. 	IF /I "!Answer!"=="Y" (
  48. 		ECHO.
  49. 		ECHO Download CMCDDE.zip, unpack it to the current
  50. 		ECHO directory or any directory in the PATH and try again.
  51. 		START http://www.istri.fr/IMG/zip/CMCDDE.zip
  52. 		ECHO.
  53. 	)
  54. 	ENDLOCAL
  55. 	GOTO:EOF
  56. )
  57.  
  58. :: Get the full (short) path to Microsoft Word
  59. Set Exec=
  60. FOR /F "tokens=1* delims==" %%A IN ('ASSOC .doc') DO SET DocType=%%B
  61. FOR /F "tokens=1* delims==" %%A IN ('FTYPE %DocType%') DO SET OpenCmd=%%B
  62. FOR %%A IN (%OpenCmd%) DO (IF NOT DEFINED Exec SET Exec=%%~sfA)
  63. ECHO.%Exec% | FIND /I "\winword.exe" >NUL
  64. IF ERRORLEVEL 1 (
  65. 	ECHO.
  66. 	ECHO ERROR: .doc files aren't associated with Microsoft Word.
  67. 	ECHO.
  68. 	GOTO Syntax
  69. )
  70.  
  71. :: Open Microsoft Word
  72. START /MIN "Word" %Exec% /x /n /dde
  73.  
  74. :: Wait 5 seconds, to allow Word to get started; modify the delay for slow computers
  75. PING 127.0.0.1 -n 6 >NUL
  76.  
  77. :: Send the DDE commands to the opened Word session
  78. IF "%~2"=="" (
  79. 	CMCDDE WinWord System "[REM _DDE_Minimize][FileOpen(\"%~1\")][t=IsDocumentDirty()][FilePrint 0][SetDocumentDirty t][DocClose]"
  80. ) ELSE (
  81. 	CMCDDE WinWord System "[REM _DDE_Minimize][FileOpen(\"%~1\")][FilePrintSetup \"%~2 on p\",.DoNotSetAsSysDefault=1][FilePrint 0][DocClose 2][FilePrintSetup \"\"]"
  82. )
  83.  
  84. :: Done
  85. ENDLOCAL
  86. GOTO:EOF
  87.  
  88.  
  89. :Syntax
  90. ECHO.
  91. ECHO PrintDoc.bat,  Version 1.00 for Windows 2000 and later
  92. ECHO Print a Word document using Microsoft Word
  93. ECHO.
  94. ECHO Usage:  PRINTDOC  document  [ printer ]
  95. ECHO.
  96. ECHO Where:  document  is the Word document to be printed
  97. ECHO         printer   is the name of the printer (default printer if not specified)
  98. ECHO.
  99. ECHO Notes:  This batch file requires Microsoft Word to be associated to .DOC files.
  100. ECHO         This batch file also requires CMCDDE by Commence Corp.
  101. ECHO         If CMCDDE is not available, you will be prompted to download it.
  102. ECHO         This batch file opens Microsoft Word, but does not close it.
  103. ECHO         The DDE commands used are valid for MS Office 2007, and were retrieved
  104. ECHO         by GetDDE.vbs: http://www.robvanderwoude.com/wshexamples_g.html#GetDDE
  105. ECHO.
  106. ECHO Written by Rob van der Woude
  107. ECHO http://www.robvanderwoude.com
  108.  
  109. IF "%OS%"=="Windows_NT" ENDLOCAL
  110.  

page last modified: 2024-04-16; loaded in 0.0378 seconds