Sub ConvertExcelToPDF() ' 定义变量 Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim objExcel As Object Dim objWorkbook As Object Dim objPDFPrinter As Object Dim strFolderPath As String Dim strFileName As String Dim strPDFFileName As String Dim i As Long ' 设置打印机为 Microsoft Print to PDF Set objPDFPrinter = CreateObject("WScript.Network") objPDFPrinter.SetDefaultPrinter "Microsoft Print to PDF" ' 设置文件夹路径 strFolderPath = Environ("USERPROFILE") & "\Desktop\1\" ' 创建文件系统对象 Set objFSO = CreateObject("Scripting.FileSystemObject") ' 获取文件夹对象 Set objFolder = objFSO.GetFolder(strFolderPath) ' 遍历文件夹下的所有 Excel 文件 For Each objFile In objFolder.Files If objFSO.GetExtensionName(objFile.Path) = "xlsx" Then ' 打开 Excel 文件 Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(objFile.Path) ' 构造 PDF 文件名 strFileName = objFSO.GetFileName(objFile.Path) strPDFFileName = objFSO.BuildPath(objFolder.Path, objFSO.GetBaseName(strFileName)) & ".pdf" ' 将文件保存为 PDF 格式 objWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPDFFileName ' 关闭 Excel 文件 objWorkbook.Close False objExcel.Quit ' 显示正在处理的文件名 Application.StatusBar = "正在处理:" & strFileName ' 等待 1 秒,以便显示下一个文件名 Application.Wait Now + TimeValue("0:00:01") End If Next objFile ' 恢复状态栏 Application.StatusBar = False ' 释放对象 Set objFolder = Nothing Set objFile = Nothing Set objExcel = Nothing Set objWorkbook = Nothing Set objPDFPrinter = Nothing Set objFSO = NothingEnd Sub 使用用户窗体显示进度消息
时间: 2024-02-10 14:17:24 浏览: 130
可以在代码中添加一个用户窗体(UserForm),并在其中添加一个标签(Label),用于显示进度消息。在代码中,可以使用 UserForm.Show 方法显示用户窗体,在 For 循环中,可以更新标签的 Caption 属性,以显示正在处理的文件名。最后,可以使用 UserForm.Hide 方法隐藏用户窗体。以下是修改后的代码示例:
```vb
Sub ConvertExcelToPDF()
' 定义变量
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim objExcel As Object
Dim objWorkbook As Object
Dim objPDFPrinter As Object
Dim strFolderPath As String
Dim strFileName As String
Dim strPDFFileName As String
Dim i As Long
' 创建用户窗体
Dim frmProgress As UserForm
Set frmProgress = UserForms.Add("frmProgress")
frmProgress.Caption = "转换进度"
frmProgress.Width = 300
frmProgress.Height = 100
frmProgress.Show
' 设置打印机为 Microsoft Print to PDF
Set objPDFPrinter = CreateObject("WScript.Network")
objPDFPrinter.SetDefaultPrinter "Microsoft Print to PDF"
' 设置文件夹路径
strFolderPath = Environ("USERPROFILE") & "\Desktop\1\"
' 创建文件系统对象
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 获取文件夹对象
Set objFolder = objFSO.GetFolder(strFolderPath)
' 遍历文件夹下的所有 Excel 文件
For Each objFile In objFolder.Files
If objFSO.GetExtensionName(objFile.Path) = "xlsx" Then
' 打开 Excel 文件
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
' 构造 PDF 文件名
strFileName = objFSO.GetFileName(objFile.Path)
strPDFFileName = objFSO.BuildPath(objFolder.Path, objFSO.GetBaseName(strFileName)) & ".pdf"
' 将文件保存为 PDF 格式
objWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPDFFileName
' 关闭 Excel 文件
objWorkbook.Close False
objExcel.Quit
' 显示正在处理的文件名
frmProgress.lblProgress.Caption = "正在处理:" & strFileName
' 等待 1 秒,以便显示下一个文件名
Application.Wait Now + TimeValue("0:00:01")
End If
Next objFile
' 隐藏用户窗体
frmProgress.Hide
' 恢复状态栏
Application.StatusBar = False
' 释放对象
Set objFolder = Nothing
Set objFile = Nothing
Set objExcel = Nothing
Set objWorkbook = Nothing
Set objPDFPrinter = Nothing
Set objFSO = Nothing
End Sub
```
阅读全文