Excelmacros放置图像,但在其他人打开工作簿时不显示

我一直在奴役,拼命拼凑出一个巨大的工作,从我在互联网上find的工作。 目的是最终格式化报告。

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

问题是其他人需要查看这些报告,但是当我将工作簿发送给他们时,图像不显示。 我不知道如何纠正这一点,这是一个很大的问题。 我正在开始,请帮助我找出一个办法,让其他员工能够看到图像! 我的工作可能取决于它! 🙁

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:\Users\desid\reportimages\" 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 ,尝试embedded它与此方法。 另请注意,单元格不接受字母作为列名称,它需要列数,以便“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) 

这将使您可以将图片保存在文件本身中。 你必须弄清楚如何用wdth和hgth来计算图片的大小,因为用这种方法你必须在插入图片的时候指定宽度和高度。 我build议的解决scheme是在代码中,但它可能无法用于您的设置。

希望这有助于,如果确实如此,请将答案标记为已接受。 祝你好运!

如果人们不得不运行macros,那么问题就来自这个问题: If (Dir(pathForPicture & pictureName & ".jpg") <> vbNullString)不能访问filePath,则If (Dir(pathForPicture & pictureName & ".jpg") <> vbNullString)将返回False 。 尝试find一个公共文件夹来把你的照片。我没有解决你的问题,但至less现在你知道问题来自哪里。