Bestand importeren uit vaste directory

Status
Niet open voor verdere reacties.

LisaAlberta

Gebruiker
Lid geworden
9 aug 2012
Berichten
73
Hoi,

ik wil een dialoogvenster openen waarbij mensen een file uit moeten kiezen dat vervolgens geimporteerd moet worden (als nieuw tabblad) in een reeds openstaand bestand.
De files waaruit gekozen moet worden staan altijd in dezelfde directory, dus ik wil de dialogbox altijd meteen naar die directory laten gaan.
Ik kan wel codes vinden om de filedialog te openen, maar daarbij kan ik niet aangeven wat de standaard directory moet zijn.
Code:
With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            MsgBox .SelectedItems(1)
        End If
    End With
Kan iemand mij hierbij helpen?
Alvast bedankt :)
Lisa
 
ik gebruik deze
(onder een command button)

Code:
Dim Slash As String

CommonDialog1.Filter = "Tekst|*.txt"
If Right(App.Path, 1) <> "\" Then Slash = "\"
CommonDialog1.InitDir = App.Path & Slash & "Standard directory"
CommonDialog1.FileName = App.Path & Slash & "Standard directory" & "\*.txt"

CommonDialog1.ShowOpen
 
Ik wil m heel graag onder een gewone macro houden en importeren in excel (daarna moeten er nog wat dingen mee gebeuren)
 
Ik heb er eentje gevonden:
Code:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'the number of the button chosen
Dim FileChosen As Integer
FileChosen = fd.Show

'1) To set the caption of the dialog box, 
' set the Title property
fd.Title = "Example of choosing file"

'2) Set the oddly named InitialFileName property to
' determine the initial folder selected
fd.InitialFileName = "c:\wise owl\"

'3) Set the InitialView property to control how your files
' appear on screen (as a list, icons, etc.)
fd.InitialView = msoFileDialogViewSmallIcons

'4) To set the filters (you can have as many as you like)
' first clear any existing ones, then add them one by one
fd.Filters.Clear
fd.Filters.Add "Excel macros", "*.xlsm"

' if there's more than one filter, you can control which
' one is selected by default
fd.FilterIndex = 1

'5) Set the ButtonName property to control the text on
' the OK button (the ampersand means the following
' letter is underlined and choosable with the ALT key)
fd.ButtonName = "Choose &owly file"

If FileChosen <> -1 Then

'didn't choose anything (clicked on CANCEL)
MsgBox "You chose cancel"

Else

'display name and path of file chosen
MsgBox fd.SelectedItems(1)

End If
 
Hoi,
onderstaande oplossing werkt, maar ik heb een raar probleempje. De allereerste keer in een sessie dat je dit doet gaat ie naar de meest recent gebruikte directory, en niet naar de genoemde directory. Iemand enig idee hoe ik eea zo ver krijg om meteen de goede directory te laten zien?
Code:
Dim fd As FileDialog
Dim FileName As String

Set fd = Application.FileDialog(msoFileDialogOpen)

'the number of the button chosen
Dim FileChosen As Integer
FileChosen = fd.Show

fd.Title = "Bestand kiezen"
fd.InitialFileName = "F:\Divers\Rooster\overplaatsingen"
fd.InitialView = msoFileDialogViewList

'show Excel workbooks and macro workbooks
fd.Filters.Clear
fd.Filters.Add "Excel workbooks", "*.xlsx"


fd.FilterIndex = 1

fd.ButtonName = "Importeer tabblad"

If FileChosen <> -1 Then

'didn't choose anything (clicked on CANCEL)
MsgBox "Je hebt geen bestand gekozen"
Exit Sub
Else

'get file, and open it (NAME property
'includes path, which we need)
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)

End If
met vriendelijke groet,
Lisa
 
Je moet de volgorde gewoon iets aanpassen:

Code:
Dim fd As FileDialog
Dim FileName As String

Set fd = Application.FileDialog(msoFileDialogOpen)

'the number of the button chosen
Dim FileChosen As Integer

fd.Title = "Bestand kiezen"
fd.InitialFileName = "F:\Divers\Rooster\overplaatsingen"
fd.InitialView = msoFileDialogViewList

FileChosen = fd.Show()

'show Excel workbooks and macro workbooks
fd.Filters.Clear
fd.Filters.Add "Excel workbooks", "*.xlsx"


fd.FilterIndex = 1

fd.ButtonName = "Importeer tabblad"

If FileChosen <> -1 Then

'didn't choose anything (clicked on CANCEL)
MsgBox "Je hebt geen bestand gekozen"
Exit Sub
Else

'get file, and open it (NAME property
'includes path, which we need)
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)

End If
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan