将下述代码保存到txt文件中,
然后修改文件扩展名为.vbs文件
方案一:把多个待转换的word文件拖到该vbs文件上进行转换,就可以把转换的PDF保存到指定文件夹中
1、保存路径中不包含中文:
'Convert .doc or .docx to .pdf files via Send To menu
Const savePath = "E:\zhuomian\PDF\"
Set fso = CreateObject("Scripting.FileSystemObject")
For i= 0 To WScript.Arguments.Count -1
docPath = WScript.Arguments(i)
docPath = fso.GetAbsolutePathName(docPath)
If LCase(Right(docPath, 4)) = ".doc" Or LCase(Right(docPath, 5)) = ".docx" Then
Set objWord = CreateObject("Word.Application")
pdfPath =savePath & _
fso.GetBaseName(docpath) & ".pdf"
objWord.Visible = False
Set objDoc = objWord.documents.open(docPath)
objDoc.saveas pdfPath, 17
objDoc.Close
objWord.Quit
End If
Next
MsgBox "success!"
2、保存路径中可以包含中文
'Convert .doc or .docx to .pdf files via Send To menu
Const savePath = "E:\zhuomian\PDF开发\"
Set fso = CreateObject("Scripting.FileSystemObject")
For i= 0 To WScript.Arguments.Count -1
docPath = WScript.Arguments(i)
docPath = fso.GetAbsolutePathName(docPath)
If LCase(Right(docPath, 4)) = ".doc" Or LCase(Right(docPath, 5)) = ".docx" Then
Set objWord = CreateObject("Word.Application")
pdfPath =savePath & _
fso.GetBaseName(docpath) & ".pdf"
objWord.Visible = False
Set objDoc = objWord.documents.open(docPath)
objDoc.saveas pdfPath, 17
objDoc.Close
objWord.Quit
End If
Next
MsgBox "success!"
方案二:将该vbs文件放到一个含有待转换的word文件夹内,双击该vbs文件,就可以把转换的PDF保存到指定文件夹中
1、保存路径中不包含中文:
On Error Resume Next
Const savePath = "E:\zhuomian\PDF\"
Const wdExportFormatPDF = 17
Set oWord = WScript.CreateObject("Word.Application")
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set fds=fso.GetFolder(".")
Set ffs=fds.Files
For Each ff In ffs
If (LCase(Right(ff.Name,4))=".doc" Or LCase(Right(ff.Name,4))="docx" ) And Left(ff.Name,1)<>"~" Then
Set oDoc=oWord.Documents.Open(ff.Path)
odoc.ExportAsFixedFormat Left(savePath+ff.Name,InStrRev(savePath+ff.Name,"."))&"pdf",wdExportFormatPDF
oDoc.Close
If Err.Number Then
MsgBox Err.Description
End If
End If
Next
oWord.Quit
Set oDoc=Nothing
Set oWord =Nothing
MsgBox "success!"
2、保存路径中可以包含中文
On Error Resume Next
Const savePath = "E:\zhuomian\PDF开发\"
Const wdExportFormatPDF = 17
Set oWord = WScript.CreateObject("Word.Application")
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set fds=fso.GetFolder(".")
Set ffs=fds.Files
For Each ff In ffs
If (LCase(Right(ff.Name,4))=".doc" Or LCase(Right(ff.Name,4))="docx" ) And Left(ff.Name,1)<>"~" Then
Set oDoc=oWord.Documents.Open(ff.Path)
odoc.ExportAsFixedFormat Left(savePath+ff.Name,InStrRev(savePath+ff.Name,"."))&"pdf",wdExportFormatPDF
oDoc.Close
If Err.Number Then
MsgBox Err.Description
End If
End If
Next
oWord.Quit
Set oDoc=Nothing
Set oWord =Nothing
MsgBox "success!"