WD2000: How to Find the Word Startup-Path Using an External Solution

ID: Q210860


The information in this article applies to:


SUMMARY

A driving goal of Office 2000 is to reduce total cost of software ownership (TCO) and comply with the ZAW (Zero Administration Windows) standard to make it easier and more cost-effective for organizations to deploy and manage application installations. ZAW standards emphasize writing as little to the registry as possible, and in the spirit of ZAW and customer requests, Office 2000 has significantly reduced adding information to the registry.

In keeping to these standards, Word 2000 (and Office 2000) will not write out the Startup-Path key to the registry when using the new Roaming User feature and/or User Profiles in Windows, unless the path is specifically changed by the user.

This article provides a method you can use to obtain the current user's Startup folder path, which you can incorporate into an Office 2000 solution.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/refguide/
The proper way to install a solution in Office 2000 is to use the SHGetSpecialFolderLocation Windows API function to return the current user application folder and append "Microsoft\Word\Startup" to it. The name of the folder is typically:

C:\Windows\Application Data
-or-
C:\Windows\Profiles\username\Application Data


where username is a particular user name.

NOTE: Using the SHGetSpecialFolderLocation API may fail if the user manually changes the location of his or her Startup folder (using Tools/Options/File Locations) to a different location. The API will return the location of the current user's application folder with "Microsoft\Word\Startup" appended to it, and this may not be the proper location.

To determine whether the user has changed the location of the Startup folder, your application can first check the Microsoft Word registry. If the following Startup-Path registry entry exists, the user has changed the folder location or is not using User Profiles:
HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Word\Options\STARTUP-PATH
If this registry entry does not exist, you can then use the SHGetSpecialFolderLocation API to return the current user's Startup folder location.

The following example code demonstrates how to retrieve the Word Startup path in Visual Basic or Visual Basic for Applications.

Enter the following code in the General Declarations section of a global module of your Visual Basic project:

NOTE: The following procedures require the following:
Word 2000 or later
Windows 98 or later
Windows NT 4.0 or later



' ====== Begin General Declarations Module ===========

' ----Begin SHGetSpecialFolderLocation API Declarations----

' Declare Public variables.
Public Type ShortItemId
   cb As Long
   abID As Byte
End Type

Public Type ITEMIDLIST
   mkid As ShortItemId
End Type
   
' Declare API functions.
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _
   (ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHGetSpecialFolderLocation Lib _
   "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder _
   As Long, pidl As ITEMIDLIST) As Long

' ----End SHGetSpecialFolderLocation API Declarations----

' ----Begin Get 32 Bit Registry Entry Value Declarations----
Const HKEY_CURRENT_USER = &H80000001
Const ERROR_SUCCESS = 0&
Const REG_DWORD = 4
Const REG_BINARY = 3
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2 ' Unicode nul terminated string
Const ERROR_NONE = 0
Const ERROR_BADDB = 1
Const ERROR_BADKEY = 2
Const ERROR_CANTOPEN = 3
Const ERROR_CANTREAD = 4
Const ERROR_CANTWRITE = 5
Const ERROR_OUTOFMEMORY = 6
Const ERROR_ARENA_TRASHED = 7
Const ERROR_ACCESS_DENIED = 8
Const ERROR_INVALID_PARAMETERS = 87
Const ERROR_NO_MORE_ITEMS = 259
Const KEY_ALL_ACCESS = &H3F
Const REG_OPTION_NON_VOLATILE = 0

Declare Function RegOpenKeyEx Lib "advapi32.dll" _
    Alias "RegOpenKeyExA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, ByVal ulOptions As Long, _
    ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
    String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
    As String, lpcbData As Long) As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
    String, ByVal lpReserved As Long, lpType As Long, lpData As _
    Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
    String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
    As Long, lpcbData As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData _
   As Long) As Long

' ----End Get 32 Bit Registry Entry Value Declarations----
' ====== End General Declarations Module =========== 


Public Function QueryValue(sKeyName As String, _
   sValueName As String)
' This function, in conjunction with the QueryValueEx Function
' will return a specified registry entry.

   Dim lRetVal As Long      ' Result of the API functions.
   Dim hKey As Long         ' Handle of opened key.
   Dim vValue As Variant    ' Setting of queried value.

   lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _
      KEY_ALL_ACCESS, hKey)

   lRetVal = QueryValueEx(hKey, sValueName, vValue)
   QueryValue = vValue
   RegCloseKey (hKey)

