【问题标题】:microsoft excel - how to fit image inside shape?microsoft excel - 如何将图像放入形状中?
【发布时间】:2018-07-07 12:25:56
【问题描述】:

我在一些单元格中输入了一些图像位置并将它们超链接。当我单击此单元格时,将执行一个宏并用这些单元格中指定的图片填充一个矩形形状。这是宏:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Row = ActiveCell.Row
col = ActiveCell.Column

ActiveSheet.Shapes.Range(Array("Rectangle 38")).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .UserPicture ActiveSheet.Cells(Row, col).Value
    End With

End Sub

它可以工作,但图片被拉伸了。我希望图片适合我的形状。在excel中,您可能知道,在用图片填充形状后,裁剪选项下有一个适合按钮。当您单击它时,它会适合图片框内的图像并保持形状的大小。我只想在 VBA 中做确切的事情。

【问题讨论】:

  • 设置尺寸以匹配?有高度,宽度等?

标签: vba excel ms-office


【解决方案1】:

使用 .PictureWidth , .PictureHeight , .PictureOffsetX = .PictureOffsetY. 的形状属性

代码示例:

Option Explicit
Public Sub AddPicAndAdjust()

    Dim shp As ShapeRange
    Set shp = ActiveSheet.Shapes.Range(Array("Rectangle 1"))
    With shp.Fill
        .Visible = msoTrue
        .UserPicture "C:\Users\User\Pictures\MyNicePic.png" '<== Add pic
        .TextureTile = msoFalse
        .RotateWithObject = msoTrue
    End With

    'Positioning within fill
    With shp.PictureFormat.Crop
        .PictureWidth = 231
        .PictureHeight = 134
        .PictureOffsetX = 50
        .PictureOffsetY = 28
    End With

    With shp
        .LockAspectRatio = msoFalse
        .IncrementLeft 2
    End With
End Sub

【讨论】:

  • 这不是我想要的。让我再解释一下。当您用图片填充形状时,您可以在用户界面中设置其他选项。在裁剪中您可以选择适合和填充选项,您可以选择左偏移、右偏移、上偏移和下偏移。我必须知道这个选项的 VBA 对应关系。
  • 现在给出的例子。
猜你喜欢
  • 2017-02-18
  • 2018-05-15
  • 2020-12-10
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-08
相关资源
最近更新 更多