Get desktop folder

S

Sigurd Bruteig

The following code works with VB6, but when I try to use it with Access 2002
I get an error message about the me. keyword. Can somebody thell me how to
change to fit Access VBA

Option Explicit

Private Declare Function SHGetPathFromIDList Lib "shell32" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As Long) As Long

Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)



Private Sub Form_Load()
Debug.Print UserDeskTopFolder()
End Sub

Public Function UserDeskTopFolder()
' Excerpted and modified from:
' http://www.mvps.org/vbnet/index.html?code/shell/desktoplink.htm
Dim pidl As Long
Dim pos As Long
Dim sPath As String

'fill the pidl with the specified folder item
If SHGetSpecialFolderLocation(Me.hWnd, 0, pidl) = 0 Then
'initialize & get the path
sPath = String$(261, 0)
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
'return folder
UserDeskTopFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
End If

End If
CoTaskMemFree pidl

End Function

Sigurd
 
S

Stephen Lebans

Try changing the Public Function "UserDeskTopFolder()" to Private and
place the code directly in the Form. Otherwise you can either:
1) Pass 0 instead of Me.hWnd
or
2) Change the function so that you pass the Form's hWnd prop directly to
the called function.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
S

Sigurd Bruteig

Stephen Lebans said:
Try changing the Public Function "UserDeskTopFolder()" to Private and
place the code directly in the Form. Otherwise you can either:
1) Pass 0 instead of Me.hWnd
or
2) Change the function so that you pass the Form's hWnd prop directly to
the called function.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.

Your alternative 1 worked well, as I want the code in a module that I can
call when needed. Thank you a lot, and a happy new year.

Sigurd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top