我有一个文本文档,其中明确写出图像路径,例如:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
c:\Users\userx\Documents\img_000180.jpg
我正在寻找一些宏脚本,它将在文档中查找这些文件路径并将这些图像插入到 Word 文档中。
你知道这样的脚本吗?
答案1
我录制了一个宏,并对其进行了一些修改,以使其具有通用性。它满足了我的需要。以下是脚本:
Sub replace_path_with_image()
'
' replace_path_with_image Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "c:\users"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EscapeKey
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Dim Sel As Selection
Set Sel = Application.Selection
Dim FilePath As String
If Sel.Type <> wdSelectionIP Then
FilePath = Sel.Text
End If
Selection.Cut
Selection.InlineShapes.AddPicture FileName:= _
FilePath _
, LinkToFile:=False, SaveWithDocument:=True
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub