使用VBA更改图片

Pla*_*Kid 11 vba image excel-2007 ms-office

当您在Excel/Word/Powerpoint中右键单击一个Shape时,我正在尝试使用VBA自动执行更改图片功能.

但是,我找不到任何参考,你能帮忙吗?

ric*_*nis 10

您可以使用应用于矩形形状的UserPicture方法更改图片的来源.但是,如果您希望保持图片的原始高宽比,则需要相应地调整矩形的大小,因为图片将采用矩形的尺寸.

举个例子:

 ActivePresentation.Slides(2).Shapes(shapeId).Fill.UserPicture ("C:\image.png")
Run Code Online (Sandbox Code Playgroud)

  • 从技术上讲,这会为形状添加填充,如果您的形状是图片,则填充将不可见. (4认同)

chr*_*sen 8

据我所知,你不能改变图片的来源,你需要删除旧图片并插入一个新图片

这是一个开始

strPic ="Picture Name"
Set shp = ws.Shapes(strPic)

'Capture properties of exisitng picture such as location and size
With shp
    t = .Top
    l = .Left
    h = .Height
    w = .Width
End With

ws.Shapes(strPic).Delete

Set shp = ws.Shapes.AddPicture("Y:\our\Picture\Path\And\File.Name", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic
shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
Run Code Online (Sandbox Code Playgroud)

  • 在删除旧图片之前将其属性记录在变量中,或者在复制其属性时将图片保留在原处,记录其 z 顺序,然后将其删除并将新图片移回原始图片的 z 顺序 (2认同)

小智 5

'change picture without change image size
Sub change_picture()
strPic = "Picture 1"
Set shp = Worksheets(1).Shapes(strPic)

'Capture properties of exisitng picture such as location and size
With shp
    t = .Top
    l = .Left
    h = .Height
    w = .Width
End With

Worksheets(1).Shapes(strPic).Delete

Set shp = Worksheets(1).Shapes.AddPicture("d:\pic\1.png", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic

End Sub
Run Code Online (Sandbox Code Playgroud)