我有一个文件夹,里面有许多产品的图片。每个产品都有多个图像。我需要识别以我的产品编号(例如,100100)开头并以"_FRONT“或"_ALTERNATE”两个结尾之一结尾的那些。这两者之间还有其他信息。例如,文件名可以是100100_headset_FRONT或100100_headset_SIDE。我希望它能找到每个产品的正面或备用图像。
我已经成功地提取了图像,我想我已经接近命名、调用该文件的方法了,但还没有完全到位。代码返回错误“未找到指定的文件”
Sub PictureP()
Dim picname As String, picend As String
Dim PicPath As String
Dim lThisRow As Long
Dim Pic As Shape
Dim rngPic As Range
lThisRow = 3
Do While (Cells(lThisRow, 2) <> "")
Set rngPic = Cells(lThisRow, 1) 'This is where picture will be inserted
picname = Cells(lThisRow, 2) 'This is the picture name
picend = "_FRONT"
present = Dir("H:\Media\Images\1 Web Ready\Previews\" & picname & "*" & picend & ".jpg")
PicPath = ("H:\Media\Images\1 Web Ready\Previews\" & picname & "*" & picend & ".jpg")
If present <> "" Then
Set Pic = ActiveSheet.Shapes.AddPicture(PicPath, msoFalse, msoCTrue, 1, 1, -1, -1)
Else
Cells(lThisRow, 1) = ""
End If
lThisRow = lThisRow + 1
Loop
Range("B3").Select
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub
End Sub
代码返回错误“未找到指定的文件”
发布于 2019-07-11 22:59:47
Dir()
正在正确评估*
通配符,并返回第一个匹配的值。
PicPath =
正在设置字符串值。设置字符串值不关心通配符,因此它被添加为文字值。
如果在运行时调试并打印出这两个值,就会在PicPath中看到*
。
最简单的解决方案是将picPath更改为使用Dir
,present
的结果,并将其附加到Dir()
搜索的目录中。
见下文。
Sub PictureP()
Dim picname As String, picend As String
Dim PicPath As String
Dim lThisRow As Long
Dim Pic As Shape
Dim rngPic As Range
lThisRow = 3
Do While (Cells(lThisRow, 2) <> "")
Set rngPic = Cells(lThisRow, 1) 'This is where picture will be inserted
picname = Cells(lThisRow, 2) 'This is the picture name
picend = "_FRONT"
present = Dir("H:\Media\Images\1 Web Ready\Previews\" & picname & "*" & picend & ".jpg")
PicPath = ("H:\Media\Images\1 Web Ready\Previews\" & present)
If present <> "" Then
Set Pic = ActiveSheet.Shapes.AddPicture(PicPath, msoFalse, msoCTrue, 1, 1, -1, -1)
Else
Cells(lThisRow, 1) = ""
End If
lThisRow = lThisRow + 1
Loop
Range("B3").Select
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub
End Sub
https://stackoverflow.com/questions/56991765
复制相似问题