so i recently got back into VBA and i want to write my code so that it works for both 32- and 64- bit and also for VBA7 and older versions.
The simple Code i came up with is the following:
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function GetSystemDirectoryA Lib "kernel32" _
(ByVal lpBuffer As String, ByVal nSize As LongPtr) As LongPtr
#Else
Private Declare Function GetSystemDirectoryA Lib "kernel32" _
(ByVal lpBuffer As String, ByVal nSize As LongPtr) As LongPtr
#End If
#Else
Private Declare Function GetSystemDirectoryA Lib "kernel32" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
#End If
Public Function GetSystemDirectory() As String
Dim lngLen As LongPtr, lpBuffer As String, nSize As LongPtr
nSize = 255
lpBuffer = String(nSize, vbNullChar)
lngLen = GetSystemDirectoryA(lpBuffer, nSize)
GetSystemDirectory = Left(lpBuffer, lngLen)
End Function
Sub test()
Debug.Print GetSystemDirectory()
End Sub
So now i thought i got it, but if i try to run the code it says:
Error while compiling:
Types Incompatible
When i reverse everything back to Long instead of LongPtr, it works but thats not what i want/need.
I read something that i should only replace some Longs with LongPtr, but that it would also work by just replacing all of them.
As you can see i am quite new to this. Anyway, does anyone know what i dont see?
Thanks in Advance.