【问题标题】:Convert excel to pdf with VBScript使用 VBScript 将 excel 转换为 pdf
【发布时间】:2018-01-27 05:23:31
【问题描述】:

我想用 vbscript 将 pdf 转换为 excel,但它给了我一个错误,下面是我的代码:

Option Explicit
Dim objExcel, strExcelPath, objSheet

strExcelPath = "path\file.xlsx"

' Open specified spreadsheet and select the first worksheet.
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' Modify a cell.
objSheet.Range("B8").FormulaR1C1  = [sample1]
objSheet.Range("G11").FormulaR1C1 = [sample2]
objSheet.Range("G12").FormulaR1C1 = [sample3]
objSheet.Range("B10").FormulaR1C1 = [sample4]
objSheet.Range("B11").FormulaR1C1 = [sample5]

' Save as Excel.
objExcel.ActiveWorkbook.SaveAs "path\name.xlsx" 
' Problem in saving to pdf
objExcel.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:= "path\name.pdf", _
        Quality:= xlQualityStandard, IncludeDocProperties:=True,_
        IgnorePrintAreas:=False, OpenAfterPublish:=False

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

如果您还发现代码有任何改进,请告诉我。

【问题讨论】:

  • 在哪一行抛出错误?
  • 您能详细解释一下错误详情吗?
  • 在我说“保存到 pdf 时出现问题”的 cmets 中
  • xlQualityStandard 更改为 0
  • 不,它没有帮助

标签: excel pdf vbscript


【解决方案1】:

您最好在编辑并保存后关闭 Excel 工作簿。您正在更新 excel 文件,同时您试图将其保存为 Pdf。这可能会导致问题。

将其保存为excel后,将其关闭并重新打开以导出为Pdf文件。

【讨论】:

  • 没有。在从 VBScript 中保存宏之前,我要么停止它,要么更好地运行一个宏,但我必须看看如何传递我想要保存的名称的值......
  • 添加文件的完整路径可能会解决此问题。例如:C:\path\name.pdf 或 C:\path\name.xlsx
【解决方案2】:

如果其他人遇到此问题,请替换:

objExcel.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:= "path\name.pdf", _
        Quality:= xlQualityStandard, IncludeDocProperties:=True,_
        IgnorePrintAreas:=False, OpenAfterPublish:=False

与:

objExcel.ActiveSheet.ExportAsFixedFormat 0, "path\name.pdf" ,0, 1, 0,,,0

这样就可以了!

【讨论】:

  • 您需要将xlTypePDF 替换为0(以及其他人)的原因是vbscript 无法识别VBA 对象模型常量,例如xlTypePDF。 VBA 自动将此转换为枚举值 (0),但您需要在 vbscript 中指定枚举。
【解决方案3】:

如果您将文件拖放到上面,我的代码将转换 word 和 excel 文件:

Set fso = CreateObject("Scripting.FileSystemObject")

if WScript.Arguments.Count = 0 then
    MsgBox "Please drop Word or Excel ile to conver it to PDF"

Else
    Dim iDocCount: iDocCount = 0
    Dim iXlsCount: iXlsCount = 0

    For i = 0 to WScript.Arguments.Count -1
      sFilePath = WScript.Arguments(i)
      iPos = InStrRev(sFilePath,".")
      If iPos > 0 Then
        sExt = Mid(sFilePath,iPos)
        sPdfPath = Mid(sFilePath,1,iPos) & "pdf"

        If fso.FileExists(sPdfPath) = False Then        
          If sExt = ".xlsx" Then
            ExcelToPdf sFilePath, sPdfPath
            iXlsCount = iXlsCount + 1

          ElseIf sExt = ".docx" Then
            WordToPdf sFilePath, sPdfPath
            iDocCount = iDocCount + 1
          End If          
        End If

      End If
    Next

    If iDocCount = 0 And iXlsCount = 0 Then
        MsgBox "Did not convert any documents"
    Else
        MsgBox "Converted " & iXlsCount & " xlsx file(s) and " & iDocCount & " docx file(s)"
    End If

End if


Sub ExcelToPdf(sFrom, sTo)
  Set excel = CreateObject("Excel.Application")
  excel.ScreenUpdating = false
  excel.DisplayAlerts = false

  Set workbook = excel.Workbooks.Open(sFrom)
  workbook.ExportAsFixedFormat 0, sTo

  workbook.Close()
  excel.Quit()

  Set workbook = Nothing
  Set excel = Nothing
End Sub

Sub WordToPdf(sFrom, sTo)
  Set word = CreateObject("Word.Application")
  Set doc = word.Documents.Open(sFrom)
  doc.Activate()
  doc.SaveAs2 sTo, 17
  doc.Close()
  word.Quit()
  Set doc= Nothing
  Set word = Nothing
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2018-02-03
    相关资源
    最近更新 更多