Hi,

Those familiar with VBA might know the PrivateProfileString function, that can read/write the Windows registry as well as an ini-file. The latter had my interest and I searched for an existing replacement; makes conversions easier.
But I didn't find any replacement (maybe didn't seach good enough).

Anyway, I've made two functions myself, which could be placed in the codesnippets.
Is someone skilled in doing that swiftly?
Then pls feel free to.
If not, I'll do it somewhat later on ;-)

Cheers,
Cor


'=========================================================================
' Nou&Off     |    31-05-07    |    ©  www.nouenoff.nl
' Description : Replacement for READING with MsWin System.PrivateProfileString
' Arguments   :  Strings for File, Section and Item
' Returns     :  String with value found
'-------------------------------------------------------------------------
Function OOoReadPrivateProfileString (sFile$, sSec$, sItem$) as String
        Dim n%
        Dim s$, s1$
        Dim bRightSection as Boolean
        Dim bSecEnd as Boolean
        
        On Error GoTo ErrorHandler
        If Dir(sFile) = "" Then       ' not found
                OOoReadPrivateProfileString = ""
                Exit Function
        End If
        
        sSec= "["& sSec & "]"
        n = FreeFile()
        Open sFile for Input As #n
        Do while NOT EOF(n)
                Input #n, s
                If NOT bRightSection Then
                        If Left (s, len(sSec)) = sSec Then
                                bRightSection = True
                        End If
                Else
                        If Left (s, len(sItem)) = sItem Then
                                s1 = Right (s, (len(s) - len(sItem)-1))
                                Exit Do
                        ElseIf Left (s, 1) = "[" Then
                                ' sItem not found in the right section
                                ' do not search in other sections
                                Exit Do
                        End If
                End If
        Loop
        Close #n
        OOoReadPrivateProfileString = s1
        
        Exit Function
ErrorHandler:   
        MyErrMessenger ("OOoReadPrivateProfileString")
End Function


'=========================================================================
'   Nou&Off     |    31-05-07    |    ©  www.nouenoff.nl
' Description : Replacement for WRITING with MsWin System.PrivateProfileString
' Arguments   :  Strings for File, Section, Item and Value
' Returns     :  0 if OK; -1 on failure
'-------------------------------------------------------------------------
Function OOoWritePrivateProfileString (sFile$, sSec$, sItem$, sText$)
        Dim n1%, n2%
        Dim s$, s1$, s2$, sFileBak
        Dim bRightSection as Boolean
        Dim bItemFound as Boolean
                
        On Error GoTo ErrorHandler
        If Dir(sFile) = "" Then       ' not found
                OOoWritePrivateProfileString = -1
                Exit Function
        End If
        
        sFileBak = sFile & "Bak"
        
        FileCopy (sFile, sFileBak)
        
        sSec= "["& sSec & "]"
        n1 = FreeFile()
        Open sFile for Input As #n1
        n2 = FreeFile()
        Open sFileBak for Output As #n2
        
        Do while NOT EOF(n1)
                ' read the line
                Input #n1, s
                
                ' analyse the line, and if necessary change it
                If NOT bRightSection Then
                        If Left (s, len(sSec)) = sSec Then
                                ' section found, go searching item
                                bRightSection = True
                        End If
                Else
                        If NOT bItemFound Then
                                If Left (s, len(sItem)) = sItem Then
                                        s1 = Left (s, (len(sItem)+1)) & sText
                                        s = s1
                                        ' item changed - may leave this loop
                                        bItemFound = True
                                End If
                        End If
                End If
                ' write the line
                If Left (s, 1) = "[" Then
                        ' print extra blank line first
                        ' those are not read from the orig. file
                        print #n2, Chr(13)
                        print #n2, s
                Else
                        print #n2, s
                End If
                
        Loop
        Close #n1
        Close #n2
        
        FileCopy (sFileBak, sFile)
        'Kill (sFileBak)  '  you might want to kill the bak-file
        
'       All OK
        OOoWritePrivateProfileString = 0
        
        Exit Function
ErrorHandler:   
        MyErrMessenger ("OOoWritePrivateProfileString ")
        OOoWritePrivateProfileString = -1
End Function



--

Cor Nouws
Arnhem - Netherlands
nl.OpenOffice.org - marketing contact

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to