End Function 


Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
   String, vValue As Variant) As Long
   
' This function, in conjunction with the QueryValue Function
' will return a specified registry entry.

   Dim cch As Long
   Dim lrc As Long
   Dim lType As Long
   Dim lValue As Long
   Dim sValue As String

   On Error GoTo QueryValueExError

   ' Determine the size and type of data to be read
   lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
   If lrc <> ERROR_NONE Then Error 5
   Select Case lType
       ' For strings
      Case REG_SZ:
         sValue = String(cch, 0)
           lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
              sValue, cch)
           If lrc = ERROR_NONE Then
               vValue = Left$(sValue, cch - 1)
           Else
               vValue = Empty
           End If
      Case REG_EXPAND_SZ:
         sValue = String(cch, 0)
         lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
            sValue, cch)
         If lrc = ERROR_NONE Then
             vValue = Left$(sValue, cch - 1)
         Else
             vValue = Empty
         End If
      Case REG_DWORD:
         lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
            lValue, cch)
         If lrc = ERROR_NONE Then vValue = lValue
       Case Else
         'all other data types not supported
         lrc = -1
   End Select

QueryValueExExit:
   QueryValueEx = lrc
   Exit Function
QueryValueExError:
   Resume QueryValueExExit
End Function 


Function GetSpecialFolder() As String

' This function returns the StartUp Folder Path found
' in the Current Users Profile.

   Dim idlstr As Long
   Dim sPath As String
   Dim IDL As ITEMIDLIST
   Const NOERROR = 0
   Const MAX_LENGTH = 260
   Const CSIDL_APPDATA = &H1A
    
   On Error GoTo Err_GetFolder

   ' Fill the idl structure with the specified folder item.
   idlstr = SHGetSpecialFolderLocation(0, CSIDL_APPDATA, IDL)

   If idlstr = NOERROR Then
       ' Get the path from the idl list, and return
       ' the folder with a slash at the end.
       sPath = Space$(MAX_LENGTH)
       idlstr = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
       If idlstr Then
           GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) & "\"
       End If
   End If

Exit_GetFolder:
    Exit Function

Err_GetFolder:
   MsgBox "An Error was Encountered" & Chr(13) & Err.Description, _
      vbCritical Or vbOKOnly
   Resume Exit_GetFolder

End Function 

NOTE: Run the following procedure to return the values from the functions QueryValue, QueryValueEx, and GetSpecialFolder.

Sub GetWordStartUpPath()

   Dim sKey As String
   Dim sVal As String
   Dim sPath As Variant
   
   ' Set Key and Value to lookup.
   sKey = "Software\Microsoft\Office\9.0\Word\Options"
   sVal = "STARTUP-PATH"

   ' Check for user specified Start-Up Path.
   sPath = QueryValue(sKey, sVal)
   
   ' If the user specified Start-Up Path does not exist
   ' or it is empty, look for user profile Start-Up path.
   If sPath = ERROR_BADKEY Or sPath = "" Or IsEmpty(sPath) Then
      sPath = GetSpecialFolder() & "Microsoft\Word\Startup"
   End If
   
   ' Display the VALID StartUp Folder path.
   MsgBox sPath

End Sub 


Note that if you are creating a solution within Word via Visual Basic for Applications, you can retrieve the current Startup path by using the following single command line:


   MsgBox Options.DefaultFilePath(Path:=wdStartupPath) 


For more information about using the sample code in this article, please see the following article in the Microsoft Knowledge Base:
Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


REFERENCES

For additional information about using the registry APIs to save and retrieve a registry setting, please see the following article in the Microsoft Knowledge Base:

Q145679 HOWTO: Use the Registry API to Save and Retrieve Setting


For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
Q226118 OFF2000: Programming Resources for Visual Basic for Applications

Additional query words:


Keywords          : kbdta kbwordvba wd2000 
Version           : WINDOWS:2000; winnt:4.0,4.0 SP1,4.0 SP2,4.0 SP3,4.0 SP4
Platform          : WINDOWS winnt 
Issue type        : kbinfo 

Last Reviewed: June 15, 1999