如何使用VBA从Excel文件保存图像?

时间:2017-03-15 22:07:39

标签: excel vba excel-vba

我有一个Excel文件,其中包含一个列,其中的图像对应于不同的唯一ID。基本上,通过VBA,我想循环遍历每个图像,并将其名称保存为唯一ID。

我意识到你无法在Excel中保存图像,所以我发现这个VBA代码在线(见下文)将图像复制到PowerPoint中并将其保存在那里,但它对我不起作用。我正在使用Excel 2016,64位。

有什么建议吗?

Sub SaveImages()
    'the location to save all the images
    Const destFolder = "C:\Desktop\Images"

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("data")

    Dim ppt As Object, ps As Variant, slide As Variant

    Set ppt = CreateObject("PowerPoint.application")
    Set ps = ppt.presentations.Add
    Set slide = ps.slides.Add(1, 1)

    Dim shp As Shape, shpName
    For Each shp In ws.Shapes
        shpName = destFolder & shp.TopLeftCell.Offset(0, 1) & ".png"
        shp.Copy
        With slide
            .Shapes.Paste
            'This is the point where the code breaks, when I try to save
            .Shapes.SaveAs Filename:=destFolder & shpName
            .Shapes(.Shapes.Count).Delete
        End With
    Next shp

    With ps
        .Saved = True
        .Close
    End With
    ppt.Quit
    Set ppt = Nothing
End Sub

0 个答案:

没有答案