1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
'************************************************
'
' DOC2PDF.VBS Microsoft Scripting Host Script (Requires Version 5.6 or newer)
' --------------------------------------------------------------------------------
'
' Author: Philippe Mallocci
' Created: 01 april 2007
'
' This script can create PDF files from a Word document provided that you
' have Adobe Acrobat Distiller installed.
'
' Constants
Const cFolder = "c:\testfax"
Const cPrinter = "Adobe PDF"
Const cExtension = ".doc" ' .doc and .rft are both accepted
Const bShowDebug = FALSE
Const WdDoNotSaveChanges = 0
Const version = "1.0"
' ***********************************************
' DOC2PDF
'
' Converts all Word documents find into c:\testfax to PDF using Adobe
' Distiller.
'
'
Function DOC2PDF()
Dim fso ' As FileSystemObject
Dim wdo ' As Word.Application
Dim wdoc ' As Word.Document
Dim wdocs ' As Word.Documents
Dim sPrevPrinter ' As String
Dim oDistiller ' As PDFDistiller.PDFDistiller.1
Dim sDocFile, sPDFFile ' As String
Dim iNumber
Dim objFolder
Dim colFiles
Dim objFile
Set oDistiller = CreateObject("PDFDistiller.PDFDistiller.1")
If oDistiller Is Nothing Then
WScript.Echo "Error: Cannot create PDF document. Adobe Acrobat " + "Distiller is not available! Quiting..."
WScript.Quit 1
End If
Set wdo = CreateObject("Word.Application")
If wdo Is Nothing Then
WScript.Echo "Error: Cannot create Word document. MS Word " + "MS Word is not available! Quiting..."
WScript.Quit 2
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set ofile = CreateObject("Scripting.FileSystemObject")
' Remember current active printer
sPrevPrinter = wdo.ActivePrinter
' Select Adode printer
wdo.ActivePrinter = cPrinter
' get List of files in the folder
Set objFolder = ofile.GetFolder(cFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
sDocFile = cFolder + "\" + objFile.Name
sPDFFile = cFolder + "\" + fso.GetBaseName(sDocFile) + ".pdf"
sTempFile = fso.GetSpecialFolder(TemporaryFolder) + "\" + fso.GetTempName()
WScript.Echo "ici " & sDocFile & " " & cExtension
If (InStr (sDocFile,cExtension) > 0) Then
WScript.Echo "la"
' Debug outputs...
If bShowDebug Then
WScript.Echo "Doc file = '" + sDocFile + "'"
WScript.Echo "Temporary file = '" + sTempFile + "'"
WScript.Echo "PDF file = '" + sPDFFile + "'"
End If
' Open the Word document
Set wdocs = wdo.Documents
Set wdoc = wdocs.Open(sDocFile)
' Print the Word document to the Acrobat Distiller -
' will generate a postscript (.ps) (temporary) file
wdo.ActiveDocument.PrintOut False , , , sTempFile
wdoc.Close WdDoNotSaveChanges
' Debug output...
If bShowDebug Then
WScript.Echo " Distilling to '" + sPDFFile + "'"
End If
' Distill the postscript file to PDF
WScript.Echo sTempFile & " " & sPDFFile
oDistiller.FileToPDF sTempFile, sPDFFile, ""
' Delete the temporary postscript file...
WScript.Echo sTempFile
fso.DeleteFile( sTempFile )
iNumber = iNumber + 1
End if
Next
Set oDistiller = Nothing
Set fso = Nothing
Set ofile = Nothing
' restore current active printer
wdo.ActivePrinter = sPrevPrinter
wdo.Quit WdDoNotSaveChanges
Set wdo = Nothing
WScript.Echo "(doc2pdf v" & version & ") Done! " & iNumber & " PDF file(s) created"
End Function
' *** MAIN **************************************
Call DOC2PDF() |