跟我学VBA,我这里专注VBA, 授人以渔。我98年开始,从源码接触VBA已经20余年了,随着年龄的增长,越来越觉得有必要把这项技能传递给需要这项技术的职场人员。希望职场和数据打交道的朋友,都来学习VBA,利用VBA,起码可以提高自己的工作效率,可以有时间多陪陪父母,多陪陪家人,何乐而不为呢?我的教程一共九套,从入门开始一直讲到程序的分发,是学习利用VBA的实用教程。这份API资料是随高级教程赠送的.
这讲我们继续学习VBA 64位API声明语句第014讲,这些内容是MS的权威资料,看似枯燥,但对于想学习API函数的朋友是非常有用的。
【分享成果,随喜正能量】282升米恩,斗米仇。不要成为一个没有原则的滥好人,否则你帮助过的人不会感谢你,你没帮助到的人会怨恨你。我想起了有一个笑话,某个乞丐在街上祈祷,A路过后天天给了他10块钱,B看乞丐好欺负,天天打他一顿。某天A突然忘带零钱了,乞丐问他说你今天怎么不给我钱!某天B忘记打了乞丐,乞丐却感激涕零觉得B内心其实是一个好人。虽然是个笑话,不过对我们理解道理有所帮助。
当学员学习到高级阶段,如果引用API,这个资料可以直接查到64位写法。大多数情况下我们是将低版本的程序文件升级到高版本,这时您就不必为如下的错误提示所困扰了:
Declare PtrSafe Function GetThreadContext Lib "kernel32" Alias "GetThreadContext" (ByVal hThread As LongPtr, lpContext As CONTEXT) As Long
Declare PtrSafe Function SetThreadContext Lib "kernel32" Alias "SetThreadContext" (ByVal hThread As LongPtr, lpContext As CONTEXT) As Long
Declare PtrSafe Function SuspendThread Lib "kernel32" Alias "SuspendThread" (ByVal hThread As LongPtr) As Long
Declare PtrSafe Function ResumeThread Lib "kernel32" Alias "ResumeThread" (ByVal hThread As LongPtr) As Long
Declare PtrSafe Function FindResource Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As LongPtr, ByVal lpName As String, ByVal lpType As String) As LongPtr
Declare PtrSafe Function FindResourceEx Lib "kernel32" Alias "FindResourceExA" (ByVal hModule As LongPtr, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As Long) As LongPtr
Declare PtrSafe Function BeginUpdateResource Lib "kernel32" Alias "BeginUpdateResourceA" (ByVal pFileName As String, ByVal bDeleteExistingResources As Long) As LongPtr
Declare PtrSafe Function UpdateResource Lib "kernel32" Alias "UpdateResourceA" (ByVal hUpdate As LongPtr, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As Long, lpData As Any, ByVal cbData As Long) As Long
Declare PtrSafe Function EndUpdateResource Lib "kernel32" Alias "EndUpdateResourceA" (ByVal hUpdate As LongPtr, ByVal fDiscard As Long) As Long
Declare PtrSafe Function LoadResource Lib "kernel32" Alias "LoadResource" (ByVal hInstance As LongPtr, ByVal hResInfo As LongPtr) As LongPtr
Declare PtrSafe Function LockResource Lib "kernel32" Alias "LockResource" (ByVal hResData As LongPtr) As LongPtr
Declare PtrSafe Function SizeofResource Lib "kernel32" Alias "SizeofResource" (ByVal hInstance As LongPtr, ByVal hResInfo As LongPtr) As Long
' Predefined Resource Types
Const RT_CURSOR = 1&
Const RT_BITMAP = 2&
Const RT_ICON = 3&
Const RT_MENU = 4&
Const RT_DIALOG = 5&
Const RT_STRING = 6&
Const RT_FONTDIR = 7&
Const RT_FONT = 8&
Const RT_ACCELERATOR = 9&
Const RT_RCDATA = 10&
Declare PtrSafe Function InitAtomTable Lib "kernel32" Alias "InitAtomTable" (ByVal nSize As Long) As Long
Declare PtrSafe Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Declare PtrSafe Function GlobalDeleteAtom Lib "kernel32" Alias "GlobalDeleteAtom" (ByVal nAtom As Integer) As Integer
Declare PtrSafe Function GlobalFindAtom Lib "kernel32" Alias "GlobalFindAtomA" (ByVal lpString As String) As Integer
Declare PtrSafe Function GlobalGetAtomName Lib "kernel32" Alias "GlobalGetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
' User Profile Routines
' NOTE: The lpKeyName argument for GetProfileString, WriteProfileString,
' GetPrivateProfileString, and WritePrivateProfileString can be either
' a string or NULL. This is why the argument is defined as "As Any".
' For example, to pass a string specify ByVal "wallpaper"
' To pass NULL specify ByVal 0&
' You can also pass NULL for the lpString argument for WriteProfileString
' and WritePrivateProfileString
Declare PtrSafe Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long
Declare PtrSafe Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare PtrSafe Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Declare PtrSafe Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare PtrSafe Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Declare PtrSafe Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare PtrSafe 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
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
Declare PtrSafe Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare PtrSafe Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Declare PtrSafe Function Get『Windows』Directory Lib "kernel32" Alias "Get『Windows』DirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare PtrSafe Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Declare PtrSafe Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
Declare PtrSafe Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectory" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Declare PtrSafe Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long
Declare PtrSafe Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Declare PtrSafe Function CreateDirectoryEx Lib "kernel32" Alias "CreateDirectoryExA" (ByVal lpTemplateDirectory As String, ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Declare PtrSafe Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long
Declare PtrSafe Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
Const DDD_RAW_TARGET_PATH = &H1
Const DDD_REMOVE_DEFINITION = &H2
Const DDD_EXACT_MATCH_ON_REMOVE = &H4
Declare PtrSafe Function DefineDosDevice Lib "kernel32" Alias "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName As String, ByVal lpTargetPath As String) As Long
Declare PtrSafe Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As String, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
Declare PtrSafe Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As LongPtr) As LongPtr
Declare PtrSafe Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
我20多年的VBA实践经验,全部浓缩在下面的各个教程中: