我可以使用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
答案 0 :(得分:0)
以下代码提供了此信息:
-在文档开头插入文本“图形表格:”
-添加表格
-在目录中添加每张图片(包括其名称,如下面的标题和分页符)
-更新图表
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)。