Excel - 从工作表中的链接图像中提取路径信息

时间:2018-03-15 15:59:45

标签: excel excel-vba vba

全部, 我有一个Excel电子表格,其中包含一行图像,这些图像通过“插入\图片来自文件”弹出窗口添加到工作表中。我没有嵌入图像,而是选择链接到File的选项。我现在正在将表单移动到Access DB,但我无法弄清楚如何从图像行中提取每个链接图像的路径信息?

有谁知道我会如何做到这一点?任何帮助将不胜感激,提前感谢 - CES

2 个答案:

答案 0 :(得分:0)

通过Insert\Picture From File popup window在Excel中插入的图片不会保存为路径。它们“活在”Excel应用程序中。

为了将它们“移动”到Access DB,您必须将它们保存在某处,从而记住路径。根据{{​​3}}的答案,这是一种方法:

  • 将图片放入图表
  • 修正尺寸
  • 导出图表
  • 删除图表
Public Sub TestMe()

    Dim pic As Shape
    Dim chrt As Chart
    Dim cnt As Long
    Dim ws As Worksheet
    Dim myPath As String

    Application.ScreenUpdating = False
    myPath = "C:\path\"
    Set ws = Worksheets(1)

    For cnt = 1 To ws.Shapes.Count
        If InStr(ws.Shapes(cnt).Name, "") > 0 Then
            Set chrt = Charts.Add.Location(Where:=xlLocationAsObject, Name:=ws.Name)
            chrt.ChartArea.Width = ws.Shapes(cnt).Width
            chrt.ChartArea.Height = ws.Shapes(cnt).Height
            chrt.Parent.Border.LineStyle = 0

            ws.Shapes(cnt).Copy
            chrt.ChartArea.Select
            chrt.Paste
            chrt.Export Filename:=myPath & ws.Shapes(cnt).Name & ".jpg"
            ws.ChartObjects(ws.ChartObjects.Count).Delete
        End If
    Next cnt

    Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

这不是vba代码的答案,但它可以帮助您实现所需。

您可以获取链接位置,如果您的文件以excel压缩格式保存,我认为这仅来自excel 2010以后:

保存文件副本并将文件重命名为 filename .zip

提取Zip文件 。 浏览文件夹结构:\ xl \ drawings_rels \

在那里你应该找到参考文件,例如' drawing1.xml.rels '

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">

<Relationship TargetMode="External" Target="file:///C:\Users\exampleuser\Pictures\myimage.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId1"/>

从此处,您可以使用其ID

提取链接列表

导航到文件夹:\ xl \ drawings \并打开图纸xml文件,从这里你可以找到图像ID和它们的Excel名称,你也可以找出图像所在的行。其余的xml代码。

-<xdr:pic> 
-<xdr:nvPicPr>
<xdr:cNvPr name="Picture 3" id="4"/>
-<xdr:cNvPicPr>
<a:picLocks noChangeAspect="1"/>
</xdr:cNvPicPr>
</xdr:nvPicPr>
-<xdr:blipFill>
<a:blip r:link="rId1" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>
-<a:stretch>
<a:fillRect/>
</a:stretch>
</xdr:blipFill> 
</Relationships>

有了这些信息,您应该能够锻炼哪个图片链接到哪个文件及其位置。