【问题标题】:Embed Image to Excel Spreadsheet - VBA将图像嵌入到 Excel 电子表格 - VBA
【发布时间】:2011-11-15 00:19:37
【问题描述】:

我需要通过 Excel VBA 将图像嵌入到电子表格中,这样每当我重新定位我的 excel 文件时,图像仍会显示。我该怎么做?

【问题讨论】:

    标签: image excel embed vba


    【解决方案1】:

    此代码将在当前工作表上插入图像并将其定位在单元格 E10:

    Set oPic = Application.ActiveSheet.Shapes.AddPicture("d:\temp\mypic.jpg", False, True, 1, 1, 1, 1)
    oPic.ScaleHeight 1, True
    oPic.ScaleWidth 1, True
    
    oPic.Top = Range("E10").Top
    oPic.Left = Range("E10").Left
    

    【讨论】:

      【解决方案2】:

      您是否尝试过使用宏记录器?

      这就是它为我制作的:

      Sub Macro1()
      
        ActiveSheet.Pictures.Insert ("C:\mypicture.jpg")
      
      End Sub
      

      还有大量使用谷歌搜索词的信息:“使用 VBA Excel 插入图片”。以下代码取自ExcelTip感谢原作者 Erlandsen Data Consulting

      使用下面的宏,您可以在工作表的任何范围内插入图片,只要图片本身保留在其原始位置,它们就会保留。

      图片可以水平和/或垂直居中。

      Sub TestInsertPicture()
          InsertPicture "C:\FolderName\PictureFileName.gif", _
              Range("D10"), True, True
      End Sub
      
      Sub InsertPicture(PictureFileName As String, TargetCell As Range, _
          CenterH As Boolean, CenterV As Boolean)
          ' inserts a picture at the top left position of TargetCell
          ' the picture can be centered horizontally and/or vertically
          Dim p As Object, t As Double, l As Double, w As Double, h As Double
          If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
          If Dir(PictureFileName) = "" Then Exit Sub
          ' import picture
          Set p = ActiveSheet.Pictures.Insert(PictureFileName)
          ' determine positions
          With TargetCell
              t = .Top
              l = .Left
              If CenterH Then
                  w = .Offset(0, 1).Left - .Left
                  l = l + w / 2 - p.Width / 2
                  If l < 1 Then l = 1
              End If
              If CenterV Then
                  h = .Offset(1, 0).Top - .Top
                  t = t + h / 2 - p.Height / 2
                  If t < 1 Then t = 1
              End If
          End With
          ' position picture
          With p
              .Top = t
              .Left = l
          End With
          Set p = Nothing
      End Sub
      

      使用下面的宏,您可以插入图片并将它们适合工作表中的任何范围。

      Sub TestInsertPictureInRange()
          InsertPictureInRange "C:\FolderName\PictureFileName.gif", _
              Range("B5:D10")
      End Sub
      
      Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
          ' inserts a picture and resizes it to fit the TargetCells range
          Dim p As Object, t As Double, l As Double, w As Double, h As Double
          If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
          If Dir(PictureFileName) = "" Then Exit Sub
          ' import picture
          Set p = ActiveSheet.Pictures.Insert(PictureFileName)
          ' determine positions
          With TargetCells
              t = .Top
              l = .Left
              w = .Offset(0, .Columns.Count).Left - .Left
              h = .Offset(.Rows.Count, 0).Top - .Top
          End With
          ' position picture
          With p
              .Top = t
              .Left = l
              .Width = w
              .Height = h
          End With
          Set p = Nothing
      End Sub
      

      【讨论】:

      • 我们只是使用了相同的解决方案,但是一旦移动或删除了外部图像,它就不起作用了。
      • 那为什么不问我而不是投反对票!我很乐意为您提供更多代码...
      • 哦,我投了反对票,因为 OP 提到无法链接图片,因此可以移动 excel 文件,所以我认为这是对这个特定问题的错误答案。对不起,没有冒犯的意思~我现在才积极参与这个网站大约一个星期。也许下次我只会投票。
      • OP 要求当他重新定位他的 excel 文件 时,图像仍会显示。如果他说他希望在图像本身被删除后保留图像,那么你是对的,代码将不起作用,因为他可能需要使用形状。问题是 OP 没有制作任何 cmets 或标记解决方案,所以我们真的不知道。也许你可以阅读when to downvote.我已经编辑了我的帖子,所以如果你也选择的话,你可以删除反对票。
      • 如果我移动图像这不起作用,这是问题的特定要求。这个答案不好。
      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多