How to Copy Paste Userform Controls to a Worksheet? - excel

Please, can someone help me!
I have a userform which is called Configurator or "Config", which has some Image-Controls.
These images together look like a door, because the userform is a configurator for doors.
(where you can change width, height, material etc...)
Mostly the "door" consists of 4 different images which make the doorframe.
Now i want to use these images on my excel-worksheet, so the user sees a picture of the door on
the worksheet too.
Which method is the best way?
I tried to copy & paste the images from userform to a excel worksheet, but i dont know how.
I hope someone of you can show me the right way to do this!
Another way could be to somehow make a screenshot of a specific area of the userform
and paste the image to the worksheet. I dont know if there is a way to do this?
Thanks for any help!

Please, try the next way:
Let us say that pictDoor1 is the name of the picture you need to 'copy' and a new button to copy it (copyPicture). Copy next code in its Click event:
Private Sub copyPicture_Click()
Dim shPict As MSForms.Image, pic As MSForms.Image
Set pic = Me.pictDoor1 'use here the real name of the Image control
Set shPict = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Image.1", link:=False, _
DisplayAsIcon:=False, left:=20, top:=20, width:=pic.width, height:=pic.height).Object
shPict.Picture = pic.Picture
End Sub
It will insert an Image control (as the picture to be copied dimensions, cut you can change them by multiplying, if need bigger ones) and place on it the necessary picture.
Then, use the newly created shape top and left according to the previously created width and height to appropriately position them...

Related

How to copy text contained in a cell to clipboard

I'm very new to this, so please bear with me.
Background info: I'm trying to create my own quickparts / textblock tool so to speak.
In order to achieve this I have created multiple "buttons" that i want to assign to macros.
The desired result is, that you only have to click the button (named "holidays" for instance) and the corresponding explanatory text is copied into your clipboard.
The problem is that the corresponding text passages are often long and I don't want them included in the code as is the case at the moment where I use this:
Sub Holidays()
Dim obj As New DataObject
Dim txt As String
txt = "Holidays must be requested minimum 2 weeks prior"
obj.SetText txt
obj.PutInClipboard
Beep
End Sub
Instead of having the explanatory text in the code I would like to have it stored in a cell in a different sheet and it to be accessed and copied into the clipboard.
What I have tried so far is putting the text in a cell and named it [Holidays].
I can also set it as a message box, but can't seem to figure out how to get it into the clipboard.
Can anyone help me out with this?
Thanks a lot!

VBA Button Application.Caller returning wrong row

I try to use the following command on several buttons in different worksheets of myexcel workbook.
MsgBox (ActiveSheet.Shapes(Application.Caller).TopLeftCell.row)
When copying those buttons to another area of my workbook, I sometimes have the problem that still the row number of the original button location is displayed. As example in the screenshot below, I click the button at the bottom, but it return row 705 instead of 739. Can anyone explain this behaviour?
Make sure all your buttons have unique names. Somtimes it can happen that copied buttons have the same name (due to a bug in Excel). Then VBA cannot distinguish them and uses the first one that it finds. Check all your button names and make sure they are unique.
This issue can be reproduced easily:
Open a new workbook
Add a button (FormControl)
Name it MyButton
Copy that button
Paste it somewhere else in the same sheet
Use the code from the question for both buttons
The button will now always show the row of the first button, because both have the exact same name.
Image 1: Illustration how this issue can occur. In the end both buttons show the same row value. Because they have the same name VBA only can find the first one.
The soulution is, when ever you copy a button immediately make sure you rename it to a unique name.
It never happened to me to copy a shape with the same name. Only its Caption remained... But, since people says it is possible, try assigning this code to all the shapes in discussion, please. If the shape double name would be the reason, the code will return twice:
Sub callButName()
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
If sh.Name = Application.Caller Then MsgBox sh.TopLeftCell.Address
Next
End Sub

Inserting and Linking a Picture into a Shape Fill using VBA

