Forum Discussion
Gstg72
Dec 02, 2020Copper Contributor
VBA Excel Macro - Picture attached to Cell (insert) - Not link
I'm looking to simply run a macro that finds a picture's location in one cell and then insert the picture into another cell. But it has to be attached to a cell so it can be sorted AND be an actual p...
audeser
May 14, 2021Copper Contributor
Refactoring code from Nikolino, you can have some more options, although given we don't know what you wanna do with the pictures later, it will lack some functionality you are looking after (the sort thing...).
Sub insertPicture()
Dim owsh As Excel.Worksheet
Dim rng As Excel.Range
Dim oShp As Excel.Shape
Dim strFile As String
strFile = Application.GetOpenFilename("Graphic files (*.jpg; *.gif; *.png)," & "*.jpg; *.gif; *.png")
If strFile <> CStr(False) Then
Set owsh = ActiveSheet
With owsh
On Error Resume Next
Set rng = Application.InputBox("Select target cell:", "Insert Picture", ActiveCell.Address, Type:=8)
On Error GoTo 0
If Not rng Is Nothing Then
Set oShp = .Shapes.AddShape(msoShapeRectangle, rng(1, 1).Left, rng(1, 1).Top, rng(1, 1).Width, rng(1, 1).Height)
With oShp
'!!!!! alternative
.Visible = msoFalse
With .Fill
.Visible = msoTrue
.UserPicture strFile
.TextureTile = msoFalse
End With
With .TextFrame2.TextRange.Characters
With .Font
.Size = 11
With .Fill
.Visible = msoTrue
With .ForeColor
.ObjectThemeColor = msoThemeColorLight1
.TintAndShade = 0
.Brightness = 0
End With
'!!!!! alternative
.Transparency = 1 ' ranges({0 = solid} to {1 = hidden})
.Solid
End With
End With
With .ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignLeft
End With
.Text = strFile
End With
'!!!!! alternative
'.AlternativeText = strFile
End With
'!!!!! alternative
'.Hyperlinks.Add Anchor:=oShp, Address:=strFile
rng(1, 1).FormulaR1C1 = strFile
End If
End With
End If
Set rng = Nothing
Set oShp = Nothing
Set owsh = Nothing
End Sub
As you can see, the picture is "inserted" in the workbook, so it can be referred. I have hidden the image inserted
You have some options to continue with the sort issue, as the shapes will be not linked to the cells (so they will not sort):
- Use the inserted cell as a sorting index
- Use an inside text as reference (you can hide the text with the transparency value)
- Use the AlternativeText as reference (commented code)
- Use the Hyperlink (commented code)
- ... others to explore
All the alternatives you have are marked with the '!!!!! alternative tag, so you can choose which one better fits your needings.
Kind regards
audeser
May 14, 2021Copper Contributor
Instead of going with the 4 alternatives I explored on my previous post, you can solve the sorting issue:
Force the images to be inserted in an independent worksheet (inside the workbook), that can be hidden by default.
Once the images are inserted, you could recall them from the cell content (as they are now "inmune" to the sort) with a trick like the one exposed on the Show_pics_no_VBA.xlsx found at: https://onedrive.live.com/view.aspx?cid=863b7dd7364138ec&page=view&resid=863B7DD7364138EC!419
Obviously, you will need a "relation table" to keep track of the image index relation, but could be embedded on your main table just adding the index of the picture in an offset column
Kind regards
Force the images to be inserted in an independent worksheet (inside the workbook), that can be hidden by default.
Once the images are inserted, you could recall them from the cell content (as they are now "inmune" to the sort) with a trick like the one exposed on the Show_pics_no_VBA.xlsx found at: https://onedrive.live.com/view.aspx?cid=863b7dd7364138ec&page=view&resid=863B7DD7364138EC!419
Obviously, you will need a "relation table" to keep track of the image index relation, but could be embedded on your main table just adding the index of the picture in an offset column
Kind regards
- NikolinoDEMay 14, 2021Gold ContributorIf you query OneDrive about the path of the file it says something like C: usersNameOneDrive.
However, if you query the path in the file using a macro, it will give you a path with https: //d.docs.live.net / ....
If this is your case, then ...
To solve it, you could try by deactivating the "Update while opening" option in the OneDrive settings.
In this way, however, it would no longer be possible to work on one document at the same time.
Here I am at the end of my game ... I can't help anymore.
My experiences with OneDrive, Sharepoints and OnLine Excel are still in the beginning
Thank you for your patience and understanding
Nikolino