locked
Are there VBA built-in functions to read .ini files? RRS feed

  • Question

  • Hi there,

    I'm trying to find a VBA built in function or set of functions to parse configuration information from .ini files.

    I see GetSetting and SaveSetting available, but I prefer not to use the registry for my users' configuration settings, but an old-fashioned .ini file.

    I know I can write these functions, but I'd rather not and thought VBA might offer that out-of-the-box.

    Friday, April 20, 2012 2:38 PM

All replies

  • There are API routines you can use in VBA

    http://vbadud.blogspot.co.uk/2008/11/how-to-read-and-write-configuration.html

    Cheers,

    Andy
    www.andypope.info

    Friday, April 20, 2012 3:05 PM
  • There is the Open statement for opening a text file and then accompanying read and write statements to read text.

    Open "TESTFILE" For Input As #1
    ' Close before reopening in another mode.
    Close #1
    


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management

    Saturday, April 21, 2012 6:31 AM
  • Not in the VBA language, but if you happen to be using Word VBA, there's a System.PrivateProfileString property that lets you do it.

    Peter Jamieson

    Saturday, April 21, 2012 2:10 PM
  • Hi matklaer,

    Thanks for posting in the MSDN Forum.

    I'm not sure about the mean of "VBA built-in" that you mentioned.

    As usual the only way we used to access ini file is invoke Win32 function. following is a sample:

    Option Explicit
       'read
       Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
                     (ByVal lpApplicationName As String, _
                       ByVal lpKeyName As String, _
                       ByVal lpDefault As String, _
                       ByVal lpReturnedString As String, _
                       ByVal nSize As Long, _
                       ByVal lpFileName As String) As Long
       'write
       Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
                     (ByVal lpApplicationName As String, _
                       ByVal lpKeyName As String, _
                       ByVal lpString As String, _
                       ByVal lpFileName As String) As Long
    
    Sub Read()
        Dim Rec As String
        Dim NC As Long
        
        Rec = String(255, 0)
        NC = GetPrivateProfileString("BOOT", "Shell", "", Rec, 255, "*******\tmp1.ini")
        MsgBox Rec
        
    End Sub
    
    Sub WriteProfile()
        Dim NC As Long
        NC = WritePrivateProfileString("BOOT", "Shell", "Test1", "******\tmp1.ini")
    End Sub

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by johny w Wednesday, May 25, 2016 12:43 AM
    Monday, April 23, 2012 3:12 AM