Sub ConvertXLS2XLSX() Dim strPath As String, strFile As String Dim wb As Workbook '选择文件夹路径 With Application.FileDialog(msoFileDialogFolderPicker) .Title = "选择文件夹" .Show If .SelectedItems.Count > 0 Then strPath = .SelectedItems(1) & "\" Else MsgBox "未选择文件夹!", vbExclamation Exit Sub End If End With '处理文件夹中的xls文件 strFile = Dir(strPath & "*.xls") Do While strFile <> "" Set wb = Workbooks.Open(strPath & strFile) wb.SaveAs Replace(wb.FullName, ".xls", ".xlsx"), FileFormat:=xlOpenXMLWorkbook wb.Close SaveChanges:=False strFile = Dir Loop MsgBox "转换完成!", vbInformation End Sub
Sub ConvertCSV2XLSX() Dim strPath As String, strFile As String Dim wb As Workbook '选择文件夹路径 With Application.FileDialog(msoFileDialogFolderPicker) .Title = "选择文件夹" .Show If .SelectedItems.Count > 0 Then strPath = .SelectedItems(1) & "\" Else MsgBox "未选择文件夹!", vbExclamation Exit Sub End If End With '处理文件夹中的csv文件 strFile = Dir(strPath & "*.csv") Do While strFile <> "" Set wb = Workbooks.Open(strPath & strFile, Local:=True) wb.SaveAs Replace(wb.FullName, ".csv", ".xlsx"), FileFormat:=xlOpenXMLWorkbook wb.Close SaveChanges:=False strFile = Dir Loop MsgBox "转换完成!", vbInformation End Sub
Sub ConvertDOC2DOCX() Dim strPath As String, strFile As String Dim objWord As Object '选择文件夹路径 With Application.FileDialog(msoFileDialogFolderPicker) .Title = "选择文件夹" .Show If .SelectedItems.Count > 0 Then strPath = .SelectedItems(1) & "\" Else MsgBox "未选择文件夹!", vbExclamation Exit Sub End If End With '处理文件夹中的doc文件 strFile = Dir(strPath & "*.doc") Do While strFile <> "" Set objWord = CreateObject("Word.Application") With objWord.Documents.Open(FileName:=strPath & strFile, ReadOnly:=True) .SaveAs2 Replace(.FullName, ".doc", ".docx"), FileFormat:=wdFormatXMLDocument .Close SaveChanges:=False End With objWord.Quit strFile = Dir Loop MsgBox "转换完成!", vbInformation End Sub
上述代码会弹出一个对话框用于选择包含要转换的doc文件的文件夹。然后,它会遍历文件夹中的每个doc文件,并将其另存为docx格式文件。转换完成后,会有一个消息框提示转换完成。请注意,为了运行此代码,您需要在VBA编辑器中添加对”Microsoft Word xx.x Object Library”的引用(其中xx.x是安装在您计算机上的版本号)。
Sub ConvertWordToPDF() Dim objWord As Object Dim objDoc As Object Dim filePath As String Dim exportPath As String ' 设置文件夹路径 filePath = "C:\Your\Word\Documents\Folder\" exportPath = "C:\Your\Export\PDF\Folder\"
' 关闭Word应用程序 objWord.Quit ' 释放对象的引用 Set objDoc = Nothing Set objWord = Nothing MsgBox "转换完成!"
End Sub
Sub FileSearch(sourceFolder As String, fileMask As String, objWord As Object, exportPath As String) Dim fileName As Variant Dim subFolder As Variant ' 遍历文件夹中的所有子文件和子文件夹 fileName = Dir(sourceFolder & fileMask) Do While fileName <> "" ExportPDF objWord, sourceFolder, fileName, exportPath fileName = Dir Loop subFolder = Dir(sourceFolder, vbDirectory) Do While subFolder <> "" If (subFolder <> ".") And (subFolder <> "..") Then FileSearch sourceFolder & subFolder & "\", fileMask, objWord, exportPath End If subFolder = Dir Loop End Sub
Sub ExportPDF(objWord As Object, sourceFolder As String, fileName As String, exportPath As String) Dim objDoc As Object ' 打开Word文档 Set objDoc = objWord.Documents.Open(sourceFolder & fileName) ' 设置为生成PDF格式的输出选项 objDoc.ExportAsFixedFormat outputFileName:=exportPath & Left(fileName, Len(fileName) - 4) & ".pdf", _ ExportFormat:=17, OpenAfterExport:=False
' 关闭Word文档 objDoc.Close SaveChanges:=False End Sub