本文章主要内容是:
ChatGPT编写VBA程序
格式转换问题(批量转换.xls–>xlsx .csv–>xlsx .doc–>.docx .ppt–>.pptx ppt–>pdf)

准备

ChatGPT–镜像或者new-bing

Office 2019(或者其他版本,不要太老)

ChatGPT编写VBA程序

先在设置中把开发工具全选

image-20230717214210155

image-20230717214611831

然后在开发者工具

第一个代码

1
2
3
4
Sub SayHello()
MsgBox "你好!"
End Sub

image-20230717214711301

image-20230717214728629

格式转换

批量.xls–>.xlsx

image-20230717215358946

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

上述代码会弹出一个对话框用于选择包含要转换的xls文件的文件夹。然后,它会遍历文件夹中的每个xls文件,并将其另存为xlsx格式文件。转换完成后,会有一个消息框提示转换完成。请注意,这段代码只能处理一个文件夹中的xls文件,不会递归子文件夹。

批量.CSV->.xlsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

上述代码会弹出一个对话框用于选择包含要转换的csv文件的文件夹。然后,它会遍历文件夹中的每个csv文件,并将其另存为xlsx格式文件。转换完成后,会有一个消息框提示转换完成。请注意,这段代码只能处理一个文件夹中的csv文件,不会递归子文件夹。

批量.doc–>.docx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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是安装在您计算机上的版本号)。

批量将word–>pdf

提供一些关于使用VBA语言将Word文档批量转换为PDF格式的一般性指导:

  1. 打开一个新的Excel工作簿。
  2. 按Alt+F11打开Visual Basic for Applications编辑器。
  3. 在项目浏览窗格中,选择你要添加代码的工作簿对象。
  4. 在菜单栏上选择插入->模块,然后在编辑区域中输入以下VBA代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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应用程序对象
Set objWord = CreateObject("Word.Application")

' 如果不想显示打开的Word文档,可以将下一行代码取消注释
' objWord.Visible = False

' 循环处理文件夹中的所有Word文档
FileSearch filePath, "*.docx", objWord, exportPath

' 关闭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
  1. 修改代码中的filePathexportPath变量为实际的文件夹路径。确保你的Word文档在filePath指定的文件夹下,并且希望导出的PDF文件将保存在exportPath指定的文件夹下。
  2. 按F5运行代码。

这段VBA代码会递归遍历指定文件夹下的所有Word文档(.docx文件),并将它们批量转换为PDF格式保存在指定的输出文件夹中。

请注意,在使用此代码之前,你可能需要确保Microsoft Word已经安装在你的计算机上,并正确引用了Word对象库。可以通过依次选择工具->引用来添加对Microsoft Word XX.X Object Library的引用,其中的XX.X表示你所安装的Word版本。

将多个Excel文件合并成一个

您好,您可以使用下面的VBA代码来将多个Excel文件合并成一个文件。这个代码会弹出一个对话框,让您选择要合并的Excel文件所在的文件夹,然后将该文件夹中的所有Excel文件合并到一个新的Excel文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sub merge_excel_files()
Dim sDir As String
Dim result_dir As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "请选择要合并的Excel文件所在的文件夹"
.Show
result_dir = .SelectedItems(1)
End With
ChDir result_dir
sDir = Dir(result_dir & "\*.xlsx")
Dim newWB As Workbook
Set newWB = Workbooks.Add
While Len(sDir)
Dim tempWB As Workbook
Set tempWB = Workbooks.Open(Filename:=result_dir & "\" & sDir)
For Each ws In tempWB.Sheets
ws.Copy After:=newWB.Sheets(newWB.Sheets.Count)
Next ws
tempWB.Close SaveChanges:=False
sDir = Dir
Wend
newWB.SaveAs Filename:=result_dir & "\merged.xlsx", _
FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub