Home » excel » PtrSafe in Excel 2003 is giving me problems

PtrSafe in Excel 2003 is giving me problems

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am currently making some VBA code applicable for the new win7 machines on the job. This means that many function has to be added with the new 64-bit compatibility. I am currently following the guidelines from this homepage. I find them very useful and implemented following:

#If VBA7 Then
    Declare PtrSafe Function CreateProcess Lib "kernel32"  'More code, see below.
#Else
    Declare Function CreateProcess Lib "kernel32" 'More code, see below.
#EndIf

however, currently VBA is giving me following error message when I save my code:

this error code

NOTE: The code above contains the code described in the previous mentioned link: from jkp-ads. The error appears in the Declare PtrSafe Function CreateProcess Lib "kernel32" Alias "CreateProcessA". The full code can be found here.

What am I missing here?
Thanks!

Update
My full code:

Option Explicit
Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum
#If VBA7 Then
private Declare PtrSafe Function CreateProcess Lib "kernel32" _
Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                        ByVal lpCommandLine As String, _
                        lpProcessAttributes As SECURITY_ATTRIBUTES, _
                        lpThreadAttributes As SECURITY_ATTRIBUTES, _
                        ByVal bInheritHandles As Long, _
                        ByVal dwCreationFlags As Long, _
                        lpEnvironment As Any, _
                        ByVal lpCurrentDriectory As String, _
                        lpStartupInfo As STARTUPINFO, _
                        lpProcessInformation As PROCESS_INFORMATION) As LongPtr

#Else
Declare Function CreateProcess Lib "kernel32" _
Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                       ByVal lpCommandLine As String, _
                       lpProcessAttributes As SECURITY_ATTRIBUTES, _
                       lpThreadAttributes As SECURITY_ATTRIBUTES, _
                        ByVal bInheritHandles As Long, _
                        ByVal dwCreationFlags As Long, _
                        lpEnvironment As Any, _
                        ByVal lpCurrentDriectory As String, _
                        lpStartupInfo As STARTUPINFO, _
                        lpProcessInformation As PROCESS_INFORMATION) As Long
#End If

Now, I want to emphasize a few things. First, I am running Excel 2003 and Windows XP 32bit. In addition to this I have not attempted to run the code yet. Why you may ask. The reason is simple. The editor comes up with a compile error before I am able to do anything. This Compile error can be seen in the picture above this piece of code. I have taken a screenshot of some of the code and especially where the compile error occurs. This is how it looks:

Error

UpdateUpdate: Just found this article from MS support. It appears that the resolution is following: “To resolve this issue, ignore the “Compile error” and run the VBA code in the 64-bit version of the Office 2010 program.” I still hope some solution exist though.

How to&Answers:

Further to my comment below your question (which you have ignored… by mistake I guess?) I believe you are getting that error because you have not specified all the parameters correctly.

Try this and it will compile just fine.

Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum

#If VBA7 Then
    Declare PtrSafe Function CreateProcess Lib "kernel32" _
    Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                            ByVal lpCommandLine As String, _
                            lpProcessAttributes As SECURITY_ATTRIBUTES, _
                            lpThreadAttributes As SECURITY_ATTRIBUTES, _
                            ByVal bInheritHandles As Long, _
                            ByVal dwCreationFlags As Long, _
                            lpEnvironment As Any, _
                            ByVal lpCurrentDriectory As String, _
                            lpStartupInfo As STARTUPINFO, _
                            lpProcessInformation As PROCESS_INFORMATION) As LongPtr
#Else
    Declare Function CreateProcess Lib "kernel32" _
    Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                            ByVal lpCommandLine As String, _
                            lpProcessAttributes As SECURITY_ATTRIBUTES, _
                            lpThreadAttributes As SECURITY_ATTRIBUTES, _
                            ByVal bInheritHandles As Long, _
                            ByVal dwCreationFlags As Long, _
                            lpEnvironment As Any, _
                            ByVal lpCurrentDriectory As String, _
                            lpStartupInfo As STARTUPINFO, _
                            lpProcessInformation As PROCESS_INFORMATION) As Long
#End If

Answer:

This is not a solution, but a path of investigation.

It looks like the compiler is looking at the stuff under #If VBA7 Then. But this shouldn’t be happening at all in Excel 2003.

Try replacing whatever is under #If VBA7 Then with garbage, and add one declaration in the #Else part, e.g.

#If VBA7 Then
    ksjdhfg lkjsdh fgkjh,dlgf sldjkgflsd g 'obvious syntax error
#Else
    Const vOlder As String = "older VBA"
    Declare Function CreateProcess Lib "kernel32" '... keep this part as it was
#End If

Then try to run this to see if it compiles:

Sub tester()
    Debug.Print vOlder
End Sub

This should compile and run fine in Excel 2003 since it has an older version of VBA than 7.0. The stuff below #If VBA7 Then should get completely ignored (regardless of any syntax errors or anything else) and the stuff under #Else, e.g. vOlder, should be accessible.

Let us know if this helps you trace the error.