我可以使用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
发布于 2019-02-21 04:35:48
下面的代码提供了这个功能:
在文档开头插入文本"Table of Figure:“添加图表添加目录的每一张图片(包括其名称为下面的标题和分页符)更新图表
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)。
https://stackoverflow.com/questions/54783700
复制相似问题