我正在尝试浏览一个 word 文档,并用实际图像替换现有的图像路径字符串。当我输入硬编码的地址时,它可以工作。但是,如果我将相同的代码放入变量中,则会出现错误
错误:
运行时错误“5152”:
这不是一个有效的文件名。
尝试以下一项或多项操作:
* 检查路径以确保输入正确,
* 从文件和文件夹列表中选择一个文件。
代码:
Sub InsertJPGs()
For Each singleLine In ActiveDocument.Paragraphs
Dim Value As Variant
Dim imageName As String
Options.DefaultFilePath(wdDocumentsPath) = "d:\Downloads\ReportImages\"
originalLineText = singleLine.Range.Text
lineText = singleLine.Range.Text
If InStr(lineText, ".jpg") <> 0 Then
singleLine.Range.Select
rangeText = singleLine.Range.Text
imageName = rangeText
imageName = "D:\Downloads\ReportImages\" & rangeText
'imageName = "D:\Downloads\ReportImages\PictureImportTest_ATTICSkylight#1#_img2.jpg"
Selection.InlineShapes.AddPicture FileName:= _
imageName, LinkToFile:=True, SaveWithDocument:=True
End If
If InStr(lineText, "[[[IMAGE LIST]]]") <> 0 Then
Exit For
End If
Next singleLine
End Sub