I am having issues with inserting a picture in portrait mode using VBA. If the picture is in landscape mode, then the picture is inserted into the associated shape in column B. However, if the picture is in portrait, then the picture is offset 25 columns to column AA. Any help is greatly appreciated!
Sub cmdInsert1_Click()
Dim myPicture As String, MyObj As Object
Range("b5").Select
myPicture = Application.GetOpenFilename("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub
Set MyObj = ActiveSheet.Shapes.AddPicture(myPicture, False, True, Range("B5").Left, Range("B5").Top, -1, -1)
With MyObj
.Height = 293
.Locked = False
End With
Set MyObj = Nothing
End Sub
Windows "remembers" the orientation of the picture, so what is in the Top Left corner of your screen may not actually be the Top Left corner of the picture. You will need to check the .Rotation property of your picture. If it's rotated (ie, not 0), you will need to adjust your code accordingly. For example:
If myObj.Rotation = 0 Or myObj.Rotation = 180 Then
.Height = 293
Else
.Width = 293
End If
Edit: Forgot to account for it merely being upside down
I'm trying to export a picture from a PictureBox but the problem is with the height of the exported picture (the width working perfectly).
I also found out that VB6 border have major effect to the size of the exported picture so I set it to 0.
Just open vb6 drop a PictureBox (and rename it to myPic)...
This is my code :
Option Explicit
Private Sub Form_Load()
myPic.AutoRedraw = True
myPic.BorderStyle = 0
myPic.Appearance = 0
myPic.Width = 100 * Screen.TwipsPerPixelX 'WORKING PERFECTLY!!!
myPic.Height = 100 * Screen.TwipsPerPixelY 'NOT RETURN 100px !!! Why ? 93px instead
myPic.ScaleMode = vbPixels
myPic.PaintPicture LoadPicture(App.Path & "\Source.bmp"), 0, 0, 100, 100
myPic.Picture = myPic.Image
SavePicture myPic.Picture, App.Path & "\Exported.bmp"
End Sub
Any Idea ?
Thanks in advance !
I tried your code, and it works as I think you intended: it shows source.bmp in a 100x100 picturebox and exports it into a bmp of 100x100 pixels
The code I uses is:
Option Explicit
Private Sub Form_Load()
With Picture1
.AutoRedraw = True
.ScaleMode = vbPixels
.BorderStyle = 0
.Appearance = 0
.Width = 100 * Screen.TwipsPerPixelX 'WORKING PERFECTLY!!!
.Height = 100 * Screen.TwipsPerPixelY 'NOT RETURN 100px !!! Why ? 93px instead
.PaintPicture LoadPicture("c:\temp\Source.bmp"), 0, 0, 100, 100
.Picture = .Image
SavePicture .Picture, "c:\temp\Exported.bmp"
End With 'Picture1
End Sub
I just put the .Scalemode to the top as I think that is where it belongs, but your code also works with .Scalemode where you had it.
Could you please add the picture you are trying to rescale?
For the source I used the picture below, which is 300x300 pixels:
And the result was:
Good day all! I have a very tricky for me question.
In my application I have button with image inside. All properties of this button:
Me.cmdSelectAll.BackColor = System.Drawing.SystemColors.Control
Me.cmdSelectAll.Image = CType(resources.GetObject("cmdSelectAll.BackgroundImage"), System.Drawing.Image)
Me.cmdSelectAll.ImageAlign = Drawing.ContentAlignment.BottomRight
Me.cmdSelectAll.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdSelectAll.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdSelectAll.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdSelectAll.Location = New System.Drawing.Point(0, 282)
Me.cmdSelectAll.Name = "cmdSelectAll"
Me.cmdSelectAll.Padding = New System.Windows.Forms.Padding(0, 0, 0, 0)
Me.cmdSelectAll.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdSelectAll.Size = New System.Drawing.Size(22, 22)
Me.cmdSelectAll.TabIndex = 11
Me.cmdSelectAll.TabStop = False
Me.ToolTip1.SetToolTip(Me.cmdSelectAll, "Select All Channels")
Me.cmdSelectAll.UseVisualStyleBackColor = False
When I run it with default text size of win7, the image appears in the center, it's all ok. But when I set some custom value of text size (115%) my image suddenly goes more right and down. It occurs with all of my button's images. Could you please answer me why does this happen and how can i fix this issue? Thanks
I have a workbook that has 54 sheets. "Master" Totals" and "Week1" through "Week52"
I am trying to insert an image from a file to a cell on the sheets "Week1" through to "Week52".
I have tried many codes and am able to get the image placed and sized correctly
the codes below both placed the image and I was able to manipulate them to get the image in the right spot and the right size.
I can't however make them run through the other sheets (Week1 through Week52)
Set oPic = Application.ActiveSheet.Shapes.AddPicture("C:\Users\Public\Documents\Cranes\MinerPic.wmf", False, True, 1, 1, 1, 1)
oPic.ScaleHeight 0.3, True
oPic.ScaleWidth 0.3, True
oPic.Top = Range("p2").Top
oPic.Left = Range("p2").Left
.OnAction = "FC4.xlsm!MineSheet"
or
pPath = "C:\Users\Public\Documents\Cranes\MinerPic.wmf"
With ActiveSheet.Pictures.Insert(pPath)
.Left = Range("p2").Left
.Top = Range("p2").Top
.ShapeRange.Height = 50
.ShapeRange.Width = 50
.OnAction = "FC4.xlsm!MineSheet"
At one stage I was able to place 52 images on top of each other. I suspect this has something to do with the Activesheet command.
I am extremely new to VBA and would appreciate any help.
Thanks in advance.
Steve.
Wrap your code like this
For i = 1 To 52
Set sh = ActiveWorkbook.Worksheets("Week" & i)
' Reference the sh object rather than ActiveSheet
Set oPic = sh.Shapes.AddPicture( ...
' or
With sh.Pictures.Insert(pPath)
' rest of your code
Next
I am trying to use VBA to automate the Change Picture function when you right click a Shape in Excel/Word/Powerpoint.
However, I am not able to find any reference, can you assist?
So far as I know you can't change the source of a picture, you need to delete the old one and insert a new one
Here's a start
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
You can change the source of a picture using the UserPicture method as applied to a rectangle shape. However, you will need to resize the rectangle accordingly if you wish to maintain the picture's original aspect ratio, as the picture will take the dimensions of the rectangle.
As an example:
ActivePresentation.Slides(2).Shapes(shapeId).Fill.UserPicture ("C:\image.png")
'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
what I do is lay both images on top of eachother, and assign the macro below to both images. Obviously i've named the images "lighton" and "lightoff", so make sure you change that 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
What I've done in the past is create several image controls on the form and lay them on top of each other. Then you programmatically set all images .visible = false except the one you want to show.
In Word 2010 VBA it helps to change the .visible option for that picture element you want to change.
set the .visible to false
change the picture
set the .visilbe to true
that worked for me.
I tried to imitate the original function of 'Change Picture' with VBA in PowerPoinT(PPT)
The code below tries to recover following properties of the original picture:
- .Left, .Top, .Width, .Height
- zOrder
- Shape Name
- HyperLink/ Action Settings
- Animation Effects
Option Explicit
Sub ChangePicture()
Dim sld As Slide
Dim pic As Shape, shp As Shape
Dim x As Single, y As Single, w As Single, h As Single
Dim PrevName As String
Dim z As Long
Dim actions As ActionSettings
Dim HasAnim As Boolean
Dim PictureFile As String
Dim i As Long
On Error GoTo ErrExit:
If ActiveWindow.Selection.Type <> ppSelectionShapes Then MsgBox "Select a picture first": Exit Sub
Set pic = ActiveWindow.Selection.ShapeRange(1)
On Error GoTo 0
'Open FileDialog
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Picture File", "*.emf;*.jpg;*.png;*.gif;*.bmp"
.InitialFileName = ActivePresentation.Path & "\"
If .Show Then PictureFile = .SelectedItems(1) Else Exit Sub
End With
'save some properties of the original picture
x = pic.Left
y = pic.Top
w = pic.Width
h = pic.Height
PrevName = pic.Name
z = pic.ZOrderPosition
Set actions = pic.ActionSettings 'Hyperlink and action settings
Set sld = pic.Parent
If Not sld.TimeLine.MainSequence.FindFirstAnimationFor(pic) Is Nothing Then
pic.PickupAnimation 'animation effect <- only supported in ver 2010 above
HasAnim = True
End If
'insert new picture on the slide
Set shp = sld.Shapes.AddPicture(PictureFile, False, True, x, y)
'recover original property
With shp
.Name = "Copied_ " & PrevName
.LockAspectRatio = False
.Width = w
.Height = h
If HasAnim Then .ApplyAnimation 'recover animation effects
'recover shape order
.ZOrder msoSendToBack
While .ZOrderPosition < z
.ZOrder msoBringForward
Wend
'recover actions
For i = 1 To actions.Count
.ActionSettings(i).action = actions(i).action
.ActionSettings(i).Run = actions(i).Run
.ActionSettings(i).Hyperlink.Address = actions(i).Hyperlink.Address
.ActionSettings(i).Hyperlink.SubAddress = actions(i).Hyperlink.SubAddress
Next i
End With
'delete the old one
pic.Delete
shp.Name = Mid(shp.Name, 8) 'recover name
ErrExit:
Set shp = Nothing
Set pic = Nothing
Set sld = Nothing
End Sub
How to use:
I suggest you to add this macro into the Quick Access Toolbar list.
(Goto Option or Right-click on the Ribbon menu))
First, select a Picture on the slide which you want to change.
Then, if the FileDialog window opens, choose a new picture.
It's done. By using this method, you can bypass the 'Bing Search and One-Drive Window' in ver 2016 when you want to change a picture.
In the code, there might(or should) be some mistakes or something missing.
I'd appreciate it if somebody or any moderator correct those errors in the code.
But mostly, I found that it works fine.
Also, I admit that there are still more properties of the original shape to recover - like the line property of the shape, transparency, pictureformat and so on.
I think this can be a beginning for people who want to duplicate those TOO MANY properties of a shape.
I hope this is helpful to somebody.
i use 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
I'm working in Excel and VBA. I can't overlay images because I have multiple sheets of a variable number and each sheet has the images, so the file would get huge if, say 20 sheets had all 5 images I want to animate.
So I used a combination of these tricks listed here:
1) I inserted an RECTANGLE shape at the location and size 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 picture, 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've accomplished my goal of only having 1 picture per sheet (not 5 as in my animation) and duplicating the sheet only duplicates the active picture, so the animation continues seamlessly with the next picture.
![Please find attached code.
First create a shape in PPT and run the code]1