Consulting

Results 1 to 6 of 6

Thread: Ini. Files problem

  1. #1
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    3
    Location

    Ini. Files problem

    Hi All,
    I have another problem, I have a vba program that read and save to an .ini files. When the program launch, it will read from the previous .ini files that has been saved.
    I have added some new textbox in my form. When it reads from a older .ini files, half of the information that was in the form disappear and require input. If i open those files after the upgrade
    it works just fine.
    What can i do to enable the new upgrade and read the old .ini files as well?
    Please advise

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    A bit more info, some code, the workbook?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    3
    Location
    [vba]
    Option Explicit
    Private Declare Function GetPrivateProfileStringA Lib "Kernel32" _
    (ByVal strSection As String, ByVal strKey As String, _
    ByVal strDefault As String, ByVal strReturnedString As String, _
    ByVal lngSize As Long, ByVal strFileNameName As String) As Long
    Private Declare Function WritePrivateProfileStringA Lib "Kernel32" _
    (ByVal strSection As String, ByVal strKey As String, _
    ByVal strString As String, ByVal strFileNameName As String) As Long
    'Write .ini setting
    'strFileName = .ini file name
    'strSection = subsection in the .ini file (ex. [end])
    'strKey = the key you want to write (ex. s end)
    'strvalue = the value of the key (ex. s)
    Public Function WritePrivateProfileString32(ByVal strFileName As String, _
    ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
    Dim lngValid As Long
    On Error Resume Next
    lngValid = WritePrivateProfileStringA(strSection, strKey, strValue, strFileName)
    If lngValid > 0 Then WritePrivateProfileString32 = True
    On Error GoTo 0
    End Function
    'Retrieve .ini setting
    'strFileName = .ini file name
    'strSection = subsection in the .ini file (ex. [end])
    'strKey = the key you want to write (ex. lend)
    'strDefault = I dunno yet...
    Public Function GetPrivateProfileString32(ByVal strFileName As String, _
    ByVal strSection As String, ByVal strKey As String, Optional strDefault) As String
    Dim strReturnString As String, lngSize As Long, lngValid As Long
    On Error Resume Next
    If IsMissing(strDefault) Then strDefault = "0"
    strReturnString = Space(1024)
    lngSize = Len(strReturnString)
    lngValid = GetPrivateProfileStringA(strSection, strKey, strDefault, strReturnString, _
    lngSize, strFileName)
    GetPrivateProfileString32 = Left(strReturnString, lngValid)
    On Error GoTo 0
    End Function[/vba]
    Edit Lucas: VBA tags and line breadks added
    wkon, please read our faq. When posting code please select your code and hit the vba button. Use of line breaks are appreciated......line breaks added to your code.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    A bit more info, some code, the workbook?
    Someone has responded to your first post with a couple of questions about what your are trying to do.
    You have provided some code so we are hoping you will be following that up with some more information to help solve your problem.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    3
    Location
    Sorry, I am a new lounger
    I call the function in a form to save the .ini files.
    for example,
    [VBA]Data.WritePrivateProfileString32 fileName, "tpi", "auxtpifrnt", TextBoxAuxTPIFrnt.Text[/VBA]

    and it would show up as
    [tpi]
    auxtpifrnt=0.086

    in the .ini files,
    The writing process to ini always seems fine. but whats bother me is when adding new input to the .ini files, the .ini form i called for won't be the same in the form.

    Please bare with me

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Don't know if this helps, but I'm working with this just now to do PDF printing. The code is to read in Old settings, write the new settings and then restore the original. I've trimmed out the excess code relating to my project to leave the basic functionality
    [vba]
    Option Explicit
    'Read INI settings
    Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Long, _
    ByVal lpFileName As String) As Long
    'Write settings
    Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, ByVal lpString As Any, _
    ByVal lpFileName As String) As Long
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Sub pdfwrite()
    Dim syncfile As String, maxwaittime As Long
    Dim iniFileName As String, tmpPrinter As Printer
    Dim outputfile As String, x As Long
    Dim tmpoutputfile As String, tmpAutoLaunch As String

    ' set the location of the PDF995.ini
    iniFileName = "c:\pdf995\res\pdf995.ini"
    ' save current settings from the PDF995.ini file
    tmpoutputfile = ReadINIfile("PARAMETERS", "Output File", iniFileName)
    tmpAutoLaunch = ReadINIfile("PARAMETERS", "Autolaunch", iniFileName)

    ' setup new values in PDF995.ini
    x = WritePrivateProfileString("PARAMETERS", "Output File", outputfile, iniFileName)
    x = WritePrivateProfileString("PARAMETERS", "AutoLaunch", "0", iniFileName)

    ' change the default printer to PDF995
    Set tmpPrinter = Application.Printer
    Application.Printer = Application.Printers("PDF995")
    'print the report
    ActiveSheet.PrintOut

    ' restore the original default printer and the PDF995.ini settings
    x = WritePrivateProfileString("PARAMETERS", "Output File", tmpoutputfile, iniFileName)
    x = WritePrivateProfileString("PARAMETERS", "AutoLaunch", tmpAutoLaunch, iniFileName)
    x = WritePrivateProfileString("PARAMETERS", "Launch", "", iniFileName)

    On Error Resume Next
    Application.Printer = tmpPrinter
    End Sub

    Function ReadINIfile(sSection As String, sEntry As String, sFilename As String) As String
    Dim x As Long
    Dim sDefault As String
    Dim sRetBuf As String, iLenBuf As Integer
    Dim sValue As String
    'Six arguments
    'Explanation of arguments:
    'sSection: ini file section (always between brackets)
    'sEntry : word on left side of "=" sign
    'sDefault$: value returned if function is unsuccessful
    'sRetBuf$ : the value you're looking for will be copied to this buffer string
    'iLenBuf% : Length in characters of the buffer string
    'sFileName: Path to the ini file
    sDefault$ = ""
    sRetBuf$ = String$(256, 0) '256 null characters
    iLenBuf% = Len(sRetBuf$)
    x = GetPrivateProfileString(sSection, sEntry, _
    sDefault$, sRetBuf$, iLenBuf%, sFilename)
    ReadINIfile = Left$(sRetBuf$, x)
    End Function


    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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