Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for word2rtf.vbs

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

  1. Option Explicit
  2.  
  3. Doc2RTF "C:\Documents and Settings\MyUserID\My Documents\resume.doc"
  4.  
  5. Sub Doc2RTF( myFile )
  6. ' This subroutine opens a Word document, then saves it as RTF, and closes Word.
  7. ' If the RTF 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. ' Written by Rob van der Woude
  12. ' http://www.robvanderwoude.com
  13.  
  14. 	' Standard housekeeping
  15. 	Dim objDoc, objFile, objFSO, objWord, strFile, strRTF
  16.  
  17. 	Const wdFormatDocument                    =  0
  18. 	Const wdFormatDocument97                  =  0
  19. 	Const wdFormatDocumentDefault             = 16
  20. 	Const wdFormatDOSText                     =  4
  21. 	Const wdFormatDOSTextLineBreaks           =  5
  22. 	Const wdFormatEncodedText                 =  7
  23. 	Const wdFormatFilteredHTML                = 10
  24. 	Const wdFormatFlatXML                     = 19
  25. 	Const wdFormatFlatXMLMacroEnabled         = 20
  26. 	Const wdFormatFlatXMLTemplate             = 21
  27. 	Const wdFormatFlatXMLTemplateMacroEnabled = 22
  28. 	Const wdFormatHTML                        =  8
  29. 	Const wdFormatPDF                         = 17
  30. 	Const wdFormatRTF                         =  6
  31. 	Const wdFormatTemplate                    =  1
  32. 	Const wdFormatTemplate97                  =  1
  33. 	Const wdFormatText                        =  2
  34. 	Const wdFormatTextLineBreaks              =  3
  35. 	Const wdFormatUnicodeText                 =  7
  36. 	Const wdFormatWebArchive                  =  9
  37. 	Const wdFormatXML                         = 11
  38. 	Const wdFormatXMLDocument                 = 12
  39. 	Const wdFormatXMLDocumentMacroEnabled     = 13
  40. 	Const wdFormatXMLTemplate                 = 14
  41. 	Const wdFormatXMLTemplateMacroEnabled     = 15
  42. 	Const wdFormatXPS                         = 18
  43.  
  44. 	' Create a File System object
  45. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  46.  
  47. 	' Create a Word object
  48. 	Set objWord = CreateObject( "Word.Application" )
  49.  
  50. 	With objWord
  51. 		' True: make Word visible; False: invisible
  52. 		.Visible = True
  53.  
  54. 		' Check if the Word document exists
  55. 		If objFSO.FileExists( myFile ) Then
  56. 			Set objFile = objFSO.GetFile( myFile )
  57. 			strFile = objFile.Path
  58. 		Else
  59. 			WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
  60. 			' Close Word
  61. 			.Quit
  62. 			Exit Sub
  63. 		End If
  64.  
  65. 		' Build the fully qualified HTML file name
  66. 		strRTF = objFSO.BuildPath( objFile.ParentFolder, _
  67. 		         objFSO.GetBaseName( objFile ) & ".rtf" )
  68.  
  69. 		' Open the Word document
  70. 		.Documents.Open strFile
  71.  
  72. 		' Make the opened file the active document
  73. 		Set objDoc = .ActiveDocument
  74.  
  75. 		' Save as HTML
  76. 		objDoc.SaveAs strRTF, wdFormatRTF
  77.  
  78. 		' Close the active document
  79. 		objDoc.Close
  80.  
  81. 		' Close Word
  82. 		.Quit
  83. 	End With
  84. End Sub
  85.  

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