Excel VBA INI Datei schreiben / lesen

  • Excel

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von Morrison.

    Excel VBA INI Datei schreiben / lesen

    Hallo zusammen,

    ich habe hier ein VBA Script mit dem ich in eine INI Datei schreiben möchte und daten auch wieder auslesen,
    leider hat sich hier ein Fehler eingeschlichen, nur kann ich ihn nicht finden.
    ob da vlt mal jemand drüber schauen kann und mir weiter helfen könnte.

    Visual Basic-Quellcode

    1. Option Explicit
    2. Public Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" _
    3. Alias "WritePrivateProfileStringA" ( _
    4. ByVal lpApplicationName As String, _
    5. ByVal lpKeyName As Any, _
    6. ByVal lpString As Any, _
    7. ByVal lpFileName As String) As Long
    8. Public Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" _
    9. Alias "GetPrivateProfileStringA" ( _
    10. ByVal lpApplicationName As String, _
    11. ByVal lpKeyName As Any, _
    12. ByVal lpDefault As String, _
    13. ByVal lpReturnedString As String, _
    14. ByVal nSize As Long, _
    15. ByVal lpFileName As String) As Long Dim myinifile As String
    16. Dim PrintIP As String
    17. Dim CValue, RValue, IPValue, PrinterValue As String
    18. Dim lResult As Long
    19. Public ColINI, RowINI, LocRang, ColBox, RowBox As String
    20. Public Printer, IP_INI As String
    21. Public Sub ColBoxCh_onChange(control As IRibbonControl, Text As String)
    22. ColBox = Text
    23. Debug.Print Text
    24. Call writeColBox
    25. End Sub
    26. Public Sub writeColBox()
    27. myinifile = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\AddIns\config_Z-Tools.ini"
    28. WritePrivateProfileString "PWegLP", "ColBox", ColBox, myinifile
    29. End Sub
    30. Public Sub lesen()
    31. myinifile = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\AddIns\config_Z-Tools.ini"
    32. ColINI = ""
    33. RowINI = ""
    34. PrintIP = ""
    35. Printer = ""
    36. CValue = Space$(4)
    37. RValue = Space$(4)
    38. IPValue = Space$(16)
    39. PrinterValue = Space$(1)
    40. lResult = GetPrivateProfileString("PWegLP", "ColBox", ColINI, CValue, Len(CValue), myinifile)
    41. ColINI = Left$(CValue, lResult)
    42. lResult = GetPrivateProfileString("PWegLP", "RowBox", RowINI, RValue, Len(RValue), myinifile)
    43. RowINI = Left$(RValue, lResult)
    44. LocRang = ColINI + RowINI
    45. lResult = GetPrivateProfileString("DruckerIP_Label", "IP", PrintIP, IPValue, Len(IPValue), myinifile)
    46. IP_INI = Left$(IPValue, lResult)
    47. lResult = GetPrivateProfileString("DruckerIP_Label", "Printer", Printer, PrinterValue, Len(PrinterValue), myinifile)
    48. Printer = Left$(PrinterValue, lResult)
    49. End Sub
    Gruß Matze 8-)

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „matze.tele“ ()

    Was meinst Du mit

    matze.tele schrieb:

    leider hat sich hier ein Fehler eingeschlichen
    (Klingt wie bei einem Bilderrätsel: »Kannst Du alle zehn Fehler finden?«) Ist das nicht Dein eigener Code? Dann bitte Quelle verlinken. Wie äußert sich konkret der Fehler?
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Warum schreibst Du das Ganze nicht einfach in eine Textdatei?

    Visual Basic-Quellcode

    1. Sub Lesen()
    2. Dim fs As Object
    3. Dim NFile
    4. Set fs = CreateObject("Scripting.FileSystemObject")
    5. On Error GoTo Fehler
    6. Set NFile = fs.OpenTextFile(DeineDatei)
    7. Dim strZeile() As String
    8. Dim strTest As String
    9. Do Until NFile.AtEndOfStream
    10. strZeile = VBA.Split(NFile.ReadLine, ": ")
    11. Select Case strZeile(0)
    12. Case "VariableA"
    13. VariableA = strZeile(1)
    14. Case "VariableB"
    15. VariableB= strZeile(1)
    16. End Select
    17. Loop
    18. NFile.Close
    19. Exit Sub
    20. Fehler:
    21. VBA.MsgBox Err.Number & " " & Err.Description & vbNewLine
    22. End Sub
    23. Sub Schreiben()
    24. Dim fs As Object
    25. Dim NFile
    26. Set fs = CreateObject("Scripting.FileSystemObject")
    27. On Error Resume Next
    28. fs.DeleteFile (DeineDatei)
    29. Set NFile = fs.CreateTextFile(DeineDatei)
    30. NFile.WriteLine ("VariableA: " & VariableA)
    31. NFile.WriteLine ("VariableB: " & VariableB)
    32. NFile.Close
    33. End Sub



    Es muss noch ein Verweis auf die Scripting Runtime gesetzt werden.
    NB. Es ist doch schön, wenn man lesbare Namen vergibt. Siehe auch [VB.NET] Beispiele für guten und schlechten Code (Stil).

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „INOPIAE“ ()

    Hallo,
    Danke für die schnelle Reaktion.

    @petaod ja ist ein Copyfehler

    @INOPIAE danke für den Tip und die Vorbereitung für mehr :huh:, aber es ging mir da nicht um's speichern der Daten sondern um den Fehler den ich vergaß zu erwähnen

    @VaporiZed Ja entschuldige hab ich vergessen zu erwähnen, in Zeile 32 wird " ColBox " markiert, "Typen unverträglich" ist der Fehler
    Gruß Matze 8-)
    Naja, auch wenn der entsprechende Funktions-Parameter als Any eingestuft ist, glaube ich kaum, dass Du da einfach mal ein Control übergeben kannst. Spekulatius: Probier mal ColBox.Text.
    Was ist ColBox eigentlich vom konkreten Datentyp her? Scheint ja ein Control zu sein. Aber was für eines?
    btw: Allein dafür hasse ich VBA: ColBox = Text
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Hi @VaporiZed,

    ich habe ein customUI ribbon mit editboxen, ColBox soll den Manuell eingegeben Spaltennamen enthalten, den will ich dann in einer INI speichern und auch wieder abfragen können.
    Das Gleiche mache ich auch mit der Zeile, nur habe ich hier diesen Fehler.
    Die Eingabe einer IP, bei Nachdruck, macht keine Probleme.
    Bilder
    • Koordinate.png

      672 Byte, 74×64, 190.941 mal angesehen
    • Nachdruck.png

      1,88 kB, 225×100, 219.687 mal angesehen
    Gruß Matze 8-)
    Der Verwendungszweck ist hier sekundär.
    Es sind also EditBoxen. Meinst Du TextBox? Wieauchimmer. Ich glaub immer noch nicht, dass Du ein Control an die WritePrivateProfileString-Methode übergeben kannst. Dafür sind diese P/Invoke-Methoden viel zu empfindlich. Falls Du Zugriff auf den ColBox-Inhalt über den ControlNamen selbst hast, mach es notfalls über einen Zwischenstring:

    Visual Basic-Quellcode

    1. Public Sub writeColBox()
    2. myinifile = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\AddIns\config_Z-Tools.ini"
    3. Dim Foo As String
    4. Foo = ColBox
    5. WritePrivateProfileString "PWegLP", "ColBox", Foo, myinifile
    6. End Sub
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    @matze.tele
    Deine API Deklaration ist falsch. Alle Parameter müssten also für WritePrivateProfileStringA = "As String" sein und kein Any.

    C-Quellcode

    1. BOOL WritePrivateProfileStringA(
    2. [in] LPCSTR lpAppName,
    3. [in] LPCSTR lpKeyName,
    4. [in] LPCSTR lpString,
    5. [in] LPCSTR lpFileName
    6. );

    Entsprechend sollten daher die zu übergebenden Variablen an WritePrivateProfileStringA auch "As String" sein.
    Mfg -Franky-
    Hallo,

    sie müssen nicht als String deklariert werden, es kann auch ANY sein.

    Ein Beispiel (hier 64 Bit Win/Excel),so wie ich das gern mache...

    VB.NET-Quellcode

    1. ​Private Declare PtrSafe Function GetPrivateProfileStringA Lib "kernel32" ( _
    2. ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    3. ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    4. Private Declare PtrSafe Function WritePrivateProfileStringA Lib "kernel32" ( _
    5. ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
    6. ByVal lpFileName As String) As Long
    7. Private Const csTxtdatei As String = "Testtextdaten.txt"
    8. Function SetGetInidaten(sBereich As String, sItem As String, Optional sDaten As String) As String
    9. 'Schreibt Daten in die Textdatei oder liest Daten aus der Textdatei
    10. Dim sPfad As String, sTxt As String * 5000, l As Integer
    11. sPfad = Environ$("TEMP") & "\MyIni.txt"
    12. If sDaten <> "" Then
    13. WritePrivateProfileStringA sBereich, sItem, sDaten, sPfad
    14. Else
    15. l = GetPrivateProfileStringA(sBereich, sItem, "none", sTxt, 5000, sPfad)
    16. SetGetInidaten = Left$(sTxt, l)
    17. End If
    18. End Function
    19. Sub Schreibewas1()
    20. SetGetInidaten "Sektor1", "MeinWert", "Hallo World!"
    21. End Sub
    22. Sub Lesewas1()
    23. MsgBox SetGetInidaten("Sektor1", "MeinWert")
    24. End Sub

    volti schrieb:

    sie müssen nicht als String deklariert werden, es kann auch ANY sein.
    stimmt, müssen nicht, aber da man nur Strings (LPCSTR) übergeben kann, macht es keinen Sinn die Parameter als Any auszulegen.
    Mfg -Franky-
    Hallo @ all,

    eure Tips haben mir weiter geholfen, hatte es nicht richtig deklariert.

    nun habe ich ein weiteres Problem:
    beim auslesen bekomme ich keine Daten, in der INI sind alle Daten vorhanden.

    Visual Basic-Quellcode

    1. ​Public Sub lesen()
    2. myinifile = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\AddIns\config_Z-Tools.ini"
    3. ColINI = ""
    4. RowINI = ""
    5. PrintIP = ""
    6. Printer = ""
    7. CValue = Space$(4)
    8. RValue = Space$(4)
    9. IPValue = Space$(16)
    10. PrinterValue = Space$(1)
    11. lResult = GetPrivateProfileString("PWegLP", "ColBox", ColINI, CValue, Len(CValue), myinifile)
    12. ColINI = Left$(CValue, lResult)
    13. lResult = GetPrivateProfileString("PWegLP", "RowBox", RowINI, RValue, Len(RValue), myinifile)
    14. RowINI = Left$(RValue, lResult)
    15. LocRang = ColINI + RowINI
    16. lResult = GetPrivateProfileString("DruckerIP_Label", "IP", PrintIP, IPValue, Len(IPValue), myinifile)
    17. IP_INI = Left$(IPValue, lResult)
    18. lResult = GetPrivateProfileString("DruckerIP_Label", "Printer", Printer, PrinterValue, Len(PrinterValue), myinifile)
    19. Printer = Left$(PrinterValue, lResult)
    20. Debug.Print ColINI
    21. Debug.Print RowINI
    22. Debug.Print PrintIP
    23. Debug.Print Printer
    24. End Sub
    Gruß Matze 8-)
    Hallo Matze,

    hier eine Idee dazu...

    VB.NET-Quellcode

    1. Private Declare PtrSafe Function GetPrivateProfileStringA Lib "kernel32" ( _
    2. ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _
    3. ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    4. Private Declare PtrSafe Function WritePrivateProfileStringA Lib "kernel32" ( _
    5. ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, _
    6. ByVal lpFileName As String) As Long
    7. Public Sub lesen()
    8. Dim myinifile As String
    9. Dim lResult As Long, sText As String * 32
    10. Dim ColIni As String, RowIni As String, PrintIP As String, Printer As String
    11. Dim LocRang As Long
    12. myinifile = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\AddIns\config_Z-Tools.ini"
    13. lResult = GetPrivateProfileStringA("PWegLP", "ColBox", "", sText, 32, myinifile)
    14. ColIni = Left$(sText, lResult)
    15. lResult = GetPrivateProfileStringA("PWegLP", "RowBox", "", sText, 32, myinifile)
    16. RowIni = Left$(sText, lResult)
    17. LocRang = Val(ColIni) + Val(RowIni)
    18. lResult = GetPrivateProfileStringA("DruckerIP_Label", "IP", "", sText, 32, myinifile)
    19. PrintIP = Left$(sText, lResult)
    20. lResult = GetPrivateProfileStringA("DruckerIP_Label", "Printer", "", sText, 32, myinifile)
    21. Printer = Left$(sText, lResult)
    22. Debug.Print ColIni
    23. Debug.Print RowIni
    24. Debug.Print PrintIP
    25. Debug.Print Printer
    26. End Sub



    Nachtrag:Die Vorgabewerte hatte ich rausgenommen, es wird also kein Wert zurückgegeben, wenn kein Eintrag da ist.Kannst Du natürlich bei Bedarf noch ergänzen.Beispiel:

    VB.NET-Quellcode

    1. lResult = GetPrivateProfileStringA("DruckerIP_Label", "Printer", "Canon 1234", sText, 32, myinifile)


    Gruß
    Karl-Heinz

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „volti“ ()

    Hi Matze,

    habe das jetzt nicht weiter verfolgt.

    In deiner o.a. Sub hast Du die Variablen nicht gedimt, sie sind also variant. Wie Eierlein schon anmerkte könnte das der Fehler gewesen sein.

    Ich hatte ja jetzt einfach immer die gleiche Variable sText füllen lassen. Diese muss dann auf den größten zu erwartenden Text oder mehr dimensioniert werden und kann später längengerecht in Deine Zielvariablen überführt werden.

    Gruß KH
    Ich hab hier ein .ini-Modul:

    Spoiler anzeigen

    Visual Basic-Quellcode

    1. Option Explicit
    2. Public bSectionExists As Boolean
    3. Public bKeyExists As Boolean
    4. '---------------------------------------------------------------------------------------
    5. ' Procedure : Ini_ReadKeyVal
    6. ' Author : Daniel Pineault, CARDA Consultants Inc.
    7. ' Website : http://www.cardaconsultants.com
    8. ' Purpose : Read an Ini file's Key
    9. ' Copyright : The following may be altered and reused as you wish so long as the
    10. ' copyright notice is left unchanged (including Author, Website and
    11. ' Copyright). It may not be sold/resold or reposted on other sites (links
    12. ' back to this site are allowed).
    13. ' Req'd Refs: Uses Late Binding, so none required
    14. ' No APIs either! 100% VBA
    15. '
    16. ' Input Variables:
    17. ' ~~~~~~~~~~~~~~~~
    18. ' sIniFile : Full path and filename of the ini file to read
    19. ' sSection : Ini Section to search for the Key to read the Key from
    20. ' sKey : Name of the Key to read the value of
    21. '
    22. ' Usage:
    23. ' ~~~~~~
    24. ' ? Ini_Read(Application.CurrentProject.Path & "\MyIniFile.ini", "LINKED TABLES", "Path")
    25. '
    26. ' Revision History:
    27. ' Rev Date(yyyy/mm/dd) Description
    28. ' **************************************************************************************
    29. ' 1 2012-08-09 Initial Release
    30. '---------------------------------------------------------------------------------------
    31. Function Ini_ReadKeyVal(ByVal sIniFile As String, _
    32. ByVal sSection As String, _
    33. ByVal sKey As String) As String
    34. On Error GoTo Error_Handler
    35. Dim sIniFileContent As String
    36. Dim aIniLines() As String
    37. Dim sLine As String
    38. Dim i As Long
    39. sIniFileContent = ""
    40. bSectionExists = False
    41. bKeyExists = False
    42. 'Validate that the file actually exists
    43. If FileExist(sIniFile) = False Then
    44. MsgBox "The specified ini file: " & vbCrLf & vbCrLf & _
    45. sIniFile & vbCrLf & vbCrLf & _
    46. "could not be found.", vbCritical + vbOKOnly, "File not found"
    47. GoTo Error_Handler_Exit
    48. End If
    49. sIniFileContent = ReadFile(sIniFile) 'Read the file into memory
    50. aIniLines = Split(sIniFileContent, vbCrLf)
    51. For i = 0 To UBound(aIniLines)
    52. sLine = Trim(aIniLines(i))
    53. If bSectionExists = True And Left(sLine, 1) = "[" And Right(sLine, 1) = "]" Then
    54. Exit For 'Start of a new section
    55. End If
    56. If sLine = "[" & sSection & "]" Then
    57. bSectionExists = True
    58. End If
    59. If bSectionExists = True Then
    60. If Len(sLine) > Len(sKey) Then
    61. If Left(sLine, Len(sKey) + 1) = sKey & "=" Then
    62. bKeyExists = True
    63. Ini_ReadKeyVal = Mid(sLine, InStr(sLine, "=") + 1)
    64. End If
    65. End If
    66. End If
    67. Next i
    68. Error_Handler_Exit:
    69. On Error Resume Next
    70. Exit Function
    71. Error_Handler:
    72. 'Err.Number = 75 'File does not exist, Permission issues to write is denied,
    73. MsgBox "Fehler im Modul 'Ini_ReadKeyVal'" & vbCrLf & vbCrLf & _
    74. "Error Number: " & err.number & vbCrLf & _
    75. "Error Source: Ini_ReadKeyVal" & vbCrLf & _
    76. "Error Description: " & err.Description & _
    77. Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
    78. , vbOKOnly + vbCritical, "An Error has Occurred!"
    79. Resume Error_Handler_Exit
    80. End Function
    81. '---------------------------------------------------------------------------------------
    82. ' Procedure : Ini_WriteKeyVal
    83. ' Author : Daniel Pineault, CARDA Consultants Inc.
    84. ' Website : http://www.cardaconsultants.com
    85. ' Purpose : Writes a Key value to the specified Ini file's Section
    86. ' If the file does not exist, it will be created
    87. ' If the Section does not exist, it will be appended to the existing content
    88. ' If the Key does not exist, it will be appended to the existing Section content
    89. ' Copyright : The following may be altered and reused as you wish so long as the
    90. ' copyright notice is left unchanged (including Author, Website and
    91. ' Copyright). It may not be sold/resold or reposted on other sites (links
    92. ' back to this site are allowed).
    93. ' Req'd Refs: Uses Late Binding, so none required
    94. ' No APIs either! 100% VBA
    95. '
    96. ' Input Variables:
    97. ' ~~~~~~~~~~~~~~~~
    98. ' sIniFile : Full path and filename of the ini file to edit
    99. ' sSection : Ini Section to search for the Key to edit
    100. ' sKey : Name of the Key to edit
    101. ' sValue : Value to associate to the Key
    102. '
    103. ' Usage:
    104. ' ~~~~~~
    105. ' Call Ini_WriteKeyVal(Application.CurrentProject.Path & "\MyIniFile.ini", "LINKED TABLES", "Paths", "D:\")
    106. '
    107. ' Revision History:
    108. ' Rev Date(yyyy/mm/dd) Description
    109. ' **************************************************************************************
    110. ' 1 2012-08-09 Initial Release
    111. ' 2 2020-01-27 Fix to address issue flagged by users
    112. '---------------------------------------------------------------------------------------
    113. Function Ini_WriteKeyVal(ByVal sIniFile As String, _
    114. ByVal sSection As String, _
    115. ByVal sKey As String, _
    116. ByVal sValue As String) As Boolean
    117. On Error GoTo Error_Handler
    118. Dim sIniFileContent As String
    119. Dim aIniLines() As String
    120. Dim sLine As String
    121. Dim sNewLine As String
    122. Dim i As Long
    123. Dim bFileExist As Boolean
    124. Dim bInSection As Boolean
    125. Dim bKeyAdded As Boolean
    126. sIniFileContent = ""
    127. bSectionExists = False
    128. bKeyExists = False
    129. 'Validate that the file actually exists
    130. If FileExist(sIniFile) = False Then
    131. GoTo SectionDoesNotExist
    132. End If
    133. bFileExist = True
    134. sIniFileContent = ReadFile(sIniFile) 'Read the file into memory
    135. aIniLines = Split(sIniFileContent, vbCrLf) 'Break the content into individual lines
    136. sIniFileContent = "" 'Reset it
    137. For i = 0 To UBound(aIniLines) 'Loop through each line
    138. sNewLine = ""
    139. sLine = Trim(aIniLines(i))
    140. If sLine = "[" & sSection & "]" Then
    141. bSectionExists = True
    142. bInSection = True
    143. End If
    144. If bInSection = True Then
    145. If sLine <> "[" & sSection & "]" Then
    146. If Left(sLine, 1) = "[" And Right(sLine, 1) = "]" Then
    147. 'Our section exists, but the key wasn't found, so append it
    148. sNewLine = sKey & "=" & sValue
    149. i = i - 1
    150. bInSection = False ' we're switching section
    151. bKeyAdded = True
    152. End If
    153. End If
    154. If Len(sLine) > Len(sKey) Then
    155. If Left(sLine, Len(sKey) + 1) = sKey & "=" Then
    156. sNewLine = sKey & "=" & sValue
    157. bKeyExists = True
    158. bKeyAdded = True
    159. End If
    160. End If
    161. End If
    162. If Len(sIniFileContent) > 0 Then sIniFileContent = sIniFileContent & vbCrLf
    163. If sNewLine = "" Then
    164. sIniFileContent = sIniFileContent & sLine
    165. Else
    166. sIniFileContent = sIniFileContent & sNewLine
    167. End If
    168. Next i
    169. SectionDoesNotExist:
    170. 'if not found, add it to the end
    171. If bSectionExists = False Then
    172. If Len(sIniFileContent) > 0 Then sIniFileContent = sIniFileContent & vbCrLf
    173. sIniFileContent = sIniFileContent & "[" & sSection & "]"
    174. End If
    175. If bKeyAdded = False Then
    176. sIniFileContent = sIniFileContent & vbCrLf & sKey & "=" & sValue
    177. End If
    178. 'Write to the ini file the new content
    179. Call OverwriteTxt(sIniFile, sIniFileContent)
    180. Ini_WriteKeyVal = True
    181. Error_Handler_Exit:
    182. On Error Resume Next
    183. Exit Function
    184. Error_Handler:
    185. MsgBox "Fehler im Modul 'Ini_WriteKeyVal'" & vbCrLf & vbCrLf & _
    186. "Error Number: " & err.number & vbCrLf & _
    187. "Error Source: Ini_WriteKeyVal" & vbCrLf & _
    188. "Error Description: " & err.Description & _
    189. Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
    190. , vbOKOnly + vbCritical, "An Error has Occurred!"
    191. Resume Error_Handler_Exit
    192. End Function
    193. Sub TestReadKey()
    194. MsgBox "INI File: " & ThisWorkbook.Path & "\Tierhotel.ini" & vbCrLf & _
    195. "Section: SETTINGS" & vbCrLf & _
    196. "Section Exist: " & bSectionExists & vbCrLf & _
    197. "Key: License" & vbCrLf & _
    198. "Key Exist: " & bKeyExists & vbCrLf & _
    199. "Key Value: " & Ini_ReadKeyVal(ThisWorkbook.Path & "\Tierhotel.ini", "SETTINGS", "License")
    200. 'You can validate the value by checking the bSectionExists and bKeyExists variable to ensure they were actually found in the ini file
    201. End Sub
    202. Sub TestWriteKey()
    203. If Ini_WriteKeyVal(ThisWorkbook.Path & "\Tierhotel.ini", "SETTINGS", "License", "JBXR-HHTY-LKIP-HJNB-GGGT") = True Then
    204. MsgBox "The key was written"
    205. Else
    206. MsgBox "An error occurred!"
    207. End If
    208. End Sub