Excel 将电子表格从文件夹转换为PDF(保存到其他位置)
我想选择保存PDF的位置,而不是将其保存到excel文件所在的文件夹中 我还想只打印第一张工作表 以2结尾的阴影是我添加的,以尝试使其工作。我将显示两个弹出窗口,但在选择要保存PDF的位置后,它在Excel 将电子表格从文件夹转换为PDF(保存到其他位置),excel,vba,Excel,Vba,我想选择保存PDF的位置,而不是将其保存到excel文件所在的文件夹中 我还想只打印第一张工作表 以2结尾的阴影是我添加的,以尝试使其工作。我将显示两个弹出窗口,但在选择要保存PDF的位置后,它在Set objFolder2=objFileSystem2.GetFolder(strPath2) 非常感谢您的帮助 Sub ExcelPlot() Dim objShell As Object Dim objWindowsFolder As Object Dim objWindowsFolder2 A
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
谢谢您可以这样做-您需要将这两个路径都传递到
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
我得到“参数非可选”,它突出显示
子进程文件夹(sourceFolder为字符串,destFolder为字符串)
您是否调用该子进程并传递两个参数?我运行ExcelPlot子进程,它在ProcessFolders行出错我发布的代码为我运行,没有错误,所以我不确定你到底有什么问题。我的帖子只是为了满足您管理两个路径的需要-也许您忘记了在递归调用中从Sub本身(在“处理所有文件夹和子文件夹”下)添加第二个路径?我没有添加第二个补丁,也不确定要添加到哪里