我正在尝试根据单元格值将图片插入到excel中。单元格值在图像路径中。我是新的,我所拥有的部分是基于录制宏,部分是从查找东西。这就是我试过的..。
我一直在ActiveSheet.Pictures.Insert行上收到一个错误
Sub Part_Picture()
'
' Part_Picture Macro
'
Dim imageName As String
Dim imageFolder As String
Dim imagePath As String
For Each Cell In Range("B7")
imageName = Cell.Value
imageFolder = "Q:\New Project Part Folders\Elizabeth Delgado\Database pictures\Part\" & imageName
imagePath = imageFolder & ".jpg"
Range("B11").Select
'
ActiveSheet.Pictures.Insert(imagePath).Select
Next Cell
End Sub发布于 2017-05-09 07:17:19
"Unable to get the insert property of the Pictures class“是一条一般性的错误消息,您也可以将其翻译为”您正在尝试做的事情出了问题,我无法给您更多信息“。不过,镜像文件的路径很可能没有正确构建。
1)从insert语句中删除.Select。从语法上讲,这是没有意义的。只需使用ActiveSheet.Pictures.Insert(imagePath)即可
2)检查单元格B7中的值是否仅为文件名,不包括扩展名。因为您的代码添加了".jpg“,所以在B7中不需要它。
3)检查文件实际上是jpg,而不是png。
4)检查文件/文件夹是否实际存在
仅供参考的For Each Cell In Range("B7")将只迭代一个单元格-- B7 --并且是不必要的。如果只打算读取一个单元格,则应该使用imageName = Range("B7").Value,或者更好的方法是使用imageName = Range("B7").Text,因为您需要字符串
发布于 2017-07-22 20:13:51
考虑这个选项。
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Application.ScreenUpdating = False
fPath = "C:\your_path_here\"
Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
i = 1
For Each r In rng
fName = Dir(fPath)
Do While fName <> ""
If fName = r.Value Then
With ActiveSheet.Pictures.Insert(fPath & fName)
.ShapeRange.LockAspectRatio = msoTrue
Set px = .ShapeRange
If .ShapeRange.Width > Rows(i).Columns(2).Width Then .ShapeRange.Width = Columns(2).Width
With Cells(i, 2)
px.Top = .Top
px.Left = .Left
.RowHeight = px.Height
End With
End With
End If
fName = Dir
Loop
i = i + 1
Next r
Application.ScreenUpdating = True
End Sub‘注意:您需要文件扩展名,例如',jpg',或者您正在使用的任何文件扩展名,因此您可以匹配该扩展名。
无论您放在A列中的图片名称是什么,都将被导入到B列中的相邻单元格中
发布于 2018-08-08 17:31:38
.Pictures.Insert("c:\fixedfile.png")要求将fix文件名作为其参数。但是,您可以使用FileCopy "desiredfile.png"," fixedfile.png“来替换fixedfile.png的内容,从而满足您的需求。
https://stackoverflow.com/questions/43856039
复制相似问题