Open Windows Explorer and select a file - vba

Is there a way to open a Windows Explorer window from a vba form, navigate to a specific file and select it so that the file name is placed in a text box?

Check out this snippet:
Private Sub openDialog()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Please select the file."
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
End If
End With
End Sub
I think this is what you are asking for.

Related

Importing doc variables from a Word document into another

I have a Word document with a userform that allows users to complete a series of questions (filing out textboxes) that populates a Word document. The userform utilizes doc variables to mark where in the document I want answers to be populated. The code looks like:
Dim TextBox13 As Range
ActiveDocument.Variables("dvmdy").Value = Me.TextBox13.Value
Me.TextBox13.Value = Me.TextBox13.Text
With ActiveDocument
.Fields.Update
End With
I also have a command button that works to open another document but am trying to make it so it reads the docvariables in the second document and writes them to the first. The code below is for the file selector setup I put together:
Private Sub CommandButton1_Click()
Dim fDialog As FileDialog
Dim strFile1 As String
Dim strFile2 As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Filters.Clear
.Filters.Add "Word Documents", "*.docm*"
.InitialFileName = "C:\"
.Title = "Select the First Document"
If .Show = -1 Then
strFile1 = .SelectedItems(1)
strFile1 = .SelectedItems(1)
MsgBox (strFile1)
Label8v = strFile1
Else
Exit Sub
End If
End With
With fDialog
.Filters.Clear
.Filters.Add "Word Documents", "*.docm*"
.InitialFileName = "C:\"
.Title = "Select the Second Document"
If .Show = -1 Then
strFile2 = .SelectedItems(1)
strFile2 = .SelectedItems(1)
MsgBox (strFile2)
Label9v = strFile2
Else
Exit Sub
End If
End With
Set Doc1 = Documents.Open(strFile1)
Set Doc2 = Documents.Open(strFile2)
End Sub
Basically I want to make it so it allows users to open previously filled out Word documents with the same doc variables so that the previously filled out answers could be used to populate the new document/userform.

Import an Excel Sheet with File Dialog in Access

I have a splitform where I want a button to choose and import a file into a table.
I have something like this:
Private Sub Command287_Click()
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx,*.xls"
.Filters.Add "Access Databases", "*.accdb"
.FilterIndex = 1 'Excel Files
.AllowMultiSelect = False
.ButtonName = "Open"
.InitialFileName = "C:\"
.Title = "Select a File to Import"
.InitialView = msoFileDialogViewDetails
If .Show Then
DoCmd.TransferSpreadsheet acImport, , "Data", .SelectedItems(1), True, "Data!"
End If
End With
End Sub
I set the reference to Microsoft Office 16.0 Object Library.
When I tried using this button there was this error:
Method 'FileDialog' of object '_Application' failed.
I copied Albert Kallal's answer found here: It does not require any reference.
Private Sub Command287_Click()
Dim f As Object
Set f = Application.FileDialog(3)
f.filters.Clear
f.filters.Add "Excel Files", "*.xlsx,*.xls"
f.Title = "Select a file to Import"
f.InitialFileName = "C:\"
If f.Show Then
DoCmd.TransferSpreadsheet acImport, , "Data", f.selectedItems(1), True, "Data"
End If
End Sub

Showing path of selected files in ListBox

I would like to see names or paths of selected files in VBA using windows explorer.
Here's what I have done so far, it opens the explorer and allows to select files but lacks displaying the names/path of the files.
Private Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
///
///here comes the part with showing the names in ListBox
///
End If
End With
End Sub
Here is the completed code:
Public Sub CommandButton1_Click()
Dim i As Integer
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
For i = 1 To fd.SelectedItems.Count
ListBox1.AddItem fd.SelectedItems(i)
Next
End If
End With
End Sub

VBA Excel Choose File - When cancel is clicked it clears the textbox

Basically I have a spreadsheet with a form on it. On that form there is a textbox that contains a file path which could be pre-populated from a cell on the sheet. But the user can choose to browse for another file. When they are browsing they have an option of "Open" or "Cancel". The open button works fine and populates the textbox, but if they choose cancel it clears the textbox if it is already populated. How can I stop the textbox being cleared?
I have narrowed it down to this block of code where it is happening:
Function GetFileName()
Set MyFile = Application.FileDialog(msoFileDialogOpen)
With MyFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Function
End If
GetFileName = .SelectedItems(1)
End With
End Function
This done the trick. Making sure the file name has a value before populating the textbox
Private Sub btnBrowse_Click()
Dim sFileName As String
sFileName = GetFileName()
If Len(sFileName) > 0 Then
TextBox1.Value = sFileName
End If
End Sub
Function GetFileName()
Set MyFile = Application.FileDialog(msoFileDialogOpen)
With MyFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Function
End If
GetFileName = .SelectedItems(1)
End With
End Function

PowerPoint VBA Save As Dialog file filter

How do I set 1 or more Save As Dialog file filter in PowerPoint VBA? Here is my code:
Private Sub save_file_Click()
Dim fd As FileDialog
Dim clicked As Boolean
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
.InitialFileName = ActivePresentation.path & "\*.note"
.AllowMultiSelect = False
.ButtonName = "Save"
.Title = "Save File"
'-- Normal Filters are not allowed on FileDialogSaveAs --
'.Filters.Clear
'.Filters.Add "Note Files", "*.note"
'.Filters.Add "Text Files", "*.txt"
'.Filters.Add "All Files", "*.*"
End With
clicked = fd.Show
If clicked Then
Open fd.SelectedItems(1) For Input As #1
Print #1, text_box.Text
Close intFileNum
End If
End Sub
Any Ideas?
In case it makes a difference: I'm not trying to make an ppt export to HTML, but save a file that's custom (.note) in a User Form (window).