OVH Cloud OVH Cloud

Choisir et enregistrer le non d'un fichier

2 réponses
Avatar
Rom1
Bonjour,
J'aimerai par une macro enregistrer dans un variable le nom d'un classeur
choisi à partir d'une fenètre de type "ouvrir" mais sans ouvrir le classeur
en question.
Merci pour votre aide.
Romain

2 réponses

Avatar
Nicolas B.
Salut Rom1,

Un exemple utilisant FileDialog :

Sub Toto()
Dim ofd As FileDialog
Set ofd = Application.FileDialog(msoFileDialogOpen)
ofd.Show
MsgBox ofd.SelectedItems(1)
End Sub


A+
Nicolas B.

Bonjour,
J'aimerai par une macro enregistrer dans un variable le nom d'un classeur
choisi à partir d'une fenètre de type "ouvrir" mais sans ouvrir le classeur
en question.
Merci pour votre aide.
Romain


Avatar
Xav
Bonjour Rom1

Si tu as une version Excel 2000 ou antérieure, FileDialog n'existe pas.

tu peux créer la fonction suivante dans un module.

Tu appelles ensuite la fonction avec GetDirectory

Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

'32-bit API declarations

Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String)
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetDirectory(indice As Integer, Optional msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer

bInfo.pidlRoot = 0&

If IsMissing(msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = msg
End If

bInfo.ulFlags = &H1


x = SHBrowseForFolder(bInfo)
path = VBA.Strings.Space$(512)


r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = VBA.Strings.InStr(path, VBA.Strings.Chr$(0))
GetDirectory = VBA.Strings.Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function

--
Cordialement

@+
Xav
"Rom1" a écrit dans le message de
news:
Bonjour,
J'aimerai par une macro enregistrer dans un variable le nom d'un classeur
choisi à partir d'une fenètre de type "ouvrir" mais sans ouvrir le
classeur

en question.
Merci pour votre aide.
Romain