可通过VBA宏在Word中批量插入图片并自动添加文件名题注:启用开发工具→插入模块→粘贴代码→运行宏选择文件夹→支持扩展格式→可调整尺寸与样式。

如果您需要在Word文档中批量插入图片,并为每张图片自动添加对应文件名作为题注,可以通过编写VBA宏实现自动化处理。以下是完成该任务的具体步骤:
一、启用开发工具并创建新宏
Word默认不显示“开发工具”选项卡,需先启用该功能,才能访问VBA编辑器并编写宏代码。启用后可新建模块并粘贴完整脚本。
1、点击“文件”→“选项”→“自定义功能区”。
2、在右侧“主选项卡”列表中勾选开发工具,点击“确定”。
3、切换到“开发工具”选项卡,点击“Visual Basic”按钮打开VBA编辑器。
4、在左侧工程资源管理器中右键“Normal”→“插入”→“模块”,新建一个空白模块。
二、插入带路径读取与图片插入功能的宏代码
该宏将遍历指定文件夹下所有支持的图片格式(如JPG、PNG、BMP),按顺序插入当前光标位置,并在每张图片下方插入居中、加粗的文件名作为题注。
1、在新建模块的代码窗口中粘贴以下VBA代码:
Sub InsertImagesWithFilename()
Dim fd As FileDialog
Dim folderPath As String
Dim fileName As String
Dim picRange As Range
Dim para As Paragraph
Dim i As Integer
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
If fd.Show -1 Then Exit Sub
folderPath = fd.SelectedItems(1) & "\"
fileName = Dir(folderPath & "*.jpg")
Do While fileName ""
Set picRange = Selection.Range
picRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.InlineShapes.AddPicture FileName:=folderPath & fileName, LinkToFile:=False, SaveWithDocument:=True, Range:=picRange
Set para = ActiveDocument.Paragraphs.Add(Range:=picRange)
para.Range.Text = fileName
para.Range.Font.Bold = True
para.Alignment = wdAlignParagraphCenter
fileName = Dir
Loop
fileName = Dir(folderPath & "*.png")
Do While fileName ""
Set picRange = Selection.Range
picRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.InlineShapes.AddPicture FileName:=folderPath & fileName, LinkToFile:=False, SaveWithDocument:=True, Range:=picRange
Set para = ActiveDocument.Paragraphs.Add(Range:=picRange)
para.Range.Text = fileName
para.Range.Font.Bold = True
para.Alignment = wdAlignParagraphCenter
fileName = Dir
Loop
fileName = Dir(folderPath & "*.bmp")
Do While fileName ""
Set picRange = Selection.Range
picRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.InlineShapes.AddPicture FileName:=folderPath & fileName, LinkToFile:=False, SaveWithDocument:=True, Range:=picRange
Set para = ActiveDocument.Paragraphs.Add(Range:=picRange)
para.Range.Text = fileName
para.Range.Font.Bold = True
para.Alignment = wdAlignParagraphCenter
fileName = Dir
Loop
End Sub
三、运行宏并选择图片所在文件夹
宏代码保存后,可通过“开发工具”选项卡中的“宏”按钮调用执行。程序将弹出文件夹选择对话框,用户选定目标路径后,自动开始批量插入操作。
1、返回Word界面,在“开发工具”选项卡中点击“宏”按钮。
2、在宏列表中选中InsertImagesWithFilename,点击“运行”。
3、在弹出的文件夹选择窗口中,定位到存放图片的文件夹,点击“确定”。
4、宏开始执行,依次插入图片及对应文件名,光标位置将随插入内容自动下移。
四、使用通配符扩展支持其他图片格式
若需支持GIF、TIFF等格式,可在原宏中追加对应Dir查找逻辑段落,避免重复编写结构化代码,提升可维护性。
1、在原有代码末尾“fileName = Dir(folderPath & "*.bmp”)”循环之后新增:
fileName = Dir(folderPath & "*.gif")
2、复制从“Do While fileName ""”到“Loop”之间的全部代码块(含图片插入、题注添加、Dir更新)。
3、将新粘贴代码块中的“.bmp”替换为“.gif”,确保路径匹配正确。
4、对“.tiff”或“.webp”等格式依同样方式追加独立循环段落。
五、调整图片尺寸与题注样式以适配文档版式
插入后的图片默认按原始尺寸显示,可能超出页面宽度;题注字体大小和段前/段后间距也需统一设置,确保排版一致。
1、在宏代码中“ActiveDocument.InlineShapes.AddPicture”语句后插入缩放控制行:
ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).Width = CentimetersToPoints(15.5)
2、在“para.Range.Font.Bold = True”之后添加字体大小设定:
para.Range.Font.Size = 10.5
3、在“para.Alignment = wdAlignParagraphCenter”之后添加段落间距控制:
para.SpaceBefore = 6
para.SpaceAfter = 12










