Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts

    "False" is "True" problem: Word 2007, getting value from .ini file

    Yikes.

    I have a perplexing situation. I am running Office 2007 in a Virtual PC (OS is Win XP SP3). I have a template that checks an .ini file for a string value of either "true" or "false" (this is not Boolean, just plain text for what I'm checking). If the value is "false" then a dialog appears. If the value is "true" then the dialog is bypassed. If, for some reason, the .ini file is not found, there is a default value of "false" passed to the string.

    The perplexing thing is that the default value in the .ini file is "false" -- no ambiguity and no sneaky code changing it when I can't see. However, the string returned is "true." !! I added a line of code to explicitly set the string to "" before running the code that checks the .ini file.

    To add to the perplexity, I can run this same code in this same template in Office 2007 on another Virtual PC (OS is the same), on another computer. And it works correctly. The only difference is that one computer is running

    I've stepped through the code and the string variable is definitely being set to true in the line that queries the .ini file, and where the value is set to "false." I've searched for multiple versions of the .ini file and there are none (nor would the code be able to find them). I don't know what else to look for.
    Where in Heaven's name might this value be coming from?

    Thanks, Kim

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,506
    Thanks
    213
    Thanked 854 Times in 786 Posts
    Kim,

    Could you post the code and a copy of the .ini file?
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    223
    Thanks
    0
    Thanked 21 Times in 20 Posts
    Have you tried reading the ini file line by line within Word VBA, just to see what gets dumped out?

  4. #4
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    RetiredGeek and jeremybarker, thank you for your responses. I tweaked it and it's now working, but the perplexing part remains. Here is the code in the ThisDocument module:

    Code:
    Public Sub Document_New()
    Dim strDo As String
    strDo = ""
    If strAppData = "" Then GetEnvironmentStrings (True)
    strDo = GetSetting(strAppData & "\Settings\Do.ini", "DocumentNew", "value")
    If strDo = "false" Then
        modTemplate.StartDialog (True)
    Else
        Exit Sub
    End If
    End Sub
    As you can see from the strDo = GetSettings... line, I did not have a default value set, as I said in my first post. But that still should not have kept the code from reading the string from the ini file. (Adding a default value was a late addition to already working code.) At the worst (not finding a value, or not finding the file at all) it should have returned an empty string. It certainly should never have returned "true." However, the strDo line now reads:

    Code:
    strDo = GetSetting(strAppData & "\Settings\Do.ini", "DocumentNew", "value", "false")
    strDo = "" was intended to make certain there was no text in the string variable. When I stepped through the code it seemed to get "true" out of thin air. I explicitly defined strDo as a string variable so it wouldn't accidentally return "true" and "false" as a Boolean value.

    The GetEnvironmentStrings sub simply retrieves the Application Data folder for either Win XP or Win 7. The Settings folder is there.

    Here is the *.ini file (in its entirety):

    Code:
    [DocumentNew]
    value=false
    This is only ever set to "true" by a specific and isolated process that sets it back to "false" when completed. I am certain beyond a doubt that it was never "true" during my testing yesterday. And as I said before, this worked everywhere I tried it except this one VPC.

    I believe there was something wrong that I've now corrected. At any rate, it's working now, on all computers.

    Best, Kim

  5. #5
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    223
    Thanks
    0
    Thanked 21 Times in 20 Posts
    Quote Originally Posted by kmurdock View Post
    RetiredGeek and jeremybarker, thank you for your responses. I tweaked it and it's now working, but the perplexing part remains. Here is the code in the ThisDocument module:

    Code:
    Public Sub Document_New()
    Dim strDo As String
    strDo = ""
    If strAppData = "" Then GetEnvironmentStrings (True)
    strDo = GetSetting(strAppData & "\Settings\Do.ini", "DocumentNew", "value")
    If strDo = "false" Then
        modTemplate.StartDialog (True)
    Else
        Exit Sub
    End If
    End Sub
    As you can see from the strDo = GetSettings... line, I did not have a default value set, as I said in my first post. But that still should not have kept the code from reading the string from the ini file. (Adding a default value was a late addition to already working code.) At the worst (not finding a value, or not finding the file at all) it should have returned an empty string. It certainly should never have returned "true." However, the strDo line now reads:

    Code:
    strDo = GetSetting(strAppData & "\Settings\Do.ini", "DocumentNew", "value", "false")
    strDo = "" was intended to make certain there was no text in the string variable. When I stepped through the code it seemed to get "true" out of thin air. I explicitly defined strDo as a string variable so it wouldn't accidentally return "true" and "false" as a Boolean value.

    The GetEnvironmentStrings sub simply retrieves the Application Data folder for either Win XP or Win 7. The Settings folder is there.

    Here is the *.ini file (in its entirety):

    Code:
    [DocumentNew]
    value=false
    This is only ever set to "true" by a specific and isolated process that sets it back to "false" when completed. I am certain beyond a doubt that it was never "true" during my testing yesterday. And as I said before, this worked everywhere I tried it except this one VPC.

    I believe there was something wrong that I've now corrected. At any rate, it's working now, on all computers.

    Best, Kim
    I'm probably misunderstanding your situation totally, but when I Google for help on GetSetting it tells me that this is the function to read from the Registry not an ini file. Are you sure you're getting values from where you think you are?

  6. #6
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    Hi jeremybarker,

    Yes, it works with an ini file, too. Google "GetSettings ini" (without quotes) and you'll get many hits on how this works.

    Best, Kim

  7. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,506
    Thanks
    213
    Thanked 854 Times in 786 Posts
    Kim,

    Here's what I noticed while looking at the code.

    First: I assume strAppData is a Global or Module level variable since it isn't initialized in the Document_New() code.
    Second: You were missing a End If for the If strAppData test.
    Third: This is just a personal preference but i would code the test this way
    If UCase(strDo) = "FALSE"
    Now you don't have to worry about how the ini value is typed.

    I'd try the debug statement included below to make sure what is being returned from the ini file just in case.

    You probablly already know this stuff but just in case you missed something.
    Code:
    Public Sub Document_New()
    
      Dim strDo As String
    
      strDo = ""
    
      If strAppData = "" Then 
        GetEnvironmentStrings (True)
        strDo = GetSetting(strAppData & "\Settings\Do.ini", "DocumentNew", "value")
    
    ' ***  Debug Statement ***
        MsgBox "strDo = " & strDo, vbokonly+vbinformation, _
               "Value from INI file:"
    
        If strDo = "false" Then
          modTemplate.StartDialog (True)
        Else
          Exit Sub
        End If  'strDo
    
      End If    'strAppData
    
    End Sub
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  8. #8
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    Thank you, RetiredGeek.

    • strAppData is indeed a global variable used by several routines.
    • The value in the ini file is always provided by code and never typed by a user, so I always know it is lower case. On the other hand, when I add new template, I do have to remember to type the value in lower case, so your suggestion is a good one.
    • An End If isn't necessary because the Then statement follows on the same line. Just one more way to streamline code...

    If strAppData = "" Then GetEnvironmentStrings (True)


    But no matter what, it's all a little better with
    Thanks again, Kim

  9. #9
    New Lounger
    Join Date
    Dec 2009
    Location
    Sherwood, OR, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am having a problem with what you are saying here. The GetSetting function is for retrieving data from the Registry, under HKEY_CURRENT_USER\Software\VB and VBA Program Settings, not from a file.
    Jack

  10. #10
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    Hi Jack,

    See my response to jeremybarker, above. It works with files, too.

    Best, Kim

  11. #11
    New Lounger
    Join Date
    Dec 2009
    Location
    Sherwood, OR, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have tried your suggested Google search and not found any links refer to using the GetSetting function to work with anything other than the registry.

    I have also used your posted code...it returns nothing until I use the reverse SetSetting function first, which creates a registry entry.

    Are you sure you are not reading from the registry? Have you changed your INI file manually with NotePad and verified you got the new value retrieved?
    Last edited by JackStockton; 2012-04-07 at 12:32.
    Jack

  12. #12
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    To JackStockton and jeremybarker --

    You are both right. GetSetting is indeed intended to access the registry. I posted the wrong code and defended the wrong function!

    The correct line of code should be:

    Code:
    strDo = System.PrivateProfileString(strAppData & "\Settings\Do.ini", "DocumentNew", "value")
    ...which is the way to address an ini file. I was typing 'way faster than I was thinking and I apologize for the confusion.

    Best, Kim

  13. #13
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,506
    Thanks
    213
    Thanked 854 Times in 786 Posts
    Kim,

    I'm a little confused by your use of:
    If strAppData = "" Then GetEnvironmentStrings (True).

    According to my research GetEnvironmentstrings doesn't take an argument and it returns all the environment strings currently available. I ran this test code within Excel:
    Code:
    Option Explicit
    
    Private Declare Function GetEnvironmentStrings Lib "kernel32" Alias "GetEnvironmentStringsA" () As Long
    Private Declare Function FreeEnvironmentStrings Lib "kernel32" Alias "FreeEnvironmentStringsA" (ByVal lpsz As String) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
    Private Sub Form_Load()
        Dim lngRet As Long, strDest As String, lLen As Long
        'retrieve the initial pointer to the environment strings
        lngRet = GetEnvironmentStrings
        Do
            'get the length of the following string
            lLen = lstrlen(lngRet)
            'if the length equals 0, we've reached the end
            If lLen = 0 Then Exit Do
            'create a buffer string
            strDest = Space$(lLen)
            'copy the text from the environment block
            CopyMemory ByVal strDest, ByVal lngRet, lLen
            'show the text
           Debug.Print strDest
            'move the pointer
            lngRet = lngRet + lstrlen(lngRet) + 1
        Loop
    End Sub
    What I got back in the Immediate window was:
    =C:=C:\Program Files (x86)\Microsoft Office\Office14
    =G:=G:\BEKDocs\Excel
    ALLUSERSPROFILE=C:\ProgramData
    APPDATA=C:\Users\Bruce\AppData\Roaming
    CommonProgramFiles=C:\Program Files (x86)\Common Files
    CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
    CommonProgramW6432=C:\Program Files\Common Files
    COMPUTERNAME=BRUCES-PC
    ComSpec=C:\Windows\system32\cmd.exe
    devmgr_show_nonpresent_devices=1
    FP_NO_HOST_CHECK=NO
    HOMEDRIVE=C:
    HOMEPATH=\Users\Bruce
    LOCALAPPDATA=C:\Users\Bruce\AppData\Local
    LOGONSERVER=\\BRUCES-PC
    MSOffice=C:\Program Files (x86)\Microsoft Office\OFFICE11
    MyDocs=G:\BEKDocs
    NUMBER_OF_PROCESSORS=4
    OS=Windows_NT
    Path=C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Wi ndows;C:\Windows\System32\Wbem;C:\Windows\System32 \WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files\Intel\DMIX;C:\Program Files\Microsoft Windows Performance Toolkit\
    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WS F;.WSH;.MSC
    PROCESSOR_ARCHITECTURE=x86
    PROCESSOR_ARCHITEW6432=AMD64
    PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 15 Stepping 11, GenuineIntel
    PROCESSOR_LEVEL=6
    PROCESSOR_REVISION=0f0b
    ProgramData=C:\ProgramData
    ProgramFiles=C:\Program Files (x86)
    ProgramFiles(x86)=C:\Program Files (x86)
    ProgramW6432=C:\Program Files
    PSModulePath=C:\Windows\system32\WindowsPowerShell \v1.0\Modules\
    PUBLIC=C:\Users\Public
    SESSIONNAME=Console
    SystemDrive=C:
    SystemRoot=C:\Windows
    TEMP=C:\Users\Bruce\AppData\Local\Temp
    TMP=C:\Users\Bruce\AppData\Local\Temp
    USERDOMAIN=Bruces-PC
    USERNAME=Bruce
    USERPROFILE=C:\Users\Bruce
    WecVersionForRosebud.11EC=4
    windir=C:\Windows

    I see no code in your example to parse this mess and get the value you need into strAppData. Am I missing something?
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  14. #14
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    223
    Thanks
    0
    Thanked 21 Times in 20 Posts
    Quote Originally Posted by kmurdock View Post
    To JackStockton and jeremybarker --

    You are both right. GetSetting is indeed intended to access the registry. I posted the wrong code and defended the wrong function!

    The correct line of code should be:

    Code:
    strDo = System.PrivateProfileString(strAppData & "\Settings\Do.ini", "DocumentNew", "value")
    ...which is the way to address an ini file. I was typing 'way faster than I was thinking and I apologize for the confusion.

    Best, Kim
    No worries...like Jack I'd had no success with a Google search, but just assumed I'd missed something!

  15. #15
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    668
    Thanks
    3
    Thanked 21 Times in 19 Posts
    RetiredGeek,

    Well. How about that? Let me 'splain what I've been up to and what I ought to do about it.

    GetEnvironmentStrings is a routine I wrote myself years ago, with only a Boolean argument. It returns the environment variables I need to use and assigns them to string variables. I've been using this since before Win XP (which Msoft seems to say is the minimum O/S under which it can be used). I wasn't aware that it was added to the Windows API with XP. Further, it's been working for me for years.

    I'm not declaring the Windows API, so Windows has no idea I've stolen its function. Now that I know, it behooves me to change the sub to another name to avoid confusion.

    Thanks! Kim

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •