[VBA] How to locate the Documents folder?!?

simurq

Board Regular
Joined
Nov 11, 2011
Messages
73
Good evening!

I'm using the following piece of code (found on the net) to determine the operating system. It works as required (determines OS) but my real challenge is to locate the Documents folder regardless of OS locale or name of Documents folder in another language. I searched the registry and OS environment variables but none of them points to a "stable" location which I can use in my project. Can you pls help to modify the code?

Thanks!

Code:
Option Explicit


Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type


Declare PtrSafe Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer


Public Function getVersion() As String
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer


    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)

    Select Case osinfo.dwMajorVersion + osinfo.dwMinorVersion / 10
        Case 5.1, 5.2               '<-- Check if Windows XP
            ChDir (Environ("USERPROFILE") & "\My Documents")
            'Do something else
        Case 6#, 6.1               '<-- Check if Windows Vista or 7
            ChDir (Environ("USERPROFILE") & "\Documents")
            'Do something else
        Case Else
            getVersion = "Other version"
    End Select
End Function
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Does this work for you?

Code:
Sub MyDocs()
    MsgBox CreateObject("WScript.Shell").SpecialFolders("mydocuments")
End Sub
 
Upvote 0
It works like a charm, thank you very much!!!

I applied your code to my needs as this:

Code:
ChDir (CreateObject("WScript.Shell").SpecialFolders("mydocuments"))
 
Upvote 0
I assume that it will also work when there are multiple user accounts on the same rig, right?
I just tested it on my rig with two "Desktop" folders on two different accounts (Public and User) and it opens the one on a user account from which the code is executed.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top