• privateprofilestring (VBA6/Word2000/Outlook2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » privateprofilestring (VBA6/Word2000/Outlook2000)

    Author
    Topic
    #395645

    I am trying to read Outlook’s master category list from the registry while in a Word macro (finding Contacts). I am able to do this using
    privateprofilestring (“”, “HKEY_CURRENT_USERSoftwareMicrosoftOffice9.0OutlookCategories”, “MasterList”).

    But once the string is over 255 characters, I get a zero length string. I have been trying to understand the API function getprivateprofilestring, but do not understand how I use it. Is there a way to get a string out of a registry entry that is greater than 255 characters?

    Thanks!

    Viewing 2 reply threads
    Author
    Replies
    • #737926

      The following code will read registry strings up to 1023 characters long. If you need even longer strings, modify the single occurrence of 1024 in the code to be one more than the maximum length you expect.

      Public Const REG_SZ = 1
      Public Const HKEY_CURRENT_USER = &H80000001
      Public Const HKEY_LOCAL_MACHINE = &H80000002
      Public Const KEY_QUERY_VALUE = &H1

      Declare Function RegCloseKey Lib “advapi32.dll” _
      (ByVal hKey As Long) As Long

      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 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

      Function ReadString _
      (RootKey As Long, KeyName As String, ValueName As String) As String
      Dim lngKey As Long
      Dim strRes As String
      Dim intPos As Integer
      Dim lngBytes As Long
      Dim lngType As Long

      If RegOpenKeyEx _
      (RootKey, KeyName, 0, KEY_QUERY_VALUE, lngKey) = 0 Then
      strRes = String(1024, 0)
      lngBytes = Len(strRes)
      If RegQueryValueEx _
      (lngKey, ValueName, 0, lngType, ByVal strRes, lngBytes) = 0 Then
      If lngType = REG_SZ Then
      ReadString = Left(strRes, lngBytes – 1)
      End If
      End If
      RegCloseKey lngKey
      End If
      End Function

      Example:

      Dim strMasterList As String
      strMasterList = ReadString(HKEY_CURRENT_USER, _
      “SoftwareMicrosoftOffice9.0OutlookCategories”, “MasterList”)

    • #737927

      The following code will read registry strings up to 1023 characters long. If you need even longer strings, modify the single occurrence of 1024 in the code to be one more than the maximum length you expect.

      Public Const REG_SZ = 1
      Public Const HKEY_CURRENT_USER = &H80000001
      Public Const HKEY_LOCAL_MACHINE = &H80000002
      Public Const KEY_QUERY_VALUE = &H1

      Declare Function RegCloseKey Lib “advapi32.dll” _
      (ByVal hKey As Long) As Long

      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 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

      Function ReadString _
      (RootKey As Long, KeyName As String, ValueName As String) As String
      Dim lngKey As Long
      Dim strRes As String
      Dim intPos As Integer
      Dim lngBytes As Long
      Dim lngType As Long

      If RegOpenKeyEx _
      (RootKey, KeyName, 0, KEY_QUERY_VALUE, lngKey) = 0 Then
      strRes = String(1024, 0)
      lngBytes = Len(strRes)
      If RegQueryValueEx _
      (lngKey, ValueName, 0, lngType, ByVal strRes, lngBytes) = 0 Then
      If lngType = REG_SZ Then
      ReadString = Left(strRes, lngBytes – 1)
      End If
      End If
      RegCloseKey lngKey
      End If
      End Function

      Example:

      Dim strMasterList As String
      strMasterList = ReadString(HKEY_CURRENT_USER, _
      “SoftwareMicrosoftOffice9.0OutlookCategories”, “MasterList”)

    • #738068

      Hans

      Thanks! This works perfectly in Word 2000.

      In Word XP the registry key value is now BINARY. Is there any way to adjust for this?

      Thanks,

      David

      • #738083

        What would you like to do with a binary registry value? If you really have to inspect it, use RegEdit.

      • #738084

        What would you like to do with a binary registry value? If you really have to inspect it, use RegEdit.

    Viewing 2 reply threads
    Reply To: privateprofilestring (VBA6/Word2000/Outlook2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: