嵌入以前使用Pictures.Insert

时间:2018-07-18 16:07:01

标签: excel vba image embed

我已经建立了一个模板,可以帮助用户插入图像。

在VBA中,我使用了 Pictures.insert ,但是随后我发现,此方法默认情况下偷偷地将LINK插入图像,而不是嵌入图像。显然,这意味着未连接到我们网络的第三方无法查看它。

所以,尽管我现在已经修复了 templates ,但是已经设置了数百个文件,分布在各个文件夹中,并充满了链接的图像。

我想的是 我需要一个VBA脚本,该脚本将遍历所选文件夹系统中每个可见工作簿中的每个工作表,并将所有链接的图像“转换”为嵌入的图像(nb。手动插入,因此未链接)

我已经咨询了我的IT联系人,并在这里和其他地方在线搜索过,但是找不到解决方案。

请问有人有什么想法吗?

附加了简单的样本。 ImageLink Excel doc saved on Google Drive


这是我用来插入图像的代码,如果有帮助的话。生成URL的逻辑已在电子表格中处理:

Sub InsertImage()
' This script inserts the image file referenced by the active cell
' Via https://www.extendoffice.com/documents/excel/4212-excel-insert-image-from-url.html

Dim Rng As Range
Dim filenam As String

On Error Resume Next

Application.ScreenUpdating = False

Set Rng = Selection

Sheets("Sheet1").Range("activefile").Value = Rng.Value

filenam = Range("filenurl").Value

ActiveSheet.Pictures.Insert(filenam).Select

End Sub

1 个答案:

答案 0 :(得分:0)

您可以复制图像并将其替换为粘贴的副本:

Sub Macro1()

    Unlink ActiveSheet.Shapes(1)

End Sub


Sub Unlink(s)

    Dim t, l, h, w, ws
    With s
        Set ws = .DrawingObject.Parent
        t = .Top
        l = .Left
        h = .Height
        w = .Width
        .Copy
        'might need to adjust the paste format...
        ws.PasteSpecial Format:="Picture (PNG)", _
                   Link:=False, DisplayAsIcon:=False
    End With

    With ws.Shapes(ws.Shapes.Count)
        .Top = t
        .Left = l
        .Height = h
        .Width = w
    End With

End Sub