Répertoire mes documents dans macro

dodineau

XLDnaute Occasionnel
bonjour,
Je suis en train de faire une macro qui doit enregistrer un fichier XLS dans le répertoire 'Mes documents' de l'utilisateur.
Mon problème est de savoir comment faire pour accéder à ce répertoire sur n'importe quel PC utilisateur de cette macro.
Par exemple : 'C:\\documents and settings\\ utilisateur de ma macro \\mes documents\\toto.xls'.
J'ai essayé la commande 'chdir=%userprofile%\\mes documents' mais ça ne fonctionne pas.

Merci pour votre aide.

A+

Message édité par: dodineau, à: 17/10/2005 05:46
 

MichelXld

XLDnaute Barbatruc
bonjour

tu peux tester


Sub cheminMesDocuments()
'testé avec Excel2002 & WinXp
Const Cible = &H5 'Mes Documents
Dim objShell As Object
Dim objFolder As Object, objFolderItem As Object

Set objShell = CreateObject('Shell.Application')
Set objFolder = objShell.NameSpace(Cible)
Set objFolderItem = objFolder.Self

MsgBox objFolderItem.Path
End Sub



bonne journée
MichelXld
 

_Thierry

XLDnaute Barbatruc
Repose en paix
Bonjour Michel, Dodineau, le Forum

Ah oui c'est interressant par un Object Shell, moi je passai par des appels API de notre grand Maître :

'Du Grand Laurent Longre Le Grand Chef à Trois Plumes !!

Private Declare Function SHGetSpecialFolderLocation Lib 'Shell32' _
  (ByVal hwnd
As Long, ByVal nFolder As Long, ppidl As Long) As Long

Private Declare Function SHGetPathFromIDList Lib 'Shell32' _
  (ByVal Pidl
As Long, ByVal pszPath As String) As Long

Sub test()
  MsgBox 'Bureau = ' & DSpec(0)
  MsgBox '''Mes Documents'' = ' & DSpec(5)
End Sub

Private Function DSpec(nFolder As Long) As String
 
Dim Pidl As Long
  SHGetSpecialFolderLocation 0, nFolder, Pidl
  DSpec = Space(260)
  SHGetPathFromIDList Pidl, DSpec
  DSpec = Left$(DSpec, InStr(1, DSpec, vbNullChar) - 1)
End Function

Sub ListSpeacialFolders()
Dim i&
   
For i = 0 To 100
    Cells(i + 1, 1) = 'ID N° ' & i
    Cells(i + 1, 2) = DSpec(i)
   
   
Next
End Sub


Sinon j'avais une approche par Shell mais juste valable pour le Bureau :
Sub DeskTop()
Dim ScrHst As Object
Dim Where As String

Set ScrHst = CreateObject('WScript.Shell')
Where = ScrHst.SpecialFolders('DeskTop')
MsgBox Where

End Sub

Bonne Journée
[ol]@+Thierry[/ol]

PS Salut à L.L. Si il me lit ;)
 

MichelXld

XLDnaute Barbatruc
bonsoir Dodineau , bonsoir cher @+Thierry

Thierry , ta solution fonctionne aussi chez moi pour le repertoire spécial 'Mes documents'


Sub mesDocuments()
Dim ScrHst As Object
Dim Where As String

Set ScrHst = CreateObject('WScript.Shell')
Where = ScrHst.SpecialFolders('MyDocuments')
MsgBox Where
End Sub



sinon en reprenant l'exemple dans mon message précédent , vous trouverez ci joint la liste des constantes pour afficher le chemin de tous les dossiers spéciaux

....
Const Cible = &H5 'Mes Documents
....


&H5 = My Documents
&HC = (Virtual) \\My Documents\\
&H27 = \\My Documents\\My Pictures
&H2E = \\Documents
&HD = \\My Documents\\My Music
&HE = \\My Documents\\My Video
&H0 = Virtual Desktop
&H1 = Virtual Internet Explorer (icon on desktop)
&H2 = Start Menu\\Programs
&H3 = Virtual My Computer\\Control Panel
&H4 = Virtual My Computer\\Printers
&H6 = \\Favorites
&H7 = Start Menu\\Programs\\Startup
&H8 = \\Recent
&H9 = \\SendTo
&HA = Virtual \\Recycle Bin
&HB = \\Start Menu
&H10 = \\Desktop
&H11 = Virtual My Computer
&H12 = Virtual Network Neighborhood
&H13 = \\nethood (may dupe My Network Places)
&H14 = Virtual windows\\fonts
&H15 = \\templates
&H16 = \\Start Menu
&H17 = \\Programs
&H18 = \\Startup
&H19 = \\Desktop
&H1A = \\Application Data
&H1B = \\PrintHood
&H1C = \\Local Settings\\Applicaiton Data (non roaming)
&H1D = nonlocalized startup program group
&H1E = (NT) nonlocalized Startup group for all NT users
&H1F = (NT) all user's favorite items
&H20 = temporary Internet files
&H21 = (NT) Internet cookies
&H22 = (NT) Internet history items
&H23 = \\Application Data
&H24 = Windows directory or SYSROOT
&H25 = GetSystemDirectory()
&H26 = \\Program Files
&H28 = \\
&H29 = x86 system directory on RISC
&H2A = x86 Program Files folder on RISC
&H2B = \\Program Files\\Common
&H2C = x86 Program Files Common folder on RISC
&H2D = \\Templates
&H2F = \\Start Menu\\Programs\\Administrative Tools
&H30 = \\Start Menu\\Programs\\Administrative Tools
&H31 = Virtual Network and dial-up connections folder
&H35 = My Music folder for all users
&H36 = My Pictures folder for all users
&H37 = My Video folder for all users
&H38 = System resource directory
&H39 = Localized resource directory
&H3A = Links to OEM specific apps for all users
&H3B = \\Local Settings\\Application Data\\Microsoft\\CD Burning
&H3D = Virtual Computers Near Me folder



bonne soiree
MichelXld
 

Discussions similaires

Statistiques des forums

Discussions
312 327
Messages
2 087 314
Membres
103 515
dernier inscrit
Cherbil12345