跳到主要内容

如何在Word文档中插入多个带有文件名的图片?

在Word文档中,可以使用“插入”功能一次快速插入多张图片。 但是,有时在插入图片时需要插入文件路径和名称作为标题。 您如何在Word文件中处理此任务?

使用VBA代码插入多个带文件名的图片

使用 Kutools for Word 插入带有文件名的多张图片


使用VBA代码插入多个带文件名的图片

以下VBA代码可以帮助您在插入图像时插入文件路径和名称作为标题,请按以下步骤操作:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 然后,单击 插页 > 模块,将以下代码复制并粘贴到打开的空白模块中:

VBA代码:使用文件名插入多张图片:

Sub PicWithCaption()
    Dim xFileDialog As FileDialog
    Dim xPath, xFile As Variant
    On Error Resume Next
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    If xFileDialog.Show = -1 Then
        xPath = xFileDialog.SelectedItems.Item(1)
        If xPath <> "" Then
            xFile = Dir(xPath & "\*.*")
            Do While xFile <> ""
                If UCase(Right(xFile, 3)) = "PNG" Or _
                    UCase(Right(xFile, 3)) = "TIF" Or _
                    UCase(Right(xFile, 3)) = "JPG" Or _
                    UCase(Right(xFile, 3)) = "GIF" Or _
                    UCase(Right(xFile, 3)) = "BMP" Then
                    With Selection
                        .InlineShapes.AddPicture xPath & "\" & xFile, False, True
                        .InsertAfter vbCrLf
                        .MoveDown wdLine
                        .Text = xPath & "\" & xFile & Chr(10)
                        .MoveDown wdLine
                    End With
                End If
                xFile = Dir()
            Loop
        End If
    End If
End Sub

3。 然后按 F5 键以运行此代码,将显示“浏览”窗口,请选择包含要插入图像的文件夹,请参见屏幕截图:

doc插入文件名为1的图像

4。 然后点击 OK 按钮,所选文件夹中的所有图像都已插入到Word文档中,并且文件路径和名称也已作为标题插入,请参见截图:

doc插入文件名为2的图像


使用 Kutools for Word 插入带有文件名的多张图片

如果你有 Kutools for Word,其 图片 功能,您可以快速将多张带有文件路径和名称的图片立即插入Word文档。

Kutools for Word : 带有超过100个方便的Word加载项,可以在60天内免费试用. 

安装后 Kutools for Word,请这样做:

1. 点击 库工具 > 图片,请参见屏幕截图:

doc插入文件名为3的图像

2。 在弹出 插入图片 对话框,请执行以下操作:

  • (1.)点击 添加文件 or 添加文件夹 按钮选择要插入的图像;
  • (2.)然后检查 插入每张图片的文件路径作为标题 对话框左下方的选项;
  • (3.)然后单击 插页 按钮。

doc插入文件名为4的图像

3。 插入图像后,您将看到每张图片的文件路径和名称也被插入,请参见屏幕截图:

doc插入文件名为5的图像

点击下载Kutools for Word并立即免费试用!

最佳办公生产力工具

Kutools for Word - 通过 Over 提升您的文字体验 100 显着特点!

深入了解下面突出显示的功能或 单击此处探索 Kutools for Word 的全部功能.

📘 文档掌握: 分页  /  合并文件  /  以各种格式导出选择(PDF/TXT/DOC/XLSX)  /  批量转换为PDF  /  将页面导出为图像  /  一次打印多个文件 ...

内容编辑: 批量查找和替换 跨多个文件   /  调整所有图片的大小   /  转置表行和列  /  将表格转换为文字 ...

🧹 轻松清洁扫开 多余的空间  /  分节符  /   所有标题  /  文本框  /  超链接  /  如需更多删除工具,请前往我们的删除组

创意插入: 插页 千位分隔符  /  复选框  /  单选按钮  /  二维码  /  条码  /  对角线表  /  公式标题  /  图片说明  /  表标题  /  多张图片  /  在我们的插入组中发现更多信息

🔍 精准选择: 精确定位特定页面  /    /  形状  /  标题段落  /  使用我们的选择组轻松导航

星级增强: 快速导航至任何位置  /  自动插入重复文本  /  在文档窗口之间无缝切换  /  11 转换工具 ...

使用 Kutools 转换您的 Word 任务。 👉 立即下载并试用 30 天 🚀。

了解更多       免费下载      购买
 
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
VBA CODE IS VERY USEFULLY MY DATA VALIDATE IMAGES SHARING.
This comment was minimized by the moderator on the site
How can I decrease the overall size of the photo so there are multiple per page?
This comment was minimized by the moderator on the site
Is there a way to insert each picture and file name per page? Sometimes if one picture is too big the file name will then go to the next page or if picture is too small the next picture will be together in one page instead on the next page
This comment was minimized by the moderator on the site
Hello, Rizza,
To insert each picture and file name per page, please apply the following vba code:
Sub PicWithCaption()
    Dim xFileDialog As FileDialog
    Dim xPath As String, xFile As String
    On Error Resume Next
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    If xFileDialog.Show = -1 Then
        xPath = xFileDialog.SelectedItems.Item(1)
        If xPath <> "" Then
            xFile = Dir(xPath & "\*.*")
            Do While xFile <> ""
                If UCase(Right(xFile, 3)) = "PNG" Or _
                    UCase(Right(xFile, 3)) = "TIF" Or _
                    UCase(Right(xFile, 3)) = "JPG" Or _
                    UCase(Right(xFile, 3)) = "GIF" Or _
                    UCase(Right(xFile, 3)) = "BMP" Then
                    
                    ' Add the picture
                    Selection.InlineShapes.AddPicture xPath & "\" & xFile, False, True
                    Selection.TypeParagraph
                    
                    ' Add the file path below the picture
                    Selection.TypeText xPath & "\" & xFile
                    Selection.TypeParagraph
                    
                    ' Move to the next page
                    Selection.InsertBreak Type:=wdPageBreak
                End If
                xFile = Dir()
            Loop
        End If
    End If
End Sub


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
A problem occurs when the file name to be added exceeds 10.

pic1
pic10
pic11
pic12
pic2
pic3
pic4
pic5...
This comment was minimized by the moderator on the site
change the file name to 01, 02, 03 and it will work
This comment was minimized by the moderator on the site
Wow this is great! I only needed the file name, so I deleted "xPath & "\" &" from the .Text line and that worked perfectly. Thanks for this!
This comment was minimized by the moderator on the site
Which line of code would you change and how to have the file path written before the picture, aka the caption (filename) is above rather than below.
This comment was minimized by the moderator on the site
With Selection
.Text = xFile
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.MoveDown wdLine

End With
This comment was minimized by the moderator on the site
Have to add this .MoveDown wdLine after .Text=xFile
This comment was minimized by the moderator on the site
buenas noches amigo me ha servido mucho su información sinembargo me surge una duda cree que es posible incertar imagenes en word de la misma manera pero dentro de celdas incertadas?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations