VBA 插入并链接图片

huangapple go评论100阅读模式
英文:

VBA Insert and Link Pictures

问题

我想要做的是将图片插入Excel工作表中。由于有很多图片,我想使用VBA子程序来实现这个目标。这些图片定期更新(每次都会更改的图表,获取新的数据点)。每张图片还应该分配一个准备好的子程序。

项目/子程序的当前状态如下:

  1. 使用以下代码将图片插入Excel工作表中:
ActiveSheet.Pictures.Insert(path as String)
  1. 使用以下代码迭代所有形状,并为每张图片分配准备好的子程序ImageClick:
ActiveSheet.Shapes(i as Integer).OnAction = "ImageClick"
  1. 为了保持图片的更新,每次打开工作簿时都会调用构建工作表并插入所有图片的宏。

这个方法目前运行良好,但我认为这可能不是最终解决方案。随着图片数量的增加,运行时间可能会超出可接受的范围。

我的问题是,是否有任何函数或选项可以自动插入和链接图片(就像您在手动插入图片到Excel时可以选择的选项一样),以确保由于与源的链接而始终保持图片是最新的?

另一个问题是,是否可能由于链接的原因而无法将子程序ImageClick分配给这些图片?

英文:

What I want to do, is to insert pictures into an excel worksheet. Because there are a lot of pictures, I want to do this using a VBA Sub. These pictures are updated on a regular basis (Graphs that change every time, new data points are obtained). Every Picture should also have a prepared Sub assigned to it.
The current state of the project/Sub is as following:

  1. The Pictures are inserted into the excel worksheet using<br>ActiveSheet.Pictures.Insert(path as String)
  2. The prepared Sub ImageClick is assigned to each picture by iterating over all Shapes and using<br> ActiveSheet.Shapes(i as Integer).OnAction = &quot;ImageClick&quot;
  3. To keep the pictures up to date, the macro, which builds up the worksheet and inserts all the pictures is called everytime, the workbook is being opened.

This works just fine at this point, but I guess, that this could not be the final solution. With a growing amount of pictures, the runtime might exceed the acceptable values.

My question is, if there is any function or option to automatically Insert and Link the pictures (like the option you can choose, when you manually insert pictures into excel), to ensure, that the pictures are always up to date because of the link to the source?

Another question is, whether there might be a problem with assigning the Sub ImageClick to these pictures (because of the link)?

答案1

得分: 0

我找到了解决这个问题的方法。
通过使用函数Shapes.AddPicture,您可以启用与源文件的链接。参数LinkToFile必须设置为msoTrueSaveWithDocument必须设置为msoFalse,以确保在再次打开工作簿时自动更新图片。因为所有参数都是必需的,所以还必须定义图片的确切位置。

还可以使用Shape.OnAction将宏分配给图片 = &lt;Sub_or_Function_Name as String&gt;

以下是示例代码:

'向Excel工作表插入链接图片的子例程
Public Sub LoadPicture()
    Dim path As String
    Dim ws As Worksheet
    Dim shc As Shape
    
    Set ws = ActiveWorkbook.Sheets(1)
    
    path = &lt;Filepath as String&gt;
    
    '所有参数都是必需的
    Set shc = ws.Shapes.AddPicture(Filename:=path, LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, _
                                Left:=100, Top:=100, Width:=100, Height:=100)

    '将准备好的子例程或函数分配给插入的形状。
    shc.OnAction = "Click"
    
End Sub

________________________________________________________________________________

Private Sub Click()
    MsgBox "你敢点击我吗?"
End Sub
英文:

I found a solution for this problem.
By usage of the function Shapes.AddPicture you can enable a link to the source file. The parameter LinkToFile must be set to msoTrue and SaveWithDocument must be msoFalse to ensure the automatic updating of the pictures (when the workbook is opened again). Because all parameters are necessary, also the exact position of the picture has to be defined.

Assigning macros to the pictures is also possible with Shape.OnAction = &lt;Sub_or_Function_Name as String&gt;.

An example code is provided below:

&#39;Sub to insert a linked picture into a Excel Worksheet
Public Sub LoadPicture()
    Dim path As String
    Dim ws As Worksheet
    Dim shc As Shape
    
    Set ws = ActiveWorkbook.Sheets(1)
    
    path = &lt;Filepath as String&gt;
    
    &#39;All parameter are necessary
    Set shc = ws.Shapes.AddPicture(Filename:=path, LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, _
                                Left:=100, Top:=100, Width:=100, Height:=100)

    &#39;Assign a prepared Sub or Function to the inserted Shape.
    shc.OnAction = &quot;Click&quot;
    
End Sub

_________________________________________________________________________________

Private Sub Click()
    MsgBox &quot;You dare to click me?&quot;
End Sub

huangapple
  • 本文由 发表于 2020年1月6日 18:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定