Import an Excel Sheet with File Dialog in Access - sql

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

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.

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

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).

Open a workbook using FileDialog and manipulate it in Excel VBA

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)

Open Windows Explorer and select a file

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.