Browse for a file name and place as string in text box (1 Viewer)

IanO

Registered User.
Local time
Today, 20:49
Joined
Jan 2, 2014
Messages
25
I have created a form to send emails with attachments.
The attachment path is specified in an unbound field which I have called [ToAttach]
Rather than typing in the path, I want to use the browse function.
I have inserted a browse button and can browse for the required file using following, but can't figure out how to place the file name in the unbound field as a string.
Code:
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
f.Show
 

pr2-eugin

Super Moderator
Local time
Today, 19:49
Joined
Nov 30, 2011
Messages
8,494
Create a button next to the Text box, or use the On Click of the Text box method, use the following code.
Code:
Public Sub textBoxName_Click()  
    Dim fileDump As Object
    Set fileDump = Application.FileDialog(msoFileDialogOpen)

    If fileDump.Show = -1 Then Me.textBoxName = fileDump.SelectedItems(1)
End Sub
 

IanO

Registered User.
Local time
Today, 20:49
Joined
Jan 2, 2014
Messages
25
Create a button next to the Text box, or use the On Click of the Text box method, use the following code.
Code:
Public Sub textBoxName_Click()  
    Dim fileDump As Object
    Set fileDump = Application.FileDialog(msoFileDialogOpen)

    If fileDump.Show = -1 Then Me.textBoxName = fileDump.SelectedItems(1)
End Sub

Thank you for responding.
I get this error
Method 'FileDialog' of object'_Application' failed
I have searched for a solution but not successful. Does this need an object library reference?
 

pr2-eugin

Super Moderator
Local time
Today, 19:49
Joined
Nov 30, 2011
Messages
8,494
Then try early biding.
Code:
Public Sub textBoxName_Click()
    Dim fileDump As FileDialog
    Set fileDump = FileDialog(msoFileDialogOpen)

    If fileDump.Show = -1 Then Debug.Print fileDump.SelectedItems(1)
End Sub
This will require Microsoft Office XX.X Object Library, under Tools->Reference. Where XX.X represents version number in my system this is 14.0.
 

IanO

Registered User.
Local time
Today, 20:49
Joined
Jan 2, 2014
Messages
25
Then try early biding.
Code:
Public Sub textBoxName_Click()
    Dim fileDump As FileDialog
    Set fileDump = FileDialog(msoFileDialogOpen)

    If fileDump.Show = -1 Then Debug.Print fileDump.SelectedItems(1)
End Sub
This will require Microsoft Office XX.X Object Library, under Tools->Reference. Where XX.X represents version number in my system this is 14.0.

Thank you Paul
That was the missing link. I struggled to find a place tying Object Libraries to specific codes.
For anyone reading this in future, Ctrl-G displays the dump while in Visual Basic.
 

pr2-eugin

Super Moderator
Local time
Today, 19:49
Joined
Nov 30, 2011
Messages
8,494
Oops, sorry when testing I used Debug.Print. you can change it as,
Code:
Public Sub textBoxName_Click()
    Dim fileDump As FileDialog
    Set fileDump = FileDialog(msoFileDialogOpen)

    If fileDump.Show = -1 Then Me.textBoxName = fileDump.SelectedItems(1)
End Sub
 

IanO

Registered User.
Local time
Today, 20:49
Joined
Jan 2, 2014
Messages
25
Thanks, that was a useful slip -I t meant I also learned about debug today.
It works well.
 

Users who are viewing this thread

Top Bottom