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
Related
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.
I'm trying to use below code to selecting a file and write it's path and filename to textbox. I tried from a Worksheet with codename TESTAREA But i'm always get an error Type Mismatch.
I'm using below procedure to use function from sheet TEST_AREA(TESTAREA) Sheet Name (Code Name)
Private Sub CommandButton1_Click()
Call myFunctions.SelectFile(TESTAREA, "*.txt", TextBox1)
End Sub
This is function
Function SelectFile(ByVal strSheetName As Worksheet, strFilterExt As String, strTextBox As TextBox)
Dim fdo As Office.FileDialog
Set fdo = Application.FileDialog(msoFileDialogFilePicker)
With fdo
.InitialFileName = AUTOMBS.path
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", strFilterExt
If .Show = True Then
strSheetName.strTextBox.Value = .SelectedItems(1)
End If
End With
End Function
Could you please help me where i'm wrong ?
This worked for me. I added an ActiveX TextBox to Sheet1 and used your code. It gave me a Type Mismatch until I changed the function to take a MSForms.TextBox instead.
Sub Test()
Debug.Print GetName(Sheet1.TextBox1)
End Sub
Function GetName(tb As MSForms.TextBox)
GetName = tb.Value
End Function
So try this. I changed it to a Sub because you aren't returning anything to the Function!
Sub SelectFile(ByVal strSheetName As Worksheet, strFilterExt As String, strTextBox As MSForms.TextBox)
Dim fdo As Office.FileDialog
Set fdo = Application.FileDialog(msoFileDialogFilePicker)
With fdo
.InitialFileName = AUTOMBS.path
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", strFilterExt
If .Show = True Then
strTextBox.Value = .SelectedItems(1)
End If
End With
End Sub
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).
I am learning how to use Excel macros and I found this code:
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select the file to kill his non colored cells"
.Filters.Add "Excel", "*.xls"
.Filters.Add "All", "*.*"
If .Show = True Then
txtFileName = .SelectedItems(1)
End If
End With
This code opens the FileDialog. How can I open the selected Excel file without over-writing the previously opened?
Thankyou Frank.i got the idea.
Here is the working code.
Option Explicit
Private Sub CommandButton1_Click()
Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls?"
If .Show = True Then
fileName = Dir(.SelectedItems(1))
End If
End With
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open (fileName)
For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("import-sheets.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("import-sheets.xlsm").Worksheets(total)
Next sheet
Workbooks(fileName).Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Unless I misunderstand your question, you can just open a file read only.
Here is a simply example, without any checks.
To get the file path from the user use this function:
Private Function get_user_specified_filepath() As String
'or use the other code example here.
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Please select the file."
get_user_specified_filepath = fd.SelectedItems(1)
End Function
Then just open the file read only and assign it to a variable:
dim wb as workbook
set wb = Workbooks.Open(get_user_specified_filepath(), ReadOnly:=True)
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.