【问题标题】:How to add multiple cross references to the multiple images added in a Word document?如何为Word文档中添加的多个图像添加多个交叉引用?
【发布时间】:2019-07-12 18:26:08
【问题描述】:

我可以使用 VBA 将多个图像添加到 Word 文档,同时我无法使用 VBA 添加多个交叉引用。

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

【问题讨论】:

  • 对于以下代码添加的图像,我需要为每个图像添加交叉引用,如果有任何人请建议我。 Sub add_images() FnInsertMultipleImages ("d:\Data\Images") End Sub
  • 函数定义为 : Function FnInsertMultipleImages(strFolderPath) Dim objWord Dim objDoc Dim objSelection Dim objShapes Dim objFSO Dim objFolder Set objWord = CreateObject("Word.Application") Set objFSO = CreateObject("Scripting.FileSystemObject" ) 设置 objFolder = objFSO.GetFolder(strFolderPath) 设置 objDoc = objWord.Documents.Open("D:\document.docx") objWord.Visible = True 设置 objSelection = objWord.Selection 对于 objFolder.Files 中的每个 Img ImgPath = Img。路径 objSelection.InlineShapes.AddPicture (ImgPath) objSelection.insertbreak Next End Function
  • 请编辑您的问题并将代码粘贴到那里。
  • 我在stackoverflow.com/a/54794824/10908769 提供了第三个问题的答案,并建议在此处删除此问题。

标签: vba ms-word


【解决方案1】:

我将向您展示一个示例。您将不得不修改它以满足您的需要。我将插入 2 张图片并创建一个 Cross Reference。我已经对代码进行了注释,因此您理解它应该没有问题。

逻辑:

  1. 添加图片
  2. 为图片添加标题
  3. 创建交叉引用
  4. 插入分页符?
  5. 重复上述步骤

代码:

Sub Sample()
    Dim shp As InlineShape
    Dim n As Long

    '
    '~~> Insert Image 1
    '
    Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageA.Png", _
                                                LinkToFile:=False, _
                                                SaveWithDocument:=True)

    '~~> Adding a caption
    n = 1
    CaptionLabels.Add Name:="MyImage" & n

    shp.Select
    Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
    Position:=wdCaptionPositionBelow, ExcludeLabel:=0

    Selection.InsertBreak


    '
    '~~> Insert Image 2
    '
    Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageB.Png", _
                                                LinkToFile:=False, _
                                                SaveWithDocument:=True)

    '~~> Adding a caption
    n = n + 1
    CaptionLabels.Add Name:="MyImage" & n

    shp.Select
    Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
    Position:=wdCaptionPositionBelow, ExcludeLabel:=0

    Selection.InsertBreak

    '~~> Creating cross reference
    For n = 1 To 2
        Selection.InsertCrossReference ReferenceType:="MyImage" & n, ReferenceKind:= _
        wdEntireCaption, ReferenceItem:=1, InsertAsHyperlink:=True, _
        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    Next n
End Sub

【讨论】:

  • 感谢@Siddharth Rout 提供信息,但是当我在循环中包含此步骤时遇到编译错误。
  • 设置 shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageB.Png", _ LinkToFile:=False, _ SaveWithDocument:=True) '~~> 添加标题 n = n + 1 CaptionLabels.Add Name:="MyImage" & n shp.Select Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _ Position:=wdCaptionPositionBelow, ExcludeLabel:= 0
  • 编译错误为 InlineShape 的“用户定义类型未定义”
  • 您能否在循环中实现添加和提供交叉引用,以便在文件夹路径中提供 15 个不同的图像。我试过了,但它显示了一些用户定义的类型错误和其他......提前感谢@Sidharth Rout
  • 我在没有修改的情况下也尝试了您的代码,但我收到了用户定义类型错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多