首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为word中存在的多个图像插入多个标题和超链接?例如,下面的图片“Figure:1”及其超链接

如何为word中存在的多个图像插入多个标题和超链接?例如,下面的图片“Figure:1”及其超链接
EN

Stack Overflow用户
提问于 2019-02-20 18:07:52
回答 1查看 273关注 0票数 0

我可以使用VBA向word文档添加多个图像,但无法为从文件夹路径加载的多个图像添加标题及其超链接。你能在这方面提出建议吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 04:35:48

下面的代码提供了这个功能:

在文档开头插入文本"Table of Figure:“添加图表添加目录的每一张图片(包括其名称为下面的标题和分页符)更新图表

代码语言:javascript
复制
Sub InsertPicturesAndTheirNames()
    Dim objWord As Object   ' Word.Application
    Dim objDoc As Object    ' Word.Document
    Dim objShape As Object  ' Word.InlineShape
    Dim objTOF As Object    ' Word.TableOfFigures
    Dim objFSO As Object    ' Scripting.FileSystemObject
    Dim strFolderPath As String
    Dim objFolder As Object ' Scripting.Folder
    Dim imgpath As String
    Dim img As Object       ' Scripting.File

    strFolderPath = "C:\images"

    On Error Resume Next
    If objWord Is Nothing Then
        Set objWord = GetObject(, "Word.Application")
        If objWord Is Nothing Then
            Set objWord = CreateObject("Word.Application")
        End If
    End If
    On Error GoTo 0
    objWord.Visible = True

    Set objDoc = objWord.Documents.Open("D:\myfile.docx")

    objDoc.Bookmarks("\StartOfDoc").Select
    objWord.Selection.Text = "Table of Figures:"
    objWord.Selection.InsertParagraphAfter
    objWord.Selection.Collapse 0    ' 0 = wdCollapseEnd

    objDoc.TablesOfFigures.Format = 5 ' 5 = wdTOFSimple
    Set objTOF = objDoc.TablesOfFigures.Add( _
        Range:=objWord.Selection.Range, _
        Caption:=-1, _
        IncludeLabel:=True, _
        RightAlignPageNumbers:=True, _
        UseHeadingStyles:=False, _
        UpperHeadingLevel:=1, _
        LowerHeadingLevel:=3, _
        IncludePageNumbers:=True, _
        AddedStyles:="", _
        UseHyperlinks:=True, _
        HidePageNumbersInWeb:=True) ' -1 = wdCaptionFigure
    objTOF.TabLeader = 1 ' 1 = wdTabLeaderDots
    objTOF.Range.InsertParagraphAfter
    objTOF.Range.Next(Unit:=4, Count:=1).InsertBreak Type:=7 ' 4 = wdParagraph, 7 = wdPageBreak

    objDoc.Bookmarks("\EndOfDoc").Select

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolderPath)
    For Each img In objFolder.Files
        imgpath = img.Path
        Set objShape = objDoc.InlineShapes.AddPicture( _
            Filename:=imgpath, _
            LinkToFile:=True, _
            SaveWithDocument:=False)
        objShape.Range.InsertCaption _
                Label:=-1, _
                TitleAutoText:="", _
                Title:=": " & Mid(imgpath, InStrRev(imgpath, "\") + 1), _
                Position:=1, _
                ExcludeLabel:=False ' -1 = wdCaptionFigure, 1 = wdCaptionPositionBelow
        objDoc.Bookmarks("\EndOfDoc").Select
        objWord.Selection.InsertParagraphAfter
        objDoc.Bookmarks("\EndOfDoc").Select
        objWord.Selection.InsertBreak Type:=7 ' 7 = wdPageBreak
    Next

    objTOF.Update
End Sub

如果您添加了对Microsoft Word x.x Object Library的引用,则可以使用早期绑定。这意味着您可以使用我标注为注释的自解释ENUM值。

图片作为链接存储在文档中,因为如果完全存储它们,文档可能会变得非常大(请参阅AddPicture)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54783700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档