几个实用的模块化代码

=========================================================
‘判断文件是否存在
Function FileExist(FileName As String) As Boolean
On Error Resume Next
Dim FileNum As Integer
FileNum = FreeFile()
Open FileName For Input As #FileNum
If Err = 0 Then
FileExist = True
Else
FileExist = False
End If
Close #FileNum
End Function

‘获取程序本身所在的目录(返回的字符以“\”结尾)
Function Path() As String
If Len(App.Path) <= 3 Then
Path = App.Path
Else
Path = App.Path & "\"
End If
End Function

‘获取系统目录(SYSTEM)路径
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Function SysPath() As String
SysPath = String(145, Chr(0))
SysPath = Left(SysPath, GetSystemDirectory(SysPath, 145)) & "\"
End Function

‘获取Window路径(WINDOWS系统目录)
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Function WinPath() As String
WinPath = String(145, Chr(0))
WinPath = Left(WinPath, GetWindowsDirectory(WinPath, 145)) & "\"
End Function

‘字符串替换(使用方法:输出=ModifyString(欲处理的字符串,原字符,替换字符),例如 strOut=ModifyString(strSource,"hello","你好"),表示把strSource变量里的“hello”替换为“你好”)
Public Function ModifyString(strModString As String, strSrc As String, sgnModify As Variant)
On Error Resume Next
If strSrc <> sgnModify Then
While InStr(strModString, strSrc) <> 0
strModString = Left(strModString, InStr(strModString, strSrc) - 1) & sgnModify & Mid(strModString, InStr(strModString, strSrc) + Len(strSrc))
Wend
End If
ModifyString = strModString
End Function
=========================================================

相关日志

Leave a Reply