목차 열기

티스토리 뷰

VBA

VBA / ini 파일 읽기 쓰기

RuungJi · 2020. 2. 16. 21:54
728x90
반응형


선언
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" ( _
    ByVal lpApplicationName As StringByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As String _
    ) As Long
Private Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpString As Any, _
    ByVal lpFileName As String _
    ) As Long
cs

(64Bit 환경이어서 PtrSafe가 추가 되었음.)


 

예제 함수

읽기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Public Function readIniFile(ByVal strSec As String, _
ByVal strKey As StringAs String
    '20200216
    'ini파일 [Section]에서 Key의 값을 읽어 반환한다.
    
    Dim buffer As String * 128 '데이터가 들어갈 버퍼
    Dim int_Max_buffer As Integer '버퍼의 크기
    Dim strFilePath As String 'ini 파일의 경로
    Dim strReturn As String '반환 값
    Dim strReturnGPPS As Long 'GPPS 변수
    
    strReturn = "#SecOrKey"
    If strSec <> "" And strKey <> "" Then
        
        buffer = space(128)
        int_Max_buffer = Len(buffer)
        strFilePath = "ini 파일의 경로'ini 파일의 경로
        strReturnGPPS = GetPrivateProfileString(strSec, strKey, "", _
buffer, int_Max_buffer, strFilePath) 'ini Core
        If strReturnGPPS Then
            strReturn = Left$(buffer, strReturnGPPS) '찾은 값 저장
        End If
    End If
    readIniFile = strReturn '값 반환
End Function
cs
 

쓰기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Public Function writeIniFile(ByVal strSec As String, _
ByVal strKey As StringByVal strCon As StringAs String
    '20200216
    'ini파일 [Section]에서 Key의 값에 Content를 쓰고 성공여부를 Boolean으로 반환한다.
    
    Dim strFilePath As String 'ini 파일의 경로
    Dim strReturn As String '반환 값
    
    strReturn = False
    If strSec <> "" And strKey <> "" Then
        strFilePath = "ini 파일의 경로" 'ini 파일의 경로
        strReturnGPPS = WritePrivateProfileString(strSec, strKey, _
strCon, strFilePath) 'ini Core
        strReturn = True
    End If
    writeIniFile = strReturn '값 반환
End Function
cs
 

연관검색어
더보기

vbs vba VBA 비주얼베이직 엑셀 코딩 excel visual basic for application

 

728x90
반응형
댓글
글 보관함
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Total
Today
Yesterday