Userform hulp gevraagd (knoppen)

Status
Niet open voor verdere reacties.

Teebee

Gebruiker
Lid geworden
8 jan 2006
Berichten
157
Hallo,

ik heb een userform gemaakt met 3 knoppen, en als ik op een knop druk wil ik een bepaalde folder openen waarna ik het juiste bestand kan kiezen, maar iets loopt er niet goed:

Code:
Private Sub CommandButton1_Click()
' Label1.Caption = UserForm1.ActiveControl.Name
Dim tb_fileurl As String
tb_fileurl = "N:\Bestanden\Excel\Jaaroverzicht"
Label1.Caption = UserForm1.ActiveControl.name & " " & tb_fileurl
tb_openfiledialog (tb_fileurl)
End Sub
Private Sub CommandButton2_Click()
Dim tb_fileurl As String
tb_fileurl = "N:\Bestanden\Excel\Handleidingen"
Label1.Caption = UserForm1.ActiveControl.name & " " & tb_fileurl
tb_openfiledialog (tb_fileurl)
End Sub
Private Sub CommandButton3_Click()
' Label1.Caption = UserForm1.ActiveControl.Name
Dim tb_fileurl As String
tb_fileurl = "I:\Gegevens\Excel\Verslagen"
Label1.Caption = UserForm1.ActiveControl.name & " " & tb_fileurl
tb_openfiledialog (tb_fileurl)
End Sub

    
Sub tb_openfiledialog(tb_fileurl As String)
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 = "Kies het juiste bestand"
fd.InitialFileName = tb_fileurl
fd.InitialView = msoFileDialogViewList
'show Excel workbooks and macro workbooks
fd.Filters.Clear
fd.Filters.Add "Excel workbooks", "*.xlsx"
fd.Filters.Add "Excel macros", "*.xlsm"
fd.FilterIndex = 1
fd.ButtonName = "Choose this file"
If FileChosen <> -1 Then
'didn't choose anything (clicked on CANCEL)
' MsgBox "No file opened"
Else
'get file, and open it (NAME property
'includes path, which we need)
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)
End If

End Sub

Als ik nu op een knop druk doet hij eerst de voorgaande folder open, als ik dan nog eens druk opent hij wel de juiste locatie.

Wat doe ik mis?
 
Je opent de filedialog twee keer, de eerste keer dus met de oude map.
Code:
    Set fd = Application.FileDialog(msoFileDialogOpen)
    With fd
        .Title = "Kies het juiste bestand"
        .InitialFileName = tb_fileurl
        .InitialView = msoFileDialogViewList
        'show Excel workbooks and macro workbooks
        .Filters.Clear
        .Filters.Add "Excel workbooks", "*.xlsx"
        .Filters.Add "Excel macros", "*.xlsm"
        .FilterIndex = 1
        .ButtonName = "Choose this file"
        If .Show = -1 Then                          'Bepaal of gebruiker op OK-knop heeft geklikt.
            FileName = .SelectedItems(1)
            Workbooks.Open (FileName)
        Else
            MsgBox "Er is op <Annuleren> gedrukt..."
            Exit Sub
        End If
    End With
 
Weet je misschien ook hoe ik het veld van de bestandsnaam leeg kan houden?
 
Volgens mij is dit genoeg:

Code:
Private Sub CommandButton1_Click()
    With Application.FileDialog(3)
      .InitialFileName = "N:\Bestanden\Excel\Jaaroverzicht\*.xls*"
      If .Show Then GetObject .SelectedItems(1)
   End With
End Sub
 
Laatst bewerkt:
Bedankt.

Ik heb nu ook zelf gemerkt dat als ik de folder variabel eindig met \ dat bestandnaam ook leeg blijft.
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan