lamquin
Messages postés35Date d'inscriptionvendredi 18 novembre 2005StatutMembreDernière intervention 5 juin 2008
-
19 juil. 2006 à 11:30
sloy1978
Messages postés13Date d'inscriptionmardi 13 juillet 2010StatutMembreDernière intervention20 août 2012
-
24 févr. 2011 à 11:57
Bonjour,
lors de la sélection d'un fichier par Getopenfilename, je voudrais que pour aller cherhcer le fichier, la boite de dialogue apparaisse avec un repertoire par défault que je lui spécifierais dans le code avant, et non plus dans le répertoire bateau Mes Documents..
Private Declare Function ap_GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Function ap_OpenFile( _
Optional ByVal strFileNameIn As String = "", _
Optional strDialogTitle As String = "Nom du fichier à ouvrir")
Dim lngReturn As Long
Dim intLocNull As Integer
Dim strTemp As String
Dim ofnFileInfo As OPENFILENAME
Dim strInitialDir As String
Dim strFileName As String
Dim strDbName As String
Dim intNbSlashe As Integer
Dim strFileNameDeb As String
'-- Si un chemin d'accès est fourni avec le nom,
'-- l'analyser et le dissocier de celui-ci.
If IsNull(strFileNameIn) Or (strFileNameIn = "") Then
strFileNameIn = CurrentDb.Name
End If
If InStr(strFileNameIn, "") <> 0 Then
strFileName = Left(Mid$(strFileNameIn, _
InStr(strFileNameIn, "") + 1) & _
String(256, 0), 256)
Else
strFileName = Left (strFileNameIn & String(256, 0), 256)
End If
With ofnFileInfo
.lStructSize = Len(ofnFileInfo)
.lpstrFile = strFileName
.lpstrFileTitle = String (256, 0)
.lpstrInitialDir = strInitialDir
.hwndOwner = Application.hWndAccessApp
.lpstrFilter = "All Files (*.txt)" & Chr (0)
.nFilterIndex = 1
.nMaxFile = Len (strFileName)
.nMaxFileTitle = ofnFileInfo.nMaxFile
.lpstrTitle = strDialogTitle
.flags = cdlOFNFileMustExist Or _
cdlOFNHideReadOnly Or _
cdlOFNNoChangeDir
.hInstance = 0
.lpstrCustomFilter = String (255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With
lngReturn = ap_GetOpenFileName(ofnFileInfo)
If (lngReturn = 0) Then
strTemp = ""
Else
'-- Couper toute chaîne Null
strTemp = Trim (ofnFileInfo.lpstrFile)
intLocNull = InStr(strTemp, Chr(0))
If intLocNull Then
strTemp = Left (strTemp, intLocNull - 1)
End If
End If
ap_OpenFile = strTemp
End Function
Dim NomFichier As String
' Exemple avec l'extension d'Access
With Me.dlgNomFichier
.CancelError = True
.Filter = "Base de données Access (*.mdb)|*.mdb"
.FilterIndex = 1
.InitDir = App.Path
On Error GoTo Annuler
.ShowOpen
End With
' la variable Suivante récupère le nom de fichier choisi
NomFichier = Me.dlgNomFichier.FileName
lamquin
Messages postés35Date d'inscriptionvendredi 18 novembre 2005StatutMembreDernière intervention 5 juin 2008 19 juil. 2006 à 14:34
Ok merci, ça me parait bien compliqué pour juste une dialogue box! j'ai trouvé un truc qui fait la même chose, peut être moin bien que ton code mais qui me suffit.
Sub Main()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
'Set the initial path to the C:\ drive.
.InitialFileName = "C:"
'Use the Show method to display the File Picker dialog box and return the user's action.
'If the user presses the action button...
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
MsgBox "Selected item's path: " & vrtSelectedItem
Next vrtSelectedItem
'If the user presses Cancel...
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
louisvbfrance
Messages postés31Date d'inscriptiondimanche 4 mai 2003StatutMembreDernière intervention12 mars 2009 12 mars 2009 à 13:37
J'ai BEAUCOUP plus simple LAMQUIN !!!
ChDrive ("C") 'C est là le truc : changer la lettre du lecteur sur lequel on cherche.
ChDir "C:\Documents and Settings\USER\Bureau\A.I\Factures"
a = Application.GetOpenFilename("fichier excel (*.xls), *.xls", , "Sélection de vos fichiers excel", , True)