[VBA/VB6] My Documents + Environment Variables

Last update on November 19, 2008 10:17 AM by deri58
Published by deri58

[VBA/VB6] My Documents + Environment Variables




Getting started


As displayed in Windows Explorer, the My Documents folder appears to be in the root, but it is not the case. It is located in a subdirectory of C:\Documents and Settings. The problem is that this subdirectory takes the user name and is changed not only from one PC to another, but also on PC supporting multi users.

The following codes allow you to access the My Documents folder by default regardless of the user logged on.

With VBA


Just paste the following code in a general module:
Option Explicit
Private Type SHITEMID
    cb As Long
    abID As Byte
End Type
Private Type ITEMIDLIST
    mkid As SHITEMID
End Type
Private Const CSIDL_PERSONAL As Long = &H5
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
                        (ByVal hwndOwner As Long, ByVal nFolder As Long, _
                         pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
                        (ByVal pidl As Long, ByVal pszPath As String) As Long


Public Function Rep_Documents() As String
    Dim lRet As Long, IDL As ITEMIDLIST, sPath As String
    lRet = SHGetSpecialFolderLocation(100&, CSIDL_PERSONAL, IDL)
    If lRet = 0 Then
        sPath = String$(512, Chr$(0))
        lRet = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
        Rep_Documents = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
    Else
        Rep_Documents = vbNullString
    End If
End Function

To call the function, simply create a  button and paste in the following code:
Private Sub CommandButton1_Click()
    Cells(5, 2) = Rep_Documents()
End Sub


With VB6


Under VB6, use the environment variable UserProfile (works also with VBA).
Dim sPathUser as String
sPathUser = Environ$("USERPROFILE") & "\mes documents\"
MsgBox sPathUser

Environment Functions


The Environ$ function is use to receive the value of an environment variable.
For example, when you put the WINDIR command (Windows), you would have obtained the folder where Windows is installed (C:\Windows\).
These variables can use in batch file, through the Run utility and in a programming environment such as VB and VBA.
Typing % UserProfile%, it will bring in the current user file.
Typing % UserProfile%\My Documents" it will bring in the My Documents folder.
Typing %WinDir% for the Windows folder... or%tmp% to access temporary files, etc ...

Windows Variables


Variables for default user
TEMP temporary directory
TMP temporary directory
System variables
ComSpec variable path for the command prompt.
FP_NO_HOST_CHECK?
NUMBER_OF_PROCESSORS?
OS Returns OS in use.
Path?
PATHEXT?
PROCESSOR_ARCHITECTURE Returns the processor architecture (x86 etc ...)
PROCESSOR_IDENTIFIER Returns the processor id.
PROCESSOR_LEVEL?
PROCESSOR_REVISION Returns the number of processor revision.
TEMP temporary directory.
TMP temporary directory.
windir folder where Windows is installed.
SystemRoot folder where Windows is installed.

Note:
If you are log in as an administrator, the changes can be made through the Control Panel.
Best answers for « My Documents + Environment Variables » in :
Environment variables Show Environment variables An environment variable is a dynamic value loaded into the memory that can be used by several processes operating simultaneously. On most operating systems, the location of some libraries or of the main system executables may...
Replacing Variables in Bash/shell Show Replacing Variables in Bash/shell Intro: Expression:Defininition Intro: First of all make sure that you the variable is defined before replacing it, or in other cases determine that it is not empty, and to initialize with a default...
Modifying a read-only variable Show Modifying a read-only variable Solution 1 Solution 2 Solution 1 A variable declared as read-only can not be modified during outgoing process. But if the variable is exported in a child shell it may be modified. #!/bin/bash if...
Recover result of command in a variable ShowRecover result of command in a variable For specific reason, it is necessary to recover the result of a command (or back code) in variable to exploit the script later. Below are different examples: Syntax var = $...
How to read a .DOCX document ShowHow to read a .DOCX document Download Microsoft Compatibility Pack Download XML Translator Making use of online Tools For MAC users The new Office 2007 includes a new format for the word documents that bears the .docx extension....
Microsoft word document recovery ShowMICROSOFT WORD DOCUMENT RECOVERY Sometimes large documents containing images files and screenshots, sometimes tends to illogically block Microsoft *Word when you try to modify or edit them. In this case try to proceed as explain below:...
Download Kernel Excel - Repair Corrupted Excel Documents ShowDescription The application is designed by Nucleus Data Recovery.com Pvt. Ltd. Simple and easy to use, the application offers an intuitive interface. It has been awarded from various popular places. Kernel Excel - Repair Corrupted Excel Documents...
Download MS Word Document File Properties Changer ShowDescription The application is designed by VaySoft Groups. Simple and easy to use, the application has been awarded from various places. MS Word Document File Changer is a tool that enables you to change settings of your documents. The application...
Client/Server Environment ShowIntroduction to Client/Server Architecture Numerous applications run in a client/server environment, this means that client computers (computers forming part of the network) contact a server, generally a very powerful computer in terms of...
HTML document layout ShowHTML document An HTML page is a simple file containing text formatted with HTML tags. Traditionally, the file is given an .htm or .html extension, but a web page may have other extensions as well. A web page can be built from even the most basic...
EDM (Electronic document management) ShowElectronic document management EDM (Electronic Document Management or Electronic Content Management, ECM) covers the use of computerized means for the entire management of an electronic document (text file, spreadsheet, image, video, audio file,...