7,763
社区成员
发帖
与我相关
我的任务
分享
'以下代码在窗体中
'先在C盘建立一个Xls文件
'文件名为Data.Xls 即完整文件名为"C:\Data.Xls"
'在A1单元格输入一个数值
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim X%
Private Sub Cmd1_Click()
X = X + 1
If IsNumeric(X) Then
Txt1.Text = X
Else
MsgBox "Excel文件中的数据不是数值!"
End If
End Sub
Private Sub Form_Load()
X = GetData
Txt1.Text = X
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetData
End Sub
Private Function GetData() As String
If Not IIf(Dir(FileName) <> "", True, False) Then
MsgBox "未找到文件 C:\Data.Xls,请先建立文件"
Exit Function
End If
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.open("C:\Data.Xls")
Set xlSheet = xlBook.Worksheets(1) '设置活动工作表
GetData = xlSheet.Range("$A$1")
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Function
Private Function SetData()
If Not IIf(Dir(FileName) <> "", True, False) Then
MsgBox "未找到文件 C:\Data.Xls,请先建立文件"
Exit Function
End If
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.open("C:\Data.Xls")
Set xlSheet = xlBook.Worksheets(1) '设置活动工作表
xlSheet.Range("$A$1") = X
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Function
'以下代码在模块中
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 pFileName 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
'*************************************
'目的:写入数据至Ini文件
'输入: FileName 文件名
' AppName 项目名
' In_Key 键名
' In_Data 键名上的数值
'返回: 写入成功 True
' 写入失败 False
'*************************************
Public Function WriteIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, FileName
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
'*************************************
'目的:从Ini文件中读取数据
'输入: FileName 文件名
' AppName 项目名
' In_Key 键名
'返回: 取得给定键名上的数据
'*************************************
Public Function GetIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
'以下代码在窗体中
Dim X%
Private Sub Cmd1_Click()
X = X + 1
Txt1.Text = X
End Sub
Private Sub Form_Load()
X = GetIniStr(App.Path & "\Data.ini", "Data", "Index")
Txt1.Text = X
End Sub
Private Sub Form_Unload(Cancel As Integer)
WriteIniStr App.Path & "\Data.ini", "Data", "Index", X
End Sub