Forum Discussion
Excel VBA Macro to insert picture and its filename in a cell
Hello everyone,
I am new to the Tech Community and to VBA as well, so I would like to greet everyone here.
I'm currently on a project involving insertion of many pictures one by one in a report (because the position of the pictures should be left on the user to specify). There are many reports to make, and I would like to make this a bit easier for my staff. I tried to learn on VBA as much as I could but it doesn't go as fast as I expected, and the project deadline is getting closer (like always) so I wanted to ask for help.
I am trying on my own to create a macro in VBA that does following things:
- On a push of a button prompts a user to select a picture file (could be .jpg .png)
- The location of the inserted picture should be right under the active cell - embed the picture in the document
- Compress the picture while inserting it (would be nice, but not mandatory)
- I need then the filename of the inserted picture (just the last three digits) displayed in the active cell, and the cell format changed to custom format with a "Foto:" prefix
- The aspect ratio of the picture should be kept, but the maximum width should be limited to, say, 300px
- I don't want to this by resizing the active cell, but in another way (if it is possible, of course)
Here is what I have managed to find on the web so far and adjust it to my needs (not everything, though):
Sub InsertPictures()
Dim PicList() As Variant
Dim PicFormat As String
Dim Rng As Range
Dim sShape As Shape
On Error Resume Next
PicList = Application.GetOpenFilename(PicFormat, Title:="Bild auswaehlen", MultiSelect:=True)
xColIndex = Application.ActiveCell.Column
If IsArray(PicList) Then
xRowIndex = Application.ActiveCell.Row
For lLoop = LBound(PicList) To UBound(PicList)
Set Rng = Cells(xRowIndex, xColIndex)
Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top + ActiveCell.Height, -1, -1)
xRowIndex = xRowIndex + 1
Next
End If
End Sub
It inserts the picture in the active cell with its original size and places the picture right under the active cell.
The remainder of the tasks is not yet solved... I get lost in the Objects of VBA, because I didn't do any programming until now.
If there is anyway to make this code simpler as it currently is, I am open for suggestions.
I appreciate every help.
Thanks in forward everyone.
-Uros
1 Reply
- AIL ANDCopper Contributor
Créer un CommandButton au niveau de la cellule où vous voulez insérer l'image.
Utiliser la commande suivante :
CommandButton1.Picture = LoadPicture("C:\" & Le Chemin et le nom du fichier de l'image .bmp) L'image sera insérée dans le CommandButton.
Ici il s'agit du CommandButton1.
Cordialement