我想使用vbscript将docx文件转换为pdf。但是我的脚本总是出错。我不知道我在做什么错:
Set oFSO = CreateObject("Scripting.FileSystemObject")
For i= 0 To WScript.Arguments.Count -1
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
oFile = WScript.Arguments(i)
dirPath = oFSO.GetParentFolderName(oFile)
If UCase(oFSO.GetExtensionName(oFile)) = "DOC" Or UCase(oFSO.GetExtensionName(oFile)) = "DOCX" Then
pdfPath = dirPath & "\" & GetFilenameWithoutExtension(oFile) & ".pdf"
Set objDoc = objWord.documents.open(dirPath & "\" & oFile)
objDoc.saveas pdfPath, 17
objDoc.Close
End If
objWord.Quit
Next
Function GetFilenameWithoutExtension(ByVal FileName)
Dim Result, i
Result = FileName
i = InStrRev(FileName, ".")
If ( i > 0 ) Then
Result = Mid(FileName, 1, i - 1)
End If
GetFilenameWithoutExtension = Result
End Function
此代码给出以下错误:
script.vbs(15,5)Microsoft VBScript运行时错误:所需对象:'objWord.documents.open(...)'
我尝试了以下操作:
Set oFSO = CreateObject("Scripting.FileSystemObject")
For i= 0 To WScript.Arguments.Count -1
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set oFile = oFSO.GetFile(WScript.Arguments(i))
dirPath = oFSO.GetParentFolderName(WScript.Arguments(i))
If UCase(oFSO.GetExtensionName(oFile.Name)) = "DOC" Or UCase(oFSO.GetExtensionName(oFile.Name)) = "DOCX" Then
pdfPath = dirPath & "\" & GetFilenameWithoutExtension(oFile.Name) & ".pdf"
Set objDoc = objWord.documents.open(dirPath & "\" & oFile.Name)
objDoc.saveas pdfPath, 17
objDoc.Close
End If
objWord.Quit
Next
Function GetFilenameWithoutExtension(ByVal FileName)
Dim Result, i
Result = FileName
i = InStrRev(FileName, ".")
If ( i > 0 ) Then
Result = Mid(FileName, 1, i - 1)
End If
GetFilenameWithoutExtension = Result
End Function
但是后来我得到了错误:
script.vbs(12,5)Microsoft Word:命令失败
我找到了解决方案。第二个版本适用于用word创建的文档。
因为我在通过代码创建的文档中使用此选项,所以该选项本身在单词中不起作用。因此,正常情况下,该脚本也无法使用。