如果您插入与文件链接的图片对象,当文件名更改时它们会自动更新。这假定您始终拥有相同数量的图片且名称不变。
Selection.InlineShapes.AddOLEObject ClassType:="Paint.Picture", FileName:= _
"C:\Users\name\Pictures\test.bmp", LinkToFile:=True, DisplayAsIcon:= _
False
假设您已经设置了一个包含模板Word文档的文件夹,其中包含指向另一个文件夹的图像链接,并且您想确保这些图像链接到以日期(例如20131008)命名的最新文件夹。您可以将图像链接到文件以进行自动更新,但由于其只读属性,无法以编程方式更改源路径。替代方法是循环遍历Word文档中的每个对象,查看它的路径是否为当前文件夹,如果不是,则删除原始对象并插入一个新对象。
以下是一个简单示例的代码。如果在插入图像后对其进行了任何增强,则可能需要复制定位和格式。我将我的文件夹结构设置如下,其中每个以日期命名的文件夹都有相同名称的图像。
对于OLE类型链接到.bmp图像:
Sub LinkToCurrentImageFolder()
'Get current folder by date
Dim clientFiguresPath As Variant
filePath = ActiveDocument.Path & "\ClientFigures\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(filePath)
Dim currentFolder As Variant: currentFolder = ""
For Each sf In fld.SUBFOLDERS
'Look at name and get current date
If currentFolder = "" Then
currentFolder = sf.Path
ElseIf sf.Path > currentFolder Then
currentFolder = sf.Path
End If
Next
' Debug: display current figure folder path
'MsgBox (currentFolder)
'Loop through all shapes in document and check if path is current.
'If path is not current delete current shape and add new because SourcePath is read-only
Dim Ishape As InlineShape, Wdoc As Document
MsgBox (ActiveDocument.InlineShapes.Count)
For Each Ishape In ActiveDocument.InlineShapes
If Not GetSourceInfo(Ishape) Then GoTo nextshape
With Ishape
currentPath = .LinkFormat.SourcePath
If currentPath <> currentFolder Then
cType = .OLEFormat.ClassType
shpName = .LinkFormat.SourceName
newPath = currentFolder & "\" & shpName
'Delete existing image
.Delete
'Create new image
Selection.InlineShapes.AddOLEObject ClassType:=cType, FileName:=newPath, LinkToFile:=True, DisplayAsIcon:=False
End If
End With
nextshape:
Next Ishape
End Sub
Function GetSourceInfo(oShp As InlineShape) As Boolean
On Error GoTo Error_GetSourceInfo
Test = oShp.LinkFormat.SourceFullName
GetSourceInfo = True
Exit Function
Error_GetSourceInfo:
GetSourceInfo = False
End Function
编辑
我已经修改了这段代码,使用的是链接到文件而不是OLE类型的图像。这假设您是通过以下方法插入图像:
![enter image description here](https://istack.dev59.com/GvBhk.webp)
Sub LinkToCurrentImageFolder()
'Get current folder by date
Dim clientFiguresPath As Variant
filePath = ActiveDocument.Path & "\ClientFigures\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(filePath)
Dim currentFolder As Variant: currentFolder = ""
For Each sf In fld.SUBFOLDERS
'Look at folder name/date and get most current date
If currentFolder = "" Then
currentFolder = sf.Path
ElseIf sf.Path > currentFolder Then
currentFolder = sf.Path
End If
Next
Dim Ishape As InlineShape
For Each Ishape In ActiveDocument.InlineShapes
If Ishape.Type = msoComment Then
With Ishape
currentPath = .LinkFormat.SourcePath
If currentPath <> currentFolder Then
shpName = .LinkFormat.SourceName
newPath = currentFolder & "\" & shpName
'Delete existing image
.Delete
'Create new image
Selection.InlineShapes.AddPicture FileName:=newPath, LinkToFile:=True, SaveWithDocument:=True
End If
End With
End If
Next Ishape
End Sub