Selecting an image which I have inserted through VBA

Tanuja888

New Member
Joined
Jan 27, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have used a reference VBA to embedded an image without linking

VBA Code:
Sub InsertImage()

    Dim FullPathName as string

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Clear
        .Filters.Add "JPG", "*.JPG"
        .Filters.Add "JPEG File Interchange Format", "*.JPEG"
        .Filters.Add "Graphics Interchange Format", "*.GIF"
        .Filters.Add "Portable Network Graphics", "*.PNG"
        .Filters.Add "Tag Image File Format", "*.TIFF"
        .Filters.Add "All Pictures", "*.*"

        If .Show = -1 Then
            '''''' Store the pathname of selected image to a variable
            FullPathName = .SelectedItems(1)

            '''''' Imports image as embedded into Worksheet
            Activesheet.Shapes.AddPicture filename:=FullPathName, _
            linktofile:=msoFalse, savewithdocument:=msoCTrue, _
            left:=300, _
            top:=200, _
            width:=150, _
            height:=150

            Dim Pic As shape
            For Each Pic In ActiveSheet.Shapes
               ''''' Change "A1" to which ever cell this is being placed in
               'If (Pic.left = Range("A1").left And Pic.top = Range("A1").top) Then
                  Pic.Select
                  Pic.LockAspectRatio = msoTrue
               'End If
            Next Pic
         End If
    End With
End Sub


Now I'm trying to modify this program to select the image which got inserted.

Can anyone suggest the syntax for this and where to modify the program?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi there

Could you explain some more... When do you want the Pic to be selected... When I test your code it works and it inserts the pic as well as selecting it... this is done with the piece of code at ...
VBA Code:
Pic.Select

Is there any other way you are trying to select it?
 
Upvote 0
Hi there

Could you explain some more... When do you want the Pic to be selected... When I test your code it works and it inserts the pic as well as selecting it... this is done with the piece of code at ...
VBA Code:
Pic.Select

Is there any other way you are trying to select it?
Yes, the code works. but I want to select the inserted image again and want to run the following with that. but this in not happening.

VBA Code:
Pic.Select
Selection.OnAction = "macro_name"

Or can you please suggest a different code where we can select the image for some particular sheets on top cell (A1), then I can write the above syntax after that
 
Upvote 0
Maybe try the below... (Can also edit it to your needs perhaps?) Or maybe someone else notices how it can be done better...

VBA Code:
Option Explicit
Sub InsertImage()
    Dim FullPathName As String
    Dim myTop       As Integer
    Dim myLeft      As Integer
    myTop = ActiveCell.Top
    myLeft = ActiveCell.Left
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Clear
        .Filters.Add "JPG", "*.JPG"
        .Filters.Add "JPEG File Interchange Format", "*.JPEG"
        .Filters.Add "Graphics Interchange Format", "*.GIF"
        .Filters.Add "Portable Network Graphics", "*.PNG"
        .Filters.Add "Tag Image File Format", "*.TIFF"
        .Filters.Add "All Pictures", "*.*"
        If .Show = -1 Then
            '''''' Store the pathname of selected image to a variable
            FullPathName = .SelectedItems(1)
            '''''' Imports image as embedded into Worksheet
            ActiveSheet.Shapes.AddPicture Filename:=FullPathName, _
            linktofile:=msoFalse, savewithdocument:=msoCTrue, _
            Left:=myLeft, _
            Top:=myTop, _
            Width:=150, _
            Height:=150
            Dim Pic As Shape
            For Each Pic In ActiveSheet.Shapes
                If (Pic.Left = myLeft And Pic.Top = myTop) Then
                    Pic.Select
                    Pic.LockAspectRatio = msoTrue
                    Selection.OnAction = "macro_name"
                End If
            Next Pic
        End If
    End With
End Sub
 
Upvote 0
Thank you @mikerickson @Jimmypop
I tried your both ways, still I am unable get what I need.

SO I wrote a seperate program where it select the picture using range, then I used my next coomand.

VBA Code:
Sub Link_logo()
Dim pic As Picture
For Each pic In ActiveSheet.Pictures
    If Not Application.Intersect(pic.TopLeftCell, Range("A1:A2")) Is Nothing Then
        pic.Select
        Selection.OnAction = "Macro_NAme"
    End If
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top