We help IT Professionals succeed at work.
Get Started

how to rebuild an Ini file using WritePrivateProfileString

Last Modified: 2012-09-02
Please note, although this deals with PDF995, the question is generic and could apply to any application that needs to restore an INI file to its original values.

I have a PDF995 ini file that starts out with 4 lines looking like this.
    Use GPL Ghostcript=1

When my macro is done I want to the inifile to look exactly the same.  (My macro adds many parameter, but they are all temporary in nature.)

The code at the end of this post seems to work in windows 7, but I get weird results in windows xp: when the application is done running, an undesired Explorer window pops up displaying a PDF995 subfolder.

I noticed that lines 16 to 20 are not using kernel32 API whereas the rest of the macro does use an API. I think this inconsistency led to the weird behavior.

My theory led to a successful workaround. I replaced lines 16 to 20 with the following.  
    desired = ""
    desired = desired & "AutoLaunch=,"
    desired = desired & "Autostamp=,"
    desired = desired & "Diagonal Stamp=,"
    desired = desired & "Output File=,"
    desired = desired & "Stamp Text=,"
    desired = desired & "Stamp Text=,"
    desired = desired & "Stamp Text=,"
    desired = desired & "X Position Stamp=,"
    desired = desired & "Y Position Stamp=,"
    desired = desired & "User File=,"
    desired = desired & "Launch=,"
    desired = desired & "Document Name=,"

Open in new window

But, I do not like that workaround because it does not return the ini file to its exact initial setting.  Also, any time a higher level subroutine starts to use a different parameter, I have to remember to add the parameter to the workaround.

Here is the original code, before the workaround.

Sub ChangeIni(ByVal desired As String, iniFilename As String, Optional delim As String = ",")

' windows xp first and windows 7 second
If InStr(1, Environ("Appdata"), "Roaming", 1) = 0 Then
    iniFilename = "c:\pdf995\res\pdf995.ini"
    Pdf995syncFilename = "C:\pdf995\res\pdfsync.ini"
   iniFilename = "C:\Program Files (x86)\pdf995\res\pdf995.ini"
   Pdf995syncFilename = "C:\ProgramData\pdf995\pdfsync.ini"
End If

' to restore all values to PDF995 defaults        call changeini("clear",infilename)
Dim intFileNum As Long
If LCase(desired) = "clear" Then
    intFileNum = FreeFile()
    Open iniFilename For Output As intFileNum
    Print #intFileNum, "[Parameters]"
    Close intFileNum
    desired = desired & "Install=1,"     ' from Ini file after fresh install
    desired = desired & "Quiet=0,"     ' from Ini file after fresh install
    desired = desired & "Use GPL Ghostcript=1,"     ' from Ini file after fresh install
End If
If Len(delim) > 1 Then Stop
If Right(desired, 1) = "," Then
    desired = Left(desired, Len(desired) - 1)
End If

Dim ary
Dim I As Long
Dim ub As Long
    Dim paramFull As String
    Dim paramName As String
    Dim paramDesiredValue As String

    Dim ix As Long
ary = Split(desired, delim)

ub = UBound(ary)
If Len(ary(ub)) = 0 Then ub = ub - 1
For I = 0 To ub

    paramFull = ary(I)
    ix = InStr(paramFull, "=")
    paramName = Left(paramFull, ix - 1)
    paramDesiredValue = Mid(paramFull, ix + 1)
    Dim x
    If InStr(paramDesiredValue, ",") Then Stop
    x = WritePrivateProfileString("PARAMETERS", paramName, paramDesiredValue, iniFilename)
    ' x = WritePrivateProfileString("PARAMETERS", "AutoLaunch", "0", inifilename)
Next I
End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 3 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE