首页 > 解决方案 > 嵌入以前使用 Pictures.Insert 插入的图像

问题描述

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

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

所以,虽然我现在已经修复了模板,但已经设置好并填充了链接图像的各个文件夹中有几百个文件。

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

我咨询了我的 IT 联系人并在线搜索了此处和其他地方,但找不到解决方案。

请问有人有什么想法吗?

附上简单的样品。 保存在 Google Drive 上的 ImageLink Excel 文档


这是我用来插入图像的代码,如果有帮助的话。生成 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

标签: excelvbaimageembed

解决方案


您可以复制图像并用粘贴的副本替换它们:

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

推荐阅读