【问题标题】:Insert Dynamically in Excel using image from server path使用服务器路径中的图像在 Excel 中动态插入
【发布时间】:2019-01-22 10:57:11
【问题描述】:

我有下面的代码在我们服务器的特定文件夹中查找 sku 的图像并插入 /autosize - 但我遇到的问题是,如果我将此电子表格发送给不在服务器上的其他人,他们看不到图片。 有人可以帮助解决这个问题,以便动态插入图像吗?我相信这是在工作表更新/打开时将实际图像放置在工作表中而不是链接回必须做的事情。 或者,如果图像未链接到服务器,我该如何格式化以发送并包含图像? 我查看了其他有关动态插入的帖子,但我无法正常工作

Sub Imageupdate()
' inserts the picture files listed in col A into the workbook,
' and sizes and centers in col B

Const sPath       As String = "S:\Images\Casio\"
'Const sPath       As String = "C:\Users\shg\Pictures\shg"
Dim cell          As Range
Dim sFile         As String
Dim oPic          As Picture

For Each cell In Range("A2", Cells(Rows.Count, "A").End(xlUp))
sFile = sPath & cell.Text & ".jpg"
If Len(Dir(sFile)) Then
  Set oPic = ActiveSheet.Pictures.Insert(sFile)
  oPic.ShapeRange.LockAspectRatio = msoTrue

  With cell.Offset(, 1)
    If oPic.Height > .Height Then oPic.Height = .Height
    If oPic.Width > .Width Then oPic.Width = .Width

    oPic.Top = .Top + .Height / 2 - oPic.Height / 2
    oPic.Left = .Left + .Width / 2 - oPic.Width / 2
  End With
Else
  cell.Select
  MsgBox sFile & " not found"
End If
Next cell
End Sub

【问题讨论】:

  • 我确实找到了 Dim xlApp 作为对象集 xlApp = CreateObject("Excel.Application") 所以我尝试了 Dim shpPic = xlApp.Shape 但这是我需要的吗?我会在调整图像大小的部分之后粘贴吗?
  • 您已经在 Excel 中 - 我不知道您为什么要使用 Set xlApp = CreateObject... 但无论如何,如果您使用的是 Shapes.AddPictureshpPic 只是一个 @ 987654327@ - Dim shpPic as Shape.
  • 谢谢。我真的是 VBA 的新手,你介意让我知道我应该如何在上面添加它的格式吗?

标签: excel vba image dynamic


【解决方案1】:

根据问题VBA to insert embedded picture 的两个答案,使用Shapes.AddPicture(或Shapes.AddPicture2,如果您想在插入时压缩图片。)

  • LinktoFilemsoFalse
  • SaveWithDocumentmsoTrue
  • WidthHeight 分别为-1 以保留图片的现有尺寸

Sub ImageUpdate()
' inserts the picture files listed in col A into the workbook,
' and sizes and centers in col B

Const sPath       As String = "S:\Images\Casio\"
'Const sPath       As String = "C:\Users\shg\Pictures\shg"
Dim cell          As Range
Dim sFile         As String
Dim shpPic        As Shape
Dim ws            As Worksheet: Set ws = ActiveSheet

With ws
    For Each cell In .Range(.Range("A2"), .Cells(.Rows.Count, "A").End(xlUp))
        sFile = sPath & cell.Text & ".jpg"

        If Len(Dir(sFile)) Then
            Set shpPic = .Shapes.AddPicture(sFile, msoFalse, msoTrue, 0, 0, -1, -1)
            shpPic.LockAspectRatio = msoTrue

            With cell.Offset(, 1)
                If shpPic.Height > .Height Then shpPic.Height = .Height
                If shpPic.Width > .Width Then shpPic.Width = .Width

                shpPic.Top = .Top + .Height / 2 - shpPic.Height / 2
                shpPic.Left = .Left + .Width / 2 - shpPic.Width / 2
            End With
        Else
            cell.Select
            MsgBox sFile & " not found"
        End If
    Next cell
End With

End Sub

【讨论】:

  • 很高兴为您提供帮助!如果有效,请随时单击旁边的复选标记将答案标记为已接受。
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 2012-05-10
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多