I am trying to link and insert a picture (*.png) into a shapes fill in powerpoint using vba in Excel. In the end I will loop through this on 100+ pages and the pictures will be updated frequently so automating this will be a huge time saver. Currently I have figured out how to loop through the pages and insert the pictures into the shapes, but I have been unable to figure out how to link the pictures too.
I'm using the below code to fill the shape with the picture but I can't find the syntax to both insert and link it:
Pres.Slides(1).Shapes(ShapeName).Fill.UserPicture PictureFilePath
Ultimately this should behave like clicking on a shape, going format > shape fill > picture > insert and link (On the drop down next to insert in the dialog box).
Not all user interface actions are in the VBA object model. One of those exceptions is creating a link to a shape fill. The closest you can get is to link pictures that are inserted as pictures rather than as fills. Here's the syntax to add a linked picture. It assumes the picture is in the same folder as the presentation:
Sub Macro1()
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="Picture1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, Left:=300, Top:=251, Width:=121, Height:=38).Select
End Sub
Welcome to SO.
For answering your question, I'll give some credit to this similar SO question based on Excel and this similar SO question based on PP. Some additional information was gathered from the microsoft documentation on the subject.
What you seem to be looking for is the ActionSetting object in the shapes class, which seems to be availible to shapes in PowerPoint. Below is a code snippet created directly in PowerPoint VBA
Sub insertPictureIntoShapeWithLinkToPicture()
Dim PP_Slide As Slide, PP_Shape As Shape, imagePath As String
Set PP_Slide = ActivePresentation.Slides(1) 'Set slide (change as necessary)
Set PP_Shape = PP_Slide.Shapes(1) 'Set shape (change as necessary)
imagePath = "Path to image"
With PP_Shape
'add picutre
.Fill.UserPicture imagePath
'Set an action on click
With .ActionSettings(ppMouseClick)
'Set action to hyperlink
.Action = ppActionHyperlink
'Specify address
.Hyperlink.Address = imagePath
End With
End With
End Sub
The same approach should be available via Excel, either adding a reference to the PowerPoint object library, or using Late-Binding methods. Note that the 'click' method with hyperlink defaults to standard hyperlink clicking (ctrl + left mouse for windows with a Danish keyboard).
Please find attached code.
First create a shape in PPT and run the code.

Have .jpg files within excel when using ActiveX Image Control

I have an excel file with several toggle buttons. When a toggle button is on and the "calculate" command button is pressed, my activeX image frame changes. However, I have all these files in the same directory so I'd have to send the file to my employees zipped. Is there anyway to like maybe load them into the excel workbook on a hidden sheet?
Thank you
I think the LoadPicture() function will look for a system file so you don't want that.
Add the pictures into images on a worksheet. This way they will be in the workbook.
Then right click the image you just added and select properties. Select picture property and navigate to your image file.
You can change the name to make sense of your pictures also. So they don't have to be Image1 Image2 etc.
Then in your code set the picture that you want to change = the image that you want.
If something
Image1.Picture = Image2.Picture
Else
Image1.Picture = Image3.Picture
End If
Here Image1 is the picture that changes based on what happens when the calculate button is pressed. Image3 is one of the images that you loaded into the workbook.
If you are going to store them on some other worksheet you may need to declare a worksheet object and set it to that sheet
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("ImageWorksheet")
Image1.Picture = ws.Image3.Picture
or something like this may work.
ActiveWorkbook.Sheets("ImageWorksheet").Image3.Picture
Something like that.

Is there any way to select a cell by clicking the object above it?

I have a protected sheet with many objects over it. Is there any way I can select a cell by clicking the object directly above it? In other words, a way to click "through" the object onto the cell underneath it?
Considering there are no protection (otherwise it would make this suggestion to long and to complicated) you could:
1) create this simple macro in any standard module in VBA project:
Sub left_top_cell()
ActiveSheet.Shapes(Application.Caller).TopLeftCell.Select
End Sub
2) set the action of you picture (right click and something like 'assign a macro') and choose on the list our macro left_top_cell.
3) as a result you'll get selected cell which is below top-left corner of your shape. If needed you could change into bottom-right.
Important! tried and tested for pictures according to your comment.
it seems to be very simple. I've tried it. I don't know if it applies to your situation.
1) in Design mode, double click on this picture
2) it takes you to eg: - Private Sub picture1_Click()
End Sub
3) just type between Private Sub and End Sub - range("A1").select - you can mention the address of any cell that you want, then the rest of your code can follow.
this will select the cell as soon as you click the picture and then run rest of the code.
Hope it helps.....

Resources