Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for word2pdf.vbs

(view source code of word2pdf.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim blnOverwrite
  4. Dim intConvertedFiles, intMatchingFiles, intOverwriteErrors
  5. Dim objParentFolder, objFSO
  6. Dim strExtension, strFile, strFileName, strMsg, strParentFolder, strPDFFile, strWordFile
  7.  
  8. blnOverwrite = False
  9. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  10.  
  11. With objFSO
  12. 	' Command Line Parsing
  13. 	Select Case WScript.Arguments.Unnamed.Count
  14. 		Case 0:
  15. 			Syntax Null
  16. 		Case 1:
  17. 			strWordFile     = .GetAbsolutePathName( WScript.Arguments.Unnamed(0) )
  18. 			strPDFFile      = .BuildPath( objFSO.GetParentFolderName( strWordFile ), .GetBaseName( strWordFile ) & ".pdf" )
  19. 			strExtension    = .GetExtensionName( strWordFile )
  20. 			strFileName     = .GetBaseName( strWordFile )
  21. 			strParentFolder = .GetParentFolderName( strWordFile )
  22. 		Case 2:
  23. 			strWordFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(0) )
  24. 			If InStr( strWordFile, "*" ) Then
  25. 				Syntax "No wildcards allowed in Word file specification if PDF file is specified."
  26. 			Else
  27. 				strPDFFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(1) )
  28. 			End If
  29. 		Case Else:
  30. 			Syntax "Invalid command line arguments."
  31. 	End Select
  32.  
  33. 	Select Case WScript.Arguments.Named.Count
  34. 		Case 0:
  35. 			' OK
  36. 		Case 1:
  37. 			If WScript.Arguments.Named.Exists( "O" ) Then
  38. 				blnOverwrite = True
  39. 			Else
  40. 				Syntax "Invalid command line switch."
  41. 			End If
  42. 		Case 2:
  43. 			If WScript.Arguments.Named.Exists( "O" ) Then
  44. 				Syntax "Invalid command line switch."
  45. 			Else
  46. 				Syntax "Invalid command line switches."
  47. 			End If
  48. 		Case Else:
  49. 			Syntax "Invalid command line switches."
  50. 	End Select
  51.  
  52. 	' Command Line Validation
  53. 	If InStr( strWordFile, "?" ) Then
  54. 		Syntax "Invalid wildcard(s) in specified Word document"
  55. 	End If
  56. 	If InStr( strWordFile, "*" ) Then
  57. 		If strFileName = "*" And Not InStr( strExtension, "*" ) And Not InStr( strParentFolder, "*" ) Then
  58. 			intMatchingFiles = 0
  59. 			For Each strFile In .GetFolder( strParentFolder ).Files
  60. 				If LCase( .GetExtensionName( strFile ) ) = LCase( strExtension ) Then
  61. 					intMatchingFiles = intMatchingFiles + 1
  62. 				End If
  63. 			Next
  64. 			If intMatchingFiles = 0 Then
  65. 				Syntax "No files matching Word documents specification"
  66. 			End If
  67. 		Else
  68. 			Syntax "Invalid wildcard(s) in specified Word document"
  69. 		End If
  70. 	Else
  71. 		If Not .FileExists( strWordFile ) Then
  72. 			Syntax "The specified Word document does not exist."
  73. 		End If
  74. 		If InStr( strPDFFile, "*" ) Then
  75. 			Syntax "No wildcards allowed in PDF file specification."
  76. 		End If
  77. 		If Not .FolderExists( .GetParentFolderName( strPDFFile ) ) Then
  78. 			Syntax "The parent folder for the specified output file does not exist."
  79. 		End If
  80. 		If Not LCase( .GetExtensionName( strPDFFile ) ) = "pdf" Then
  81. 			Syntax "The specified PDF file must have a "".pdf"" extension."
  82. 		End If
  83. 		If Not blnOverwrite Then
  84. 			If .FileExists( strPDFFile ) Then
  85. 				Syntax "The specified PDF file already exists." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files."
  86. 			End If
  87. 		End If
  88. 	End If
  89.  
  90. 	' The actual conversion is done by the Doc2PDF subroutine
  91. 	If InStr( strWordFile, "*" ) Then
  92. 		intOverwriteErrors = 0
  93. 		intConvertedFiles  = 0
  94. 		strMsg             = ""
  95. 		For Each strFile In .GetFolder( .GetParentFolderName( strWordFile ) ).Files
  96. 			If LCase( .GetExtensionName( strFile ) ) = LCase( .GetExtensionName( strWordFile ) ) Then
  97. 				strPDFFile = .BuildPath( strParentFolder, .GetBaseName( strFile ) & ".pdf" )
  98. 				If Not blnOverwrite And .FileExists( strPDFFile ) Then
  99. 					intOverwriteErrors = intOverwriteErrors + 1
  100. 					strMsg = strMsg & "ERROR:" & vbTab & """" & .GetFileName( strPDFFile ) & """ already exists." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files." & vbCrLf
  101. 				Else
  102. 					intConvertedFiles = intConvertedFiles + 1
  103. 					strMsg            = strMsg & "Converting """ & .GetFileName( strFile ) & """ . . ." & vbCrLf
  104. 					Doc2PDF .GetAbsolutePathName( strFile ), strPDFFile
  105. 				End If
  106. 			End If
  107. 		Next
  108. 		WScript.Echo strMsg & vbCrLf & intConvertedFiles & " documents successfully converted, " & intOverwriteErrors & " existing PDF files were skipped." & vbCrLf
  109. 		If intOverwriteErrors > 0 Then
  110. 			Syntax intOverwriteErrors & " existing PDF files were skipped." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files."
  111. 		End If
  112. 	Else
  113. 		Doc2PDF strWordFile, strPDFFile
  114. 	End If
  115. End With
  116.  
  117. ' Finished
  118. Set objFSO = Nothing
  119.  
  120.  
  121. Sub Doc2PDF( myWordFile, myPDFFile )
  122. 	Dim objDoc, objWord
  123. 	Const wdFormatDocument                    =  0
  124. 	Const wdFormatDocument97                  =  0
  125. 	Const wdFormatDocumentDefault             = 16
  126. 	Const wdFormatDOSText                     =  4
  127. 	Const wdFormatDOSTextLineBreaks           =  5
  128. 	Const wdFormatEncodedText                 =  7
  129. 	Const wdFormatFilteredHTML                = 10
  130. 	Const wdFormatFlatXML                     = 19
  131. 	Const wdFormatFlatXMLMacroEnabled         = 20
  132. 	Const wdFormatFlatXMLTemplate             = 21
  133. 	Const wdFormatFlatXMLTemplateMacroEnabled = 22
  134. 	Const wdFormatHTML                        =  8
  135. 	Const wdFormatPDF                         = 17
  136. 	Const wdFormatRTF                         =  6
  137. 	Const wdFormatTemplate                    =  1
  138. 	Const wdFormatTemplate97                  =  1
  139. 	Const wdFormatText                        =  2
  140. 	Const wdFormatTextLineBreaks              =  3
  141. 	Const wdFormatUnicodeText                 =  7
  142. 	Const wdFormatWebArchive                  =  9
  143. 	Const wdFormatXML                         = 11
  144. 	Const wdFormatXMLDocument                 = 12
  145. 	Const wdFormatXMLDocumentMacroEnabled     = 13
  146. 	Const wdFormatXMLTemplate                 = 14
  147. 	Const wdFormatXMLTemplateMacroEnabled     = 15
  148. 	Const wdFormatXPS                         = 18
  149. 	On Error Resume Next
  150. 	Set objWord = CreateObject( "Word.Application" )
  151. 	If Err Then
  152. 		Syntax "Unable to access MS Word. Make sure MS Office is installed" & vbCrLf & vbTab & "(MSI based installation, NOT a ""click-to-run"" installation)."
  153. 	Else
  154. 		objWord.Visible = True
  155. 		objWord.Documents.Open myWordFile
  156. 		If Err Then
  157. 			Syntax "Unable to open the specified document in MS Word."
  158. 		Else
  159. 			Set objDoc = objWord.ActiveDocument
  160. 			objDoc.SaveAs myPDFFile, wdFormatPDF
  161. 			objDoc.Close
  162. 			Set objDoc = Nothing
  163. 			If Err Then Syntax "Unable to save the document as PDF."
  164. 		End If
  165. 		objWord.Quit
  166. 		Set objWord = Nothing
  167. 	End If
  168. 	On Error Goto 0
  169. End Sub
  170.  
  171.  
  172. Sub Syntax( myMessage )
  173. 	Dim strMsg
  174. 	On Error Resume Next
  175. 	objDoc.Close
  176. 	objWord.Quit
  177. 	Set objDoc  = Nothing
  178. 	Set objWord = Nothing
  179. 	Set objFSO  = Nothing
  180. 	On Error Goto 0
  181. 	strMsg = ""
  182. 	If Trim( " " & myMessage ) <> "" Then strMsg = vbCrLf & "ERROR:" & vbTab & myMessage & vbCrLf
  183. 	strMsg = strMsg _
  184. 	       & vbCrLf _
  185. 	       & "Word2PDF.vbs,  Version 1.11" _
  186. 	       & vbCrLf   _
  187. 	       & "Open a Microsoft Word document and save it in PDF format" _
  188. 	       & vbCrLf   & vbCrLf _
  189. 	       & "Usage:" & vbTab  & "WORD2PDF.VBS    wordfile  [ pdffile ]  [ /O ]" _
  190. 	       & vbCrLf   & vbCrLf _
  191. 	       & "Where:" & vbTab  & """wordfile""" & vbTab & "Word document(s) to be converted (wildcard" _
  192. 	       & vbCrLf   _
  193. 	       & "      " & vbTab  & "          "   & vbTab & "allowed for file name, e.g. ""*.doc"" or ""*.docx"")" _
  194. 	       & vbCrLf   _
  195. 	       & "      " & vbTab  & """pdffile"""  & vbTab & "PDF file to be created (no wildcards allowed," _
  196. 	       & vbCrLf   _
  197. 	       & "      " & vbTab  & "          "   & vbTab & "default: path/name of Word file, extension "".pdf"")" _
  198. 	       & vbCrLf   _
  199. 	       & "      " & vbTab  & "/O        "   & vbTab & "silently Overwrite existing PDF files (default:" _
  200. 	       & vbCrLf   _
  201. 	       & "      " & vbTab  & "          "   & vbTab & "abort with error message if PDF file exists)" _
  202. 	       & vbCrLf   & vbCrLf _
  203. 	       & "Notes:" & vbTab  & "[1]"          & vbTab & "This script requires a ""regular"" (MSI based)" _
  204. 	       & vbCrLf   _
  205. 	       & "      " & vbTab  & "       "      & vbTab & "Microsoft Word installation, it WILL FAIL on" _
  206. 	       & vbCrLf   _
  207. 	       & "      " & vbTab  & "       "      & vbTab & "a ""click-to-run"" installation of MS Office." _
  208. 	       & vbCrLf   _
  209. 	       & "      " & vbTab  & "[2]"          & vbTab & "For Word 2007, this script requires the" _
  210. 	       & vbCrLf   _
  211. 	       & "      " & vbTab  & "       "      & vbTab & """Microsoft Save as PDF or XPS Add-in for 2007" _
  212. 	       & vbCrLf   _
  213. 	       & "      " & vbTab  & "       "      & vbTab & "Microsoft Office programs"", available at:" _
  214. 	       & vbCrLf   _
  215. 	       & "      " & vbTab  & "       "      & vbTab & "www.microsoft.com/en-us/download/details.aspx?id=7" _
  216. 	       & vbCrLf   _
  217. 	       & "      " & vbTab  & "[3]"          & vbTab & "Wildcard ""*"" is allowed for the file NAME of the" _
  218. 	       & vbCrLf   _
  219. 	       & "      " & vbTab  & "       "      & vbTab & "Word document, but in that case no PDF file should" _
  220. 	       & vbCrLf   _
  221. 	       & "      " & vbTab  & "       "      & vbTab & "be specified, as wildcards are NOT allowed in the" _
  222. 	       & vbCrLf   _
  223. 	       & "      " & vbTab  & "       "      & vbTab & "PDF file specification." _
  224. 	       & vbCrLf   _
  225. 	       & "      " & vbTab  & "[4]"          & vbTab & "If wildcard ""*"" is used for the Word document, and" _
  226. 	       & vbCrLf   _
  227. 	       & "      " & vbTab  & "       "      & vbTab & "the /O switch is not used, the script will display an" _
  228. 	       & vbCrLf   _
  229. 	       & "      " & vbTab  & "       "      & vbTab & "error message in case a PDF file already exists, but" _
  230. 	       & vbCrLf   _
  231. 	       & "      " & vbTab  & "       "      & vbTab & "it will then continue to convert the next file instead" _
  232. 	       & vbCrLf   _
  233. 	       & "      " & vbTab  & "       "      & vbTab & "of aborting." _
  234. 	       & vbCrLf   _
  235. 	       & "      " & vbTab  & "[5]"          & vbTab & "If Word was already active when this script is started," _
  236. 	       & vbCrLf   _
  237. 	       & "      " & vbTab  & "       "      & vbTab & "the other document(s) will be left alone, and only the" _
  238. 	       & vbCrLf   _
  239. 	       & "      " & vbTab  & "       "      & vbTab & "document opened by this script will be closed." _
  240. 	       & vbCrLf   & vbCrLf _
  241. 	       & "Written by Rob van der Woude" _
  242. 	       & vbCrLf _
  243. 	       & "http://www.robvanderwoude.com"
  244. 	WScript.Echo strMsg
  245. 	WScript.Quit 1
  246. End Sub
  247.  

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