VBA Excel Macro - Picture attached to Cell (insert) - Not link

%3CLINGO-SUB%20id%3D%22lingo-sub-1950447%22%20slang%3D%22en-US%22%3EVBA%20Excel%20Macro%20-%20Picture%20attached%20to%20Cell%20(insert)%20-%20Not%20link%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1950447%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20looking%20to%20simply%20run%20a%20macro%20that%20finds%20a%20picture's%20location%20in%20one%20cell%20and%20then%20insert%20the%20picture%20into%20another%20cell.%20But%20it%20has%20to%20be%20attached%20to%20a%20cell%20so%20it%20can%20be%20sorted%20AND%20be%20an%20actual%20picture%20vs%20a%20link.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20tried%202%20versions%20with%20each%20having%20it's%20own%20issues%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1%20-%20ActiveSheet.Pictures.Insert(picname).Select%20'%2C%20LinkToFile%3A%3DmsoFalse%2C%20SaveWithDocument%3A%3DmsoTrue%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E**%20This%20one%20attaches%20to%20cells%2C%20and%20inserts%20the%20pictures%2C%20but%20ONLY%20as%20links%2C%20so%20the%20spreadsheet%20can't%20be%20sent%20to%20anyone.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E2%20-%20ActiveSheet.Shapes.AddPicture%20Filename%3A%3Dpicname%2C%20LinkToFile%3A%3DmsoFalse%2C%20SaveWithDocument%3A%3DmsoTrue%2C%20Left%3A%3DCells(CurRow%2C%20PicLocCol).Left%2C%20Top%3A%3DCells(CurRow%2C%20PicLocCol).Top%2C%20Height%3A%3D80%2C%20Width%3A%3D100%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E**%20This%20one%20attaches%20a%20picture%20(vs%20a%20link)%20and%20can%20be%20sent%2C%20but%20won't%20attach%20to%20cells%2C%20so%20it%20can't%20be%20sorted.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%3A%20I%20don't%20know%20exactly%20how%20to%20attach%20code%2C%20so%20I'm%20attaching%20it%20as%20a%20DOC%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20spreadsheet%20currently%20has%202%20columns.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EB%20-%20C%3A%5Cpic1.jpg%3C%2FP%3E%3CP%3EA%20-%20C%3A%5Cpic2.jpg%3C%2FP%3E%3CP%3EC%20-%20C%3A%5Cpic3.jpg%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20added%20the%20letters%20in%20column%20A%20(out%20of%20order%20to%20test%20the%20sorting)%2C%20and%20a%20pic%20location%20in%20Column%20B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1950447%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1954114%22%20slang%3D%22de-DE%22%3ESubject%3A%20VBA%20Excel%20Macro%20-%20Picture%20attached%20to%20Cell%20(insert)%20-%20Not%20link%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1954114%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F883953%22%20target%3D%22_blank%22%3E%40Gstg72%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EI'm%20not%20sure%20if%20I%20understood%20it%20correctly%20from%20the%20translation%2C%20but%20I'm%20still%20sending%20you%20this%20VBA%20code%20with%20a%20selection%20function.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EIf%20this%20is%20not%20what%20you%20had%20in%20mind%2C%20just%20ignore%20it.%3C%2FSPAN%3E%3C%2FSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EOption%20Explicit%0A%0A%0ASub%20InsertPicture()%0A%20Dim%20sPicture%20As%20String%2C%20pic%20As%20Picture%0A%0A%20sPicture%20%3D%20Application.GetOpenFilename%20_%0A%20(%22Pictures%20(*.gif%3B%20*.jpg%3B%20*.bmp%3B%20*.tif)%2C%20*.gif%3B%20*.jpg%3B%20*.bmp%3B%20*.tif%22%2C%20_%0A%20%2C%20%22Select%20Picture%20to%20Import%22)%0A%0A%20If%20sPicture%20%3D%20%22False%22%20Then%20Exit%20Sub%0A%0A%20Set%20pic%20%3D%20ActiveSheet.Pictures.Insert(sPicture)%0A%20With%20pic%0A%20.ShapeRange.LockAspectRatio%20%3D%20msoFalse%0A%20.Height%20%3D%20ActiveCell.Height%0A%20.Width%20%3D%20ActiveCell.Width%0A%20.Top%20%3D%20ActiveCell.Top%0A%20.Left%20%3D%20ActiveCell.Left%0A%20.Placement%20%3D%20xlMoveAndSize%0A%20End%20With%0A%0A%20Set%20pic%20%3D%20Nothing%0A%0A%20End%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20be%20happy%20to%20know%20if%20I%20could%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E*%20Kindly%20Mark%20and%20Vote%20this%20reply%20if%20it%20helps%20please%2C%20as%20it%20will%20be%20beneficial%20to%20more%20community%20members%20reading%20here.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1954454%22%20slang%3D%22en-US%22%3EBetreff%3A%20VBA%20Excel%20Macro%20-%20Picture%20attached%20to%20Cell%20(insert)%20-%20Not%20link%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1954454%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40Nikolino%3C%2FA%3E%26nbsp%3BThank%20you%20again%20for%20your%20response.%20But%20this%20doesn't%20seem%20to%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20pictures%20are%20not%20actually%20inside%20the%20spreadsheet.%20For%20example%20if%20you%20email%20the%20spreadsheet%20and%20attempt%20to%20open%20it%20on%20another%20device%20or%20using%20office%20online%2C%20you%20will%20notice%20the%20picture%20are%20not%20there.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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 picture vs a link. 

 

