I would like to write a program in VBA.
I would like a window to pop up when the program starts, where I can bring up the file manager with a button and the elements what are (multiple)selected here, would be placed in a textbox under each other and excel would open them all at once.
Now I finished the useform with one button and one textbox, and have a code for the button, to open the file manager and enabled the multiselect. Whats next? Anybody can help me?
Private Sub CommandButton1_Click()
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
End With
rv = fldlg.Show
If rv Then
MsgBox fldlg.SelectedItems(1)
End If
End Sub
To open all the files (or build a string listing them all), you need to cycle through the SelectedItems collection and use each in turn:
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
Dim strItems
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
rv = .Show
If rv Then
'create a string of text for output using MsgBox
For Each SelectedItem In .SelectedItems
strItems = strItems & SelectedItem & Chr(13)
Next
'display string
MsgBox strItems
'open the files
For Each SelectedItem In .SelectedItems
Workbooks.Open SelectedItem
Next
End If
End With
Related
I have a function in a macro that allows me to select a file using inputbox, or is it possible to always start showing files at the same time, so that I don't have to look in folders. For example, when the selectFile window appears, the folder should be \banksl\dfs\BH\BPR\
strPath1 = selectFile
If strPath1 = "" Then Exit Function
Set bilans = Workbooks.Open(strPath1, False, True)
Private Function selectFile1()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.InitialFileName = ActiveWorkbook.path
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel", "*.xlsm"
If .Show = True Then selectFile = .SelectedItems(1)
End With
I am trying to give users the option to 'set' a default folder location, this would only be used to prefill further userform text boxes to save time having to go through the msoFileDialogFolderPicker
I did use chdir, but that was written into the vba script, this location will differ from user to user and should only need to be set once upon addon installation
This is the code for the master folder selector:
Private Sub cmdfoldsel_Click()
On Error GoTo err
Dim fileExplorer As FileDialog
Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)
'To allow or disable to multi select
fileExplorer.AllowMultiSelect = False
With fileExplorer
If .Show = -1 Then 'Any folder is selected
tbmasterloc.text = .SelectedItems.Item(1)
Else ' else dialog is cancelled
MsgBox "You have cancelled the dialogue"
[folderPath] = "" ' when cancelled set blank as file path.
End If
End With
err:
Exit Sub
End Sub
It is letting me choose the folder, but obviously the text disappears when I close ( I was using a close button with unload me but i thought that was deleting the text
An example of the second userform which will call that folder location is here:
Private Sub UserForm_Initialize()
copyfromtb.Value = mfs.tbmasterloc.text
End Sub
Private Sub copyfromcmd_Click()
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
'.InitialFileName = Application.GetSaveAsFilename()
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
copyfromtb.Value = sItem
Set fldr = Nothing
End Sub
I have the follow macro for a button click. When I run it, I hit a snag at the Label caption line with an 'object required' error. The names of the objects are correct.
Sub Button1_Click()
Dim objFileDialog As Object
Dim objSelectedFile As Variant
Set objFileDialog = Application.FileDialog(3)
With objFileDialog
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel File", "*.*", 1
.Title = "Select Input file"
.Show
For Each objSelectedFile In .SelectedItems
Label2.Caption = objSelectedFile
Next
End With
End Sub
I am trying to show the path of a file I select from a dialog box, in a text box.
Sub SelectMonthlyFile()
Dim Data As Office.FileDialog
Set Data = Application.FileDialog(msoFileDialogFilePicker)
With Data
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
Path = .SelectedItems(1)
End If
End With
End Sub
I show an error at Path = .SelectedItems(1).
variable is not defined
I named my cell Path and assumed that this would get the file path copied and pasted here.
I suggest you rename Data to something more meaningful. I've changed it to just FileDialog. The reason for your error is that the code has no idea what Path is. Note that I omitted the clearing of the filter, it is not needed.
Option Explicit 'always have this
Dim strPath As String 'explicitly declare your variable
Sub SelectMonthlyFile()
Dim FileDialog As Office.FileDialog
Set FileDialog = Application.FileDialog(msoFileDialogFilePicker)
With FileDialog
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
'was anything selected?
If .Show <> 0 Then
strPath = .SelectedItems(1)
Debug.Print strPath
End If
End With
End Sub
I have the following code
Private Sub btnOpenTemplate_Click()
Dim c As Range
Dim fd As Office.FileDialog, directory As String, fileName As String
Set c = Settings.Range("A1")
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Âûáåðèòå øàáëîí ÏÌÈ."
.Filters.Clear
.Filters.Add "Word 2003", "*.doc?"
If .Show = True Then
fileName = .SelectedItems(1)
End If
End With
If fileName = "" Then
Exit Sub
End If
If Dir(fileName, vbNormal) <> "" Then
If c.Worksheet.OLEObjects.Count > 0 Then
c.Worksheet.OLEObjects(1).Delete
End If
Settings.OLEObjects.Add fileName:=fileName, Link:=False, DisplayAsIcon:=True
End If
Settings.Range("A2").Value = fileName
lblTemplateFile.Caption = fileName
End Sub
Whenever it is executed, the workbook gets damaged and Excel is unable to repair and save it. What could be wrong? What I mean is that the workbook works fine just until I embed an object (Word doc) into it via the macro. Then it gets damaged.