使用VBA插入图片-(不链接到它们)

时间:2019-03-11 14:44:00

标签: excel vba

我有一些VBA代码,可以将图像添加到电子表格中,这一切都很好,但是当我将其发送到外部时,由于其他用户不在我的网络上,链接显然不再起作用

我如何修改此代码以将实际图像插入电子表格中,而不是链接到电子表格

我知道在这种情况下excel文件可能会变大,但是我要插入的文件很小,只有20KB缩略图

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myPict As Picture
Dim PictureLoc As String

    On Error GoTo EH
    Application.EnableEvents = False
    If Target.Column = 1 Then

        'Pictures.Delete

        PictureLoc = "\\ca-sbs-01\t\Shared\ExcelImages\" & Target.Value2 & ".jpg"


        With Target.Offset(, 1)
            Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
            .RowHeight = myPict.Height
            myPict.Top = .Top
            myPict.Left = .Left
            myPict.Placement = xlMoveAndSize
        End With

    End If
EH:
    Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:1)

您可以像这样使用Shapes.Addpicture方法:

    Dim myPict as Shape
    With Target.Offset(, 1)
        Set myPict = Me.Shapes.AddPicture(Filename:=PictureLoc, linktofile:=msoFalse, savewithdocument:=msoCTrue, Left:=.Left, Top:=.Top, Width:=-1, Height:=-1)
    .RowHeight = myPict.Height
    End With
    myPict.Placement = xlMoveAndSize