我有一个word文档,其中包含其中列出的嵌入式照片文件名(JPEGS)。我的目标是使用文档中列出的实际照片自动更新word文档。我希望编写一个宏来搜索word文档中每个文件名的出现,并在文件名后面立即将实际的JPEG对象插入到Word文档中。我需要继续这个过程,直到我在word文档中找到所有JPEG文件名。每个文件名长度为26个字符,它们都以“IMG”开头。作为一个新手,我被困在如何将文件名的文本复制到VBA变量。
以下是word文档的示例:
晚上9:52汤姆,照片的前后都附上了。
标签:建筑 照片:IMG_0000320_2014.03.11.jpg,IMG_0000321_2014.03.11.jpg,IMG_0000322_2014.03.11.jpg
下午4:24汤姆,附图。请查看。
标签:预奖 照片:IMG_0000323_2014.03.11.jpg,IMG_0000324_2014.03.11.jpg,IMG_0000325_2014.03.11.jpg,IMG_0000326_2014.03.11.jpg
我的初始编码尝试如下:(我还没有尝试过DO WHILE逻辑)
Dim JPEGName As String
Selection.Find.ClearFormatting
With Selection.Find
.Text = "IMG"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=26, Extend:=wdExtend
JPEGName = Selection.Copy
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.InlineShapes.AddPicture FileName:= _
JPEGName _
, LinkToFile:=False, SaveWithDocument:=True
Selection.TypeParagraph
End Sub
我在Selection.Copy语句中收到编译错误:预期函数或变量。
答案 0 :(得分:0)
这样的事情可能是:
Sub InsertPics()
Const PATH_TO_PICS As String = "C:\_Stuff\Test\"
Dim JPEGName As String
Selection.Find.ClearFormatting
With Selection.Find
.Text = "IMG*.jpg"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do While Selection.Find.Execute()
JPEGName = Selection.Text
Selection.Collapse wdCollapseEnd
Selection.TypeParagraph
Selection.InlineShapes.AddPicture _
FileName:=PATH_TO_PICS & JPEGName, _
LinkToFile:=False, _
SaveWithDocument:=True
Selection.TypeParagraph
Loop
End Sub