I've tried 2 versions with each having it's own issues:

 

1 - ActiveSheet.Pictures.Insert(picname).Select ', LinkToFile:=msoFalse, SaveWithDocument:=msoTrue

 

** This one attaches to cells, and inserts the pictures, but ONLY as links, so the spreadsheet can't be sent to anyone. 

 

2 - ActiveSheet.Shapes.AddPicture Filename:=picname, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=Cells(CurRow, PicLocCol).Left, Top:=Cells(CurRow, PicLocCol).Top, Height:=80, Width:=100

 

** This one attaches a picture (vs a link) and can be sent, but won't attach to cells, so it can't be sorted.

 

PS: I don't know exactly how to attach code, so I'm attaching it as a DOC file.

 

The spreadsheet currently has 2 columns. 

 

 

B - C:\pic1.jpg

A - C:\pic2.jpg

C - C:\pic3.jpg

 

I added the letters in column A (out of order to test the sorting), and a pic location in Column B

8 Replies

I found a partial work around ... but it's horrible to think this has to be done. 

 

You can use the first option to insert the pictures / attached to cells as links, then convert it to a PDF. The PDF can be sent, etc. Then convert it from PDF back to an Excel file. While this seems like a really bad work around, I haven't found any other options yet.

@Gstg72 

I'm not sure if I understood it correctly from the translation, but I'm still sending you this VBA code with a selection function. If this is not what you had in mind, just ignore it. 

 

Option Explicit


Sub InsertPicture()
 Dim sPicture As String, pic As Picture

 sPicture = Application.GetOpenFilename _
 ("Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
 , "Select Picture to Import")

 If sPicture = "False" Then Exit Sub

 Set pic = ActiveSheet.Pictures.Insert(sPicture)
 With pic
 .ShapeRange.LockAspectRatio = msoFalse
 .Height = ActiveCell.Height
 .Width = ActiveCell.Width
 .Top = ActiveCell.Top
 .Left = ActiveCell.Left
 .Placement = xlMoveAndSize
 End With

 Set pic = Nothing

 End Sub

 

 

I would be happy to know if I could help.

 

Nikolino

I know I don't know anything (Socrates)

 

* Kindly Mark and Vote this reply if it helps please, as it will be beneficial to more Community members reading here.

@NikolinoDE Thank you again for your response. But this doesn't seem to work.

 

The pictures are not actually inside the spreadsheet. For example if you email the spreadsheet and attempt to open it on another device or using office online, you will notice the picture are not there. 

 

 

@Gstg72 

 

Its Works for me

 

Look the file please.

 

* It is also helpful to know the operating system and Excel version, as different approaches may be required depending on the version and OS.

 

I would be happy to know if I could help.

 

Nikolino

I know I don't know anything (Socrates)

After repeated translations with other software, I (I think) understood what you wanted. Do not think that this can be done with the file sent by me. Maybe only partly. Anyway, I wish you the best of luck and success in your project. Thank you, Nikolino I know that I don't know (Socrates)

@Gstg72 

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):

  1. Use the inserted cell as a sorting index
  2. Use an inside text as reference (you can hide the text with the transparency value)
  3. Use the AlternativeText as reference (commented code)
  4. Use the Hyperlink (commented code)
  5. ... 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

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
If 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