Excel通过宏实现根据名字同步插入图片(同名)的时候,总是出现不存在该图片的问题,如何解决?代码如下

Sub 插入照片()✀自动插入图片前,删除所有图片 For Each Shp In ActiveSheet.Shapes If Shp.Type = msoPicture Then Shp.Delete NextDim MyPcName As String For i = 4 To ThisWorkbook.ActiveSheet.UsedRange.Rows.Count If (ActiveSheet.Cells(i, 3).Value = "姓 名") Then MyPcName = ActiveSheet.Cells(i, 4).Value & ".jpg" ✀MsgBox "图片的完整路径是" & ThisWorkbook.Path & "员工照片" & MyPcName ActiveSheet.Cells(i, 9).Select ✀选择要插入图片的单元格作为目标 Dim MyFile As Object Set MyFile = CreateObject("Scripting.FileSystemObject") If MyFile.FileExists(ThisWorkbook.Path & "员工照片" & MyPcName) = False Then MsgBox ThisWorkbook.Path & "员工照片" & MyPcName & "图片不存在" Else ✀在选定的单元格中插入图片 ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "员工照片" & MyPcName).Select End If End If Next iEnd SubSub 删除照片()Dim p As Shape Set My = Worksheets(1) For Each p In My.Shapes If Not Application.Intersect(p.TopLeftCell, Range("I4:I6")) Is Nothing Then p.Delete NextEnd Sub
2026年09月25日 11:47
有2个网友回答
网友(1):

图片太小手机看不清 路径中文件夹之间应该有斜杠。这样改试试:If MyFile.FileExists(ThisWorkbook.Path & "/员工照片/" & MyPcName) = False Then 下面其他地方也要修改。

网友(2):

这个简单
你代码中的“员工照片”是一个文件夹吧?
那么,代码中有三处
& "员工照片" &
均应改为
& "员工照片\" &
你忘了写\了!!!

如果还是有问题,那么,需要看你的目录结构!