Form Upload button

snoozee

New Member
Joined
Apr 19, 2007
Messages
37
Hi

I have a button on my user form which I need to code so that the user can upload and embed a file into a cell reference, is this possible ?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this
Amend default folder path and sheet name

Code:
Private Sub CommandButton1_Click()
    Dim myImage As Variant, defaultFolderPath As String
    defaultFolderPath = "[COLOR=#ff0000]C:\Folder\subfolder[/COLOR]"           
    
[I][COLOR=#000080]'hide userform[/COLOR][/I]
    Me.Hide
    
[I][COLOR=#000080]'where is image placed?[/COLOR][/I]
    Sheets("[COLOR=#ff0000]SheetName[/COLOR]").Activate
    Application.InputBox("Select Cell with mouse and click ok", "Insert Image", , , , , , 8).Select

[I][COLOR=#000080]'which image?[/COLOR][/I]
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = defaultFolderPath & "\"
        .Title = "Choose File"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub
        myImage = .SelectedItems(1)
    End With
    
[I][COLOR=#000080]'insert chosen image in selected cell and show userform[/COLOR][/I]
    ActiveSheet.Pictures.Insert (myImage)
    Me.Show
End Sub
 
Upvote 0
Try this
Amend default folder path and sheet name

Code:
Private Sub CommandButton1_Click()
    Dim myImage As Variant, defaultFolderPath As String
    defaultFolderPath = "[COLOR=#ff0000]C:\Folder\subfolder[/COLOR]"           
    
[I][COLOR=#000080]'hide userform[/COLOR][/I]
    Me.Hide
    
[I][COLOR=#000080]'where is image placed?[/COLOR][/I]
    Sheets("[COLOR=#ff0000]SheetName[/COLOR]").Activate
    Application.InputBox("Select Cell with mouse and click ok", "Insert Image", , , , , , 8).Select

[I][COLOR=#000080]'which image?[/COLOR][/I]
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = defaultFolderPath & "\"
        .Title = "Choose File"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub
        myImage = .SelectedItems(1)
    End With
    
[I][COLOR=#000080]'insert chosen image in selected cell and show userform[/COLOR][/I]
    ActiveSheet.Pictures.Insert (myImage)
    Me.Show
End Sub

Hi thanks for this I’ll give it a try :-)

Just a quick question will this also work with other files e.g. PDF’s
 
Upvote 0
Just a quick question will this also work with other files e.g. PDF’s
Yes - it should insert (but won't display) a PDF
 
Upvote 0
Just spotted a minor glitch
- this avoids the UserForm "disappearing" if File Dialog closed without selecting a file

Code:
Private Sub CommandButton1_Click()
    Dim myImage As Variant, defaultFolderPath As String
    defaultFolderPath = "C:\TestArea\pdf"
'hide userform
    Me.Hide
'where is image placed?
    Sheets("SheetName").Activate
    Application.InputBox("Select Cell with mouse and click ok", "Insert Image", , , , , , 8).Select
'which image?
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = defaultFolderPath & "\"
        .Title = "Choose File"
        .AllowMultiSelect = False
        If .Show <> -1 Then[COLOR=#ff0000] GoTo TheEnd:[/COLOR]
        myImage = .SelectedItems(1)
    End With
'insert chosen image in selected cell and show userform
    ActiveSheet.Pictures.Insert (myImage)
[COLOR=#ff0000]TheEnd:[/COLOR]
    Me.Show
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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