将图像插入嵌入而不是链接

时间:2017-11-20 16:52:01

标签: vba excel-vba excel

以下代码我通过SO和其他一些我加入的资源得到了一些帮助。通过一些小的改动,我可以将它与我的电子表格一起使用。

Sub AddPicture(l As Long, t As Long, w As Long, h As Long, aRatio As Boolean)

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Clear
        .Filters.Add "All Pictures", "*.*"

        If .Show = -1 Then
            Dim img As Picture
            Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))

            If (Not aRatio) Then
                img.ShapeRange.LockAspectRatio = msoFalse
            Else
                img.ShapeRange.LockAspectRatio = msoTrue
            End If
            img.left = l
            img.top = t
            img.width = w
            img.height = h
        Else
        End If
    End With

End Sub

我注意到有关此代码的一件事是图片被添加为Linked Image,这可能会导致一些问题......无论如何都要将这些问题添加为Embedded images代替?

1 个答案:

答案 0 :(得分:1)

将其添加到文件选择器的with块

  

FullPathName = .SelectedItems(1)

然后在brax的答案链接更改

  

文件名:= “C:\测试\ desert.jpg”

  

文件名:= FullPathName