PtrSafe 64bit vs 32bit APIs in VBA (Excel, Word, PowerPoint, Outlook) example

PtrSafe 64bit vs 32bit APIs in VBA (Excel, Word, PowerPoint, Outlook) example

PtrSafe

The PtrSafe attribute indicates to the VBA compiler that the Declare statement is targeted for the 64bit version of Office. Without this attribute, using the Declare statement in a 64bit system will result in a compile-time error.

Conversion and Types

  1. PtrSafe: Shows that the Declare statement is compatible with 64-bits. This attribute is mandatory on 64-bit systems.
  2. LongPtr: Variable data type which is a 4-bytes data type on 32-bit versions and an 8-byte data type on 64-bit versions of Microsoft Office. This is the recommended way of declaring a pointer or a handle for new code but also for legacy code if it has to run in the 64-bit version of Office. It is only supported in the VBA 7 runtime on 32-bit and 64-bit. Note that you can assign numeric values to it but not numeric types.
  3. LongPtr: An 8-byte data type which is available only in 64-bit versions of Microsoft Office. You can assign numeric values but not numeric types (to avoid truncation).
  4. CLngPtr: Converts a simple expression to a LongPtr data type.
  5. CLngLng: Converts a simple expression to a LongLong data type.
  6. VarPtr: Variant converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).
  7. ObjPtr: Object converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).
  8. StrPtr: String converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).

Declare code example

Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long
Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long
Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long
Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean

Compatible code example

#if Win64 then
	Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long
	Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long
	Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long
	Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean
#else
	Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
	Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
	Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
	Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
#end if

#if VBA7 then
	Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long
	Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long
	Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long
	Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean
#else
	Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
	Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
	Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
	Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
	Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
#end if

Leave a Reply

Your email address will not be published.