【问题标题】:Excel VBA macro to save excel workbook to pdf will not saveExcel VBA宏将excel工作簿保存为pdf不会保存
【发布时间】:2020-08-06 21:52:56
【问题描述】:

我有一个带有多个选项卡的 Excel 工作簿,我创建了一个 Windows 计划任务来打开工作簿并将工作簿保存为 pdf,但是当我调试它时,这部分代码出现错误。我认为可能是前一个实例已处理并将相同的 pdf 留在同一文件夹中。它可能不会覆盖旧的pdf。

ERROR 运行时错误'-214701887 (80071779)';文档未保存。

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation

完整的 VBA

    Sub Auto_Open()


Dim sht As Worksheet

'AutoFit Every Worksheet Column in a Workbook
  For Each sht In ThisWorkbook.Worksheets
    sht.Cells.EntireColumn.AutoFit
  Next sht


Application.DisplayAlerts = False
  
'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\manuel\Documents\SQL Server Management Studio\alert-email\LOG.PDF"

Application.DisplayAlerts = True

'Save active workbook as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=saveLocation
  
  
  
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAtttachments As Object

Set OutLookApp = CreateObject("Outlook.Application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments

With OutLookMailItem
.To = "manuel@gmail.com"
.Subject = "Test Summary"
.Body = "This e-email is automatically generated and will be sent every weekday at 6AM. We can customerize and add more reports later."
myAttachments.Add "C:\Users\manuel\Documents\SQL Server Management Studio\alert-email\LOG.PDF"
.send
'.Display
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing

ThisWorkbook.Save
ThisWorkbook.Saved = True
Application.Quit

End Sub

【问题讨论】:

  • “有一个错误” - 你得到的错误是什么?
  • 运行时错误'-214701887 (80071779)';文档未保存。
  • 您在哪个用户帐户下运行计划任务?它是否有权访问文件位置?

标签: excel vba pdf


【解决方案1】:

试试这个。

Option Explicit
     
Sub ExportXLToPDF()
 
    'Comments:
    'Assume list of worksheets to be included in output are listed in Column 1 on "List"
 
    Dim wb                  As Workbook
    Dim ws                  As Worksheet
    Dim Arr()               As String
    Dim MaxRows             As Long
    Dim i                   As Long
    Dim strPath             As String
    Dim strFileName         As String
    Const strEXTENSION      As String = ".pdf"
     
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("List")
     
    'User - where to save the output file
        strPath = GetFolder & "\"
         
    'User - what to name the output file
        strFileName = GetUserInput(strPrompt:="Please enter a name for the output file", _
                                   strTitle:="File Name")
         
    'Assume list to be included in sheets array in on worksheet named list in Column 1 beginning in Row 1
    'Total number of rows is dynamic
        MaxRows = GetRows(ws:=ws)
         
    'Redim the array to hold the name of the worksheets
        ReDim Preserve Arr(1 To MaxRows)
     
    'Load the list of sheets to be included into the array
        For i = 1 To MaxRows
            Arr(i) = ws.Cells(i, 1).Value
        Next i
         
    'Select the sheets array
        Sheets(Arr).Select
  
    'Export to the sheets array to pdf
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                        Filename:=strPath & strFileName & strEXTENSION, _
                                        Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=True, _
                                        IgnorePrintAreas:=False, _
                                        OpenAfterPublish:=False
                     
    'Tidy up
        'Erase arrays
            Erase Arr
         
        'Destroy objects
            Set ws = Nothing
            Set wb = Nothing
End Sub

Public Function GetRows(ws As Worksheet) As Long
  
    Dim r       As Long
      
    With ws
        r = .Cells(Rows.Count, 1).End(xlUp).Row
        GetRows = r
    End With
      
End Function
 
Public Function GetUserInput(strPrompt As String, _
                             strTitle As String) As String
       
    Dim strUserInput As String
       
    strUserInput = InputBox(Prompt:=strPrompt, _
                            Title:=strTitle)
                               
    GetUserInput = strUserInput
   
End Function
 
Public Function GetFolder() As String
   
    Dim fd As FileDialog
    Dim strFolderName As String
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
       
    With fd
        .Title = "Please select a folder"
        .AllowMultiSelect = False
        .Show
        strFolderName = .SelectedItems(1)
    End With
   
    GetFolder = strFolderName
       
    Set fd = Nothing
End Function

【讨论】:

  • 这应该是一个窗口计划任务。我不想被提示保存到某个文件夹并命名文件。该文件还有一个 SQL 连接,每次打开文件时都会刷新数据。
  • 好的,去掉提示即可。最终游戏是一样的。