INI File Read and Write VBA code

Source: Internet
Author: User

Option explicit

Private 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

Private 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

Public Function readfromini (byval INIFILE as string, byval section as string, byval key as string, byval defaultvalue as string) as string

Dim strrtn as string
Strrtn = space (256)

Dim lngrtn as long
Lngrtn = getprivateprofilestring (section, key, defaultvalue, strrtn, 255, INIFILE)

If lngrtn> 0 then
Strrtn = trim (strrtn)
Readfromini = mid (strrtn, 1, Len (strrtn)-1)
Else
Readfromini = defaultvalue
End if

End Function

Public sub writepolicini (byval INIFILE as string, byval section as string, byval key as string, byval value as string)

Dim lngrtn as long
Lngrtn = writeprivateprofilestring (section, key, value, INIFILE)

If lngrtn> 0 then
Else
Call err. Raise (-1, "inifileutil. writeiniini", "failed to write ")
End if

End sub

The following code demonstrates how to use the preceding functions readfromini and writejavasini.

Option explicit

Public sub main ()

On Error goto err_handling

Dim strinifile as string
Strinifile = activeworkbook. Path & "\ example. ini"

Dim strsection as string
Strsection = "application"

Dim strkey as string
Strkey = "version"

Dim strvalue as string
Strvalue = "1.0.30"

Call iniutil. writepolicini (strinifile, strsection, strkey, strvalue)

Strvalue = iniutil. readfromini (strinifile, strsection, strkey ,"")
Call msgbox ("version =" & strvalue, vbinformation + vbokonly, ET. activeworkbook. Name)
Exit_door:
Exit sub

Err_handling:
Call msgbox (ERR. Number & "-" & err. Description, vbexclamation + vbokonly, ET. activeworkbook. Name)
Resume exit_door

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.