Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for word2xps.vbs

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

  1. Option Explicit
  2.  
  3. Doc2XPS "C:\Documents and Settings\MyUserID\My Documents\resume.doc"
  4.  
  5. Sub Doc2XPS( myFile )
  6. ' This subroutine opens a Word document, then saves it as XPS, and closes Word.
  7. ' If the XPS file exists, it is overwritten.
  8. ' If Word was already active, the subroutine will leave the other document(s)
  9. ' alone and close only its "own" document.
  10. '
  11. ' Requirements:
  12. ' This script requires the "Microsoft Save as PDF or XPS Add-in for 2007
  13. ' Microsoft Office programs", available at:
  14. ' http://www.microsoft.com/downloads/details.aspx?
  15. '        familyid=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en
  16. '
  17. ' Written by Rob van der Woude
  18. ' http://www.robvanderwoude.com
  19.  
  20. 	' Standard housekeeping
  21. 	Dim objDoc, objFile, objFSO, objWord, strFile, strXPS
  22.  
  23. 	Const wdFormatDocument                    =  0
  24. 	Const wdFormatDocument97                  =  0
  25. 	Const wdFormatDocumentDefault             = 16
  26. 	Const wdFormatDOSText                     =  4
  27. 	Const wdFormatDOSTextLineBreaks           =  5
  28. 	Const wdFormatEncodedText                 =  7
  29. 	Const wdFormatFilteredHTML                = 10
  30. 	Const wdFormatFlatXML                     = 19
  31. 	Const wdFormatFlatXMLMacroEnabled         = 20
  32. 	Const wdFormatFlatXMLTemplate             = 21
  33. 	Const wdFormatFlatXMLTemplateMacroEnabled = 22
  34. 	Const wdFormatHTML                        =  8
  35. 	Const wdFormatPDF                         = 17
  36. 	Const wdFormatRTF                         =  6
  37. 	Const wdFormatTemplate                    =  1
  38. 	Const wdFormatTemplate97                  =  1
  39. 	Const wdFormatText                        =  2
  40. 	Const wdFormatTextLineBreaks              =  3
  41. 	Const wdFormatUnicodeText                 =  7
  42. 	Const wdFormatWebArchive                  =  9
  43. 	Const wdFormatXML                         = 11
  44. 	Const wdFormatXMLDocument                 = 12
  45. 	Const wdFormatXMLDocumentMacroEnabled     = 13
  46. 	Const wdFormatXMLTemplate                 = 14
  47. 	Const wdFormatXMLTemplateMacroEnabled     = 15
  48. 	Const wdFormatXPS                         = 18
  49.  
  50. 	' Create a File System object
  51. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  52.  
  53. 	' Create a Word object
  54. 	Set objWord = CreateObject( "Word.Application" )
  55.  
  56. 	With objWord
  57. 		' True: make Word visible; False: invisible
  58. 		.Visible = True
  59.  
  60. 		' Check if the Word document exists
  61. 		If objFSO.FileExists( myFile ) Then
  62. 			Set objFile = objFSO.GetFile( myFile )
  63. 			strFile = objFile.Path
  64. 		Else
  65. 			WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
  66. 			' Close Word
  67. 			.Quit
  68. 			Exit Sub
  69. 		End If
  70.  
  71. 		' Build the fully qualified XPS file name
  72. 		strXPS = objFSO.BuildPath( objFile.ParentFolder, _
  73. 		          objFSO.GetBaseName( objFile ) & ".xps" )
  74.  
  75. 		' Open the Word document
  76. 		.Documents.Open strFile
  77.  
  78. 		' Make the opened file the active document
  79. 		Set objDoc = .ActiveDocument
  80.  
  81. 		' Save in XML Paper Specification (XPS) format
  82. 		objDoc.SaveAs strXPS, wdFormatXPS
  83.  
  84. 		' Close the active document
  85. 		objDoc.Close
  86.  
  87. 		' Close Word
  88. 		.Quit
  89. 	End With
  90. End Sub
  91.  

page last uploaded: 2017-07-06, 12:37