Excel宏放置图像,但当其他人打开工作簿时,这些图像不会显示



我一直在埋头苦干,试图拼命拼凑一个巨大的宏从我可以在互联网上找到我的工作。最终目的是格式化报告。

这部分代码从单元格中获取值,并在给定文件夹中找到分别命名的图像文件,然后将图像"插入"到某个单元格中。(我知道这不是技术上的插入,但仍然。)

问题是其他人需要查看这些报表,但是当我将工作簿发送给他们时,图像没有显示。我不知道如何纠正这一点,这是一个大问题。我恳求你,请帮我想出一个方法来做到这一点,让其他员工将能够看到的图像!我的饭碗可能就靠它了!(

Dim pictureNameColumn As String
Dim picturePasteColumn As String
Dim pictureName As String
Dim lastPictureRow As Long
Dim pictureRow As Long
Dim pathForPicture As String
pictureNameColumn = "A"
picturePasteColumn = "B"
pictureRow = 4
lastPictureRow = Cells(Rows.Count, pictureNameColumn).End(xlUp).Row
pathForPicture = "C:Usersdesidreportimages" 
Do While (pictureRow <= lastPictureRow)
pictureName = Cells(pictureRow, "A")
If (pictureName <> vbNullString) Then
If (Dir(pathForPicture & pictureName & ".jpg") <> vbNullString) Then
Cells(pictureRow, picturePasteColumn).Select
ActiveSheet.Pictures.Insert(pathForPicture & pictureName & ".jpg").Select
With Selection
.Left = Cells(pictureRow, picturePasteColumn).Left + 30
.Top = Cells(pictureRow, picturePasteColumn).Top + 3
.ShapeRange.LockAspectRatio = msoTrue 
.ShapeRange.Height = 90#
.ShapeRange.Width = 90#
.ShapeRange.Rotation = 0#
End With

不使用ActiveSheet.Pictures.Insert插入图片,而是尝试使用此方法嵌入。还请注意,单元格不接受字母作为列名,它需要列数,因此"A"变为1:

Dim repPic as Shape
Dim pictureNameColumn As Long
Dim picturePasteColumn As Long
Dim Lft as Single
Dim Tp as Single
Dim Wdth as Single
Dim Hgth as Single
pictureNameColumn = 1
picturePasteColumn = 2
Lft = Cells(pictureRow, picturePasteColumn).Left + 30
Tp = Cells(pictureRow, picturePasteColumn).Top + 3
Wdth = Cells(pictureRow, picturePasteColumn).Width
Hgth = Cells(pictureRow, picturePasteColumn).Height
Set repPic = Application.ActiveSheet.Shapes.AddPicture(pathForPicture & pictureName & ".jpg", False, True, Lft,Tp,Wdth,Hgth)

这将允许您将图片保存在文件本身。您必须弄清楚如何使用宽度和长度计算出图片的大小,因为使用这种方法,您必须在插入图片时指定宽度和高度。我建议的解决方案在代码中,但它可能不适用于您的设置。

希望这有助于,如果它确实请标记为接受的答案。好运!

如果人们必须运行宏,那么这就是问题的来源:如果filePath无法访问,If (Dir(pathForPicture & pictureName & ".jpg") <> vbNullString)将返回False。试着找一个公用文件夹来放你的照片。我没有办法解决你的问题,但至少现在你知道问题出在哪里了。

最新更新