【发布时间】: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.AddPicture,shpPic只是一个 @ 987654327@ -Dim shpPic as Shape. -
谢谢。我真的是 VBA 的新手,你介意让我知道我应该如何在上面添加它的格式吗?