Using VBA to change the image

I am trying to use VBA to automate the Change Picture function when you right-click on Shape in Excel / Word / Powerpoint.

However, I can not find any link, can you help?

+6
source share
8 answers

As far as I know, you cannot change the image source, you need to delete the old one and insert a new one

Here begins

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 
+7
source

You can change the image source using the UserPicture method as applied to the shape of the rectangle. However, you will need to resize the rectangle accordingly if you want to keep the original aspect ratio of the image, since the image will take the size of the rectangle.

As an example:

  ActivePresentation.Slides(2).Shapes(shapeId).Fill.UserPicture ("C:\image.png") 
+7
source

In Word 2010 VBA, this helps to change the .visible parameter for this image element that you want to change.

  • set .visible to false
  • change image
  • set the .visilbe parameter to true

who worked for me.

+1
source
 '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 
+1
source

What I did in the past is to create several image controls on the form and lay them on top of each other. Then you programmatically set all the images .visible = false, except for the ones you want to show.

0
source

what i am doing is lying on both images on top of each other and assigning a macro below for both images. Obviously, I named the images β€œlighton” and β€œlightoff,” so make sure you change them to your images.

 Sub lightonoff() If ActiveSheet.Shapes.Range(Array("lighton")).Visible = False Then ActiveSheet.Shapes.Range(Array("lighton")).Visible = True Else ActiveSheet.Shapes.Range(Array("lighton")).Visible = False End If End Sub 
0
source

I am using this code:

 Sub changePic(oshp As shape) Dim osld As Slide Set osld = oshp.Parent osld.Shapes("ltkGambar").Fill.UserPicture (ActivePresentation.Path & "\" & oshp.Name & ".png") End Sub 
0
source

I work in Excel and VBA. I cannot overlay images because I have several sheets with a variable number and there are images on each sheet, so the file will become huge if, say, 20 sheets have all 5 images that I want to animate.

So, I used a combination of these tricks listed here: 1) I inserted the RECTANGLE form in the location and size that I wanted:

 ActiveSheet.Shapes.AddShape(msoShapeRectangle, 1024#, 512#, 186#, 130#).Select Selection.Name = "SCOTS_WIZARD" With Selection.ShapeRange.Fill .Visible = msoTrue .UserPicture "G:\Users\ScotLouis\Documents\My Spreadsheets\WordFind Wizard\WordFind Wizard 1.jpg" .TextureTile = msoFalse End With 

2) Now, to animate (change) the image, I only need to change the Shape.Fill.UserPicture:

 ActiveSheet.Shapes("SCOTS_WIZARD").Fill.UserPicture _ "G:\Users\ScotLouis\Documents\My Spreadsheets\WordFind Wizard\WordFind Wizard 2.jpg" 

So, I achieved my goal of having only 1 image per sheet (not 5, as in my animation), and duplicating the sheet only duplicates the active image, so the animation continues without problems with the next image.

0
source

Source: https://habr.com/ru/post/913292/


All Articles