如何为单词中存在的多个图像插入多个标题?例如,图像

时间:2019-02-20 05:14:16

标签: vba

我可以使用VBA将多个图像添加到Word文档中,但是不能为从文件夹路径加载的多个图像添加标题。
您能建议一下吗?

Sub checking()
    Dim strFolderPath
    strFolderPath = "C:\images"
    Dim objWord
    Dim objDoc
    Dim objSelection
    Dim objShapes
    Dim objFSO
    Dim objFolder

    Set objWord = CreateObject("Word.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolderPath)
    Set objDoc = objWord.Documents.Open("D:\myfile.docx")

    objWord.Visible = True

    Set objSelection = objWord.Selection

    For Each Img In objFolder.Files
        ImgPath = Img.Path
        objSelection.InlineShapes.AddPicture (ImgPath)
        objSelection.InsertBreak
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

这样,您将获得以下订单:
-带有段落符号的图片
-带有段落符号的图像路径
-分页符

For Each img In objFolder.Files
    imgpath = img.Path
    objSelection.InlineShapes.AddPicture (imgpath)
    objSelection.InsertParagraphAfter
    objSelection.InsertAfter imgpath
    objSelection.InsertParagraphAfter
    objSelection.Collapse wdCollapseEnd
    objSelection.InsertBreak
Next

另外,我建议在每个模块的开头使用Option Explicit,这将强制声明每个变量(例如Dim ImgPath as String)。