VBA userform code to browse file and attach

Jcjepot

New Member
Joined
Oct 5, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi! Apologies for asking but i am blocked looking for a vba code to. I am making a userform with the usual name/employee number / contact number and to attach 2 files (either jpeg, doc, pdf) and placing it in a database sheet.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Here's an example that I hope you can adopt for your use. It prompts the user to select a file, and then embeds the file onto the worksheet. Note that you may need to change the FileFilter. For example, you may need to replace *.doc with *.docx, or whatever is appropriate. Or you may want to include *.docx along with *.doc. Also, note that I've assume that the database sheet is called Database.. So change the sheet name accordingly.

VBA Code:
    'prompt user to select a file to embed (change the filter accordingly)
    Dim openFilename As Variant
    openFilename = Application.GetOpenFilename( _
        FileFilter:="Files (*.jpeg;*.doc;*.pdf), *.jpeg;*.doc;*.pdf", _
        Title:="Select File", _
        ButtonText:="Select")
    
    'if user cancels, exit the sub
    If openFilename = False Then Exit Sub
    
    'get the database sheet (change the name accordingly)
    Dim databaseSheet As Worksheet
    Set databaseSheet = ThisWorkbook.Worksheets("Database")
    
    'embed the selected file on the database sheet and position it at cell B2
    databaseSheet.OLEObjects.Add _
        filename:=openFilename, _
        Left:=databaseSheet.Range("B2").Left, _
        Top:=databaseSheet.Range("B2").Top

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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