代码之家  ›  专栏  ›  技术社区  ›  Matt Taylor

将电子表格从文件夹转换为PDF(保存到其他位置)

  •  0
  • Matt Taylor  · 技术社区  · 6 年前

    我想选择保存PDF的位置,而不是将它们保存到Excel文件所在的文件夹中。

    我也只想打印第一个工作表。

    我添加了以2结尾的dims来尝试使其正常工作。我会显示两个弹出窗口,但在选择要保存PDF的位置后,它会在 Set objFolder2 = objFileSystem2.GetFolder(strPath2)

    任何帮助都非常感谢。

    Sub ExcelPlot()
    Dim objShell As Object
    Dim objWindowsFolder As Object
    Dim objWindowsFolder2 As Object
    Dim strWindowsFolder As String
    
    'Select the specific Windows folder
    Set objShell = CreateObject("Shell.Application")
    Set objWindowsFolder = objShell.BrowseForFolder(0, "Locate the Excel files", 0, "")
    
    'Select where to save to
    Set objShell = CreateObject("Shell.Application")
    Set objWindowsFolder2 = objShell.BrowseForFolder(0, "Where would you like to save the PDFs?", 0, "")
    
    If Not objWindowsFolder Is Nothing Then
       strWindowsFolder = objWindowsFolder.self.Path & "\"
    
       Call ProcessFolders(strWindowsFolder)
    
       'Open the windows folder
       Shell "Explorer.exe" & " " & strWindowsFolder, vbNormalFocus
    End If
    End Sub
    
    Sub ProcessFolders(strPath As String)
    Dim strPath2 As String
    Dim objFileSystem As Object
    Dim objFileSystem2 As Object
    Dim objFolder As Object
    Dim objFolder2 As Object
    Dim objFile As Object
    Dim objExcelFile As Object
    Dim objWorkbook As Excel.Workbook
    Dim strWorkbookName As String
    
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFileSystem.GetFolder(strPath)
    Set objFolder2 = objFileSystem2.GetFolder(strPath2)
    
    For Each objFile In objFolder.Files
        strFileExtension = objFileSystem.GetExtensionName(objFile)
        If LCase(strFileExtension) = "xls" Or LCase(strFileExtension) = "xlsx" Then
           Set objExcelFile = objFile
           Set objWorkbook = Application.Workbooks.Open(objExcelFile.Path)
    
           strWorkbookName = Left(objWorkbook.Name, (Len(objWorkbook.Name) - Len(strFileExtension)) - 1)
           objWorkbook.ExportAsFixedFormat Type:=xlTypePDF, fileName:=strPath2 & strWorkbookName & ".pdf"
    
           objWorkbook.Close False
        End If
    Next
    
    'Process all folders and subfolders
    If objFolder.SubFolders.Count > 0 Then
       For Each objSubFolder In objFolder.SubFolders
           If ((objSubFolder.Attributes And 2) = 0) And ((objSubFolder.Attributes And 4) = 0) Then
              ProcessFolders (objSubFolder.Path)
           End If
       Next
    End If
    End Sub
    

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   Tim Williams    6 年前

    您可以这样做-您需要将两条路径都传递给 ProcessFolders

    Sub ExcelPlot()
    
        Dim sourceFolder As String, destFolder As String
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Title = "Locate the Excel files"
            If .Show = -1 Then
                sourceFolder = .SelectedItems(1)
                .Title = "Where would you like to save the PDFs?"
                If .Show = -1 Then
                    destFolder = .SelectedItems(1)
                    ProcessFolders sourceFolder, destFolder
                    Shell "Explorer.exe" & " " & destFolder, vbNormalFocus
                End If
            End If
        End With
    End Sub
    

    编辑:以下是文件夹处理子文件夹的更新(非递归)版本:

    Sub ProcessFolders(sourceFolder As String, destFolder As String)
    
        Dim objFileSystem As Object
        Dim objFolder As Object
        Dim objSubFolder As Object
        Dim objFile As Object
        Dim objWorkbook As Excel.Workbook
        Dim strWorkbookName As String, strFileExtension As String
    
        Set objFileSystem = CreateObject("Scripting.FileSystemObject")
        Dim colFolders As New Collection
    
        colFolders.Add sourceFolder
    
        Do While colFolders.Count > 0
    
            Set objFolder = objFileSystem.GetFolder(colFolders(1)) 'get the first path
            colFolders.Remove 1 'remove from listing
    
            'Process files in this folder
            For Each objFile In objFolder.Files
    
                strFileExtension = objFileSystem.GetExtensionName(objFile)
                If LCase(strFileExtension) = "xls" Or LCase(strFileExtension) = "xlsx" Then
    
                   Set objWorkbook = Application.Workbooks.Open(objFile.Path)
    
                   strWorkbookName = Left(objWorkbook.Name, _
                                         (Len(objWorkbook.Name) - Len(strFileExtension)) - 1)
                   objWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
                      Filename:=objFileSystem.buildpath(destFolder, strWorkbookName & ".pdf")
    
                   objWorkbook.Close False
                End If
            Next
    
            'Process subfolders
            For Each objSubFolder In objFolder.SubFolders
                If ((objSubFolder.Attributes And 2) = 0) And ((objSubFolder.Attributes And 4) = 0) Then
                   colFolders.Add objSubFolder.Path  'add this to the collection for processing
                End If
            Next
    
        Loop
    
    End Sub