vba如何读取ini文件信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vba如何读取ini文件信息相关的知识,希望对你有一定的参考价值。

功能实现函数如下

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

\'********************************************************************
\'* Sub : ReadFromIni and WriteIntoIni
\'*
\'* Purpose:  读写INI文件
\'*
\'********************************************************************
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 WriteIntoIni(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.WriteIntoIni", "Failed to write")
End If

End Sub

 

函数使用测试如下

Sub Main()

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 WriteIntoIni(strIniFile, strSection, strKey, strValue)

strValue = ReadFromIni(strIniFile, strSection, strKey, "")
MsgBox "Version = " & strValue, vbInformation

End Sub

 

运行上面的函数,程序会在当前目录生成一个example.ini文件,并向里面写入如下信息

[Application]
Version=1.0.30
然后再读取显示出来,如下

参考技术A VBA操作ini文件
1、点显示可以显示ini里对应的内容
2、点修改可以修改ini里对应的内容
3、如果没有ini文件可以点创建创建一个ini文件,要带上这个窗口里的内容
4、最好能有密码加密,也就是说在ini里不能显示真实的密码
参考技术B 可以
用open,eof,bof,get,put,close配合就可以。
参考技术C INI文件读写VBA代码

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 WriteIntoIni(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.WriteIntoIni", "Failed to write")
End If

End Sub

以下代码简单演示如何使用上述ReadFromIni、WriteIntoIni两个函数

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.WriteIntoIni(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本回答被提问者和网友采纳

通过python读取ini配置文件

ini是啥


你可以理解为就是一个配置文件的统称吧。比如test.conf,这样的你可以理解为他就是ini文件,里面一般存放一些配置信息。比如数据库的基本信息,一会我们进行讲解!

那么ta的好处是啥呢?就是把一些配置信息提出去来进行单独管理,如果以后有变动只需改配置文件,无需修改代码。

ini中的基本格式


[名称,根据实际情况写就行,没啥讲究]

key1=value1

key2=value2

python中通过ConfigParser模块来进行读取操作

实战


演示场景:

1、创建一个数据库配置文件,名字为db.conf,内容如下:

[DATABASE]

host = 127.0.0.1

port = 3306

user = root

passwd = vertrigo

db = testdb

charset = utf8

2、在python中读取信息并连接数据库,代码如下:

import configparser

import mysql.connector

class GetDB:

def __init__(self, db_config):

config = configparser.ConfigParser()

config.read(db_config)

#把配置文件里的数据读取出来并保存

self.host = config[‘DATABASE‘][‘host‘]

self.port = config[‘DATABASE‘][‘port‘]

self.user = config[‘DATABASE‘][‘user‘]

self.passwd = config[‘DATABASE‘][‘passwd‘]

self.db = config[‘DATABASE‘][‘db‘]

self.charset = config[‘DATABASE‘][‘charset‘]

#这里就是链接数据库了

def get_conn(self):

try:

conn = mysql.connector.connect(host=self.host, port=self.port, user=self.user, password=self.passwd, database=self.db, charset=self.charset)

return conn

except Exception as e:

print(‘%s‘, e)

sys.exit()


本文出自 “小强性能自动化测试品牌” 博客,请务必保留此出处http://xqtesting.blog.51cto.com/4626073/1974086

以上是关于vba如何读取ini文件信息的主要内容,如果未能解决你的问题,请参考以下文章

excel用vba自动提取某个ini文件中的数字到制定单元格

vba修改INI中指定的项

读取和写入 .ini 文件的推荐方法

delphi treeview控件读取INI文件。

中兴机顶盒现场配置工具读取配置信息获取所有ini配置文件失败

vba.ini是啥文件?