PDA

View Full Version : Ini. Files problem



wkon
03-27-2008, 08:25 AM
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 :bug:

Bob Phillips
03-27-2008, 09:07 AM
A bit more info, some code, the workbook?

wkon
03-27-2008, 09:41 AM
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
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.

lucas
03-27-2008, 10:37 AM
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.

wkon
03-27-2008, 10:46 AM
Sorry, I am a new lounger
I call the function in a form to save the .ini files.
for example,
Data.WritePrivateProfileString32 fileName, "tpi", "auxtpifrnt", TextBoxAuxTPIFrnt.Text

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

mdmackillop
03-27-2008, 11:20 AM
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

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