What am I missing (again) (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 01:22
Joined
Sep 21, 2011
Messages
14,260
Hi all,
I found a piece of code that I *thought* would allow me to get the path of special folders.

Code:
Function GetSpecialFolderNames()
Dim objFolders As Object
Set objFolders = CreateObject(“WScript.Shell”).SpecialFolders
MsgBox objFolders(“desktop”)
MsgBox objFolders(“allusersdesktop”)
MsgBox objFolders(“sendto”)
MsgBox objFolders(“startmenu”)
MsgBox objFolders(“recent”)
MsgBox objFolders(“favorites”)
MsgBox objFolders(“mydocuments”)
End Function

Fine I thought, I can tailor that, and even use it to choose which folder I am interested in.?

So I was going to create some code and use the Select Case statement to return the required path.

Then I realised I could just use the name of the folder and created

Code:
Function GetSpecialFolderPath(pstrFolderRequired As String) As String
Dim objFolders As Object
Dim strFolderPath As String
Set objFolders = CreateObject("WScript.Shell").SpecialFolders

strFolderPath = objFolders(pstrFolderRequired)

'Debug.Print objFolders("desktop")
'Debug.Print objFolders("allusersdesktop")
'Debug.Print objFolders("sendto")
'Debug.Print objFolders("startmenu")
'Debug.Print objFolders("recent")
'Debug.Print objFolders("favorites")
'Debug.Print objFolders("mydocuments")

Debug.Print strFolderPath
GetSpecialFolderPath = strFolderPath
Set objFolders = Nothing

however if I use ? getspecialfolderpath("mydocuments") in the debug window, I get
C:\Users\Public\Desktop
and not
C:\Users\Paul\Documents
 

CJ_London

Super Moderator
Staff member
Local time
Today, 01:22
Joined
Feb 19, 2013
Messages
16,605
try removing the type for pstrFolderRequired to default to variant

Function GetSpecialFolderPath(pstrFolderRequired) As String

or

Function GetSpecialFolderPath(pstrFolderRequired As Variant) As String
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:22
Joined
Sep 21, 2011
Messages
14,260
CJ_London,

That was it. :confused:

I spent over an hour trying to get to the bottom of it.:banghead:

Is there any particular reason why it does this.?
I thought I was doing it correctly by specifying all the types.

try removing the type for pstrFolderRequired to default to variant

Function GetSpecialFolderPath(pstrFolderRequired) As String

or

Function GetSpecialFolderPath(pstrFolderRequired As Variant) As String
 

CJ_London

Super Moderator
Staff member
Local time
Today, 01:22
Joined
Feb 19, 2013
Messages
16,605
seems you need to pass a variant datatype, not a string. I just googled 'windows SpecialFolders' and found several threads on the subject.

I suspect the reason why is specialfolders is an enum. So if you know the related number, you could use that instead (myDocuments is 5).

See this link, it's in German and relates to C#, but you'll get the gist

https://docs.microsoft.com/de-de/do...r?redirectedfrom=MSDN&view=netframework-4.7.2
 

Users who are viewing this thread

Top Bottom