应该如下所示。这应该适用于 32 位和 64 位。
Option Explicit
#If Win64 And VBA7 Then
'some of these declaration get red in VBA6 (no worries)
Private Type STARTUPINFO ' x86, Win64
cb As Long ' 4 4
padding1 As Long
lpReserved As String ' 4 8
lpDesktop As String ' 4 8
lpTitle As String ' 4 8
dwX As Long ' 4 4
dwY As Long ' 4 4
dwXSize As Long ' 4 4
dwYSize As Long ' 4 4
dwXCountChars As Long ' 4 4
dwYCountChars As Long ' 4 4
dwFillAttribute As Long ' 4 4
dwFlags As Long ' 4 4
wShowWindow As Integer ' 2 2
cbReserved2 As Integer ' 2 2
padding2 As Long
lpReserved2 As LongPtr ' 4 8
hStdInput As LongPtr ' 4 8
hStdOutput As LongPtr ' 4 8
hStdError As LongPtr ' 4 8
End Type ' Sum: 68 96
Private Type SECURITY_ATTRIBUTES
nLength As Long
padding1 As Long
lpSecurityDescriptor As LongPtr
bInheritHandle As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As LongPtr
hThread As LongPtr
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare PtrSafe Function CreateProcess Lib "Kernel32" Alias "CreateProcessA" ( _
ByVal lpAppName As String, ByVal lpCmdLine As String, lpProcAttr As Any, _
lpThreadAttr As Any, ByVal lpInheritedHandle As Long, ByVal lpCreationFlags As Long, _
ByVal lpEnv As Any, ByVal lpCurDir As String, lpStartupInfo As STARTUPINFO, _
lpProcessInfo As PROCESS_INFORMATION) As LongPtr
Private Declare PtrSafe Function WaitForSingleObject Lib "Kernel32" ( _
ByVal hHandle As LongPtr, ByVal dwMilliseconds As Long) As Long
Private Declare PtrSafe Function CloseHandle Lib "Kernel32" ( _
ByVal hObject As LongPtr) As Long
#Else
'some of these declaration get red in VBA7 (no worries)
Private Declare Function CreateProcess Lib "Kernel32" Alias _
"CreateProcessA" ( _
ByVal lpAppName As Long, _
ByVal lpCmdLine As String, _
ByVal lpProcAttr As Long, _
ByVal lpThreadAttr As Long, _
ByVal lpInheritedHandle As Long, _
ByVal lpCreationFlags As Long, _
ByVal lpEnv As Long, _
ByVal lpCurDir As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInfo As PROCESS_INFORMATION _
) As Long
Private Declare Function WaitForSingleObject Lib "Kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long _
) As Long
Private Declare Function CloseHandle Lib "Kernel32" ( _
ByVal hObject As Long _
) As Long
'Einige Datentypen erstellen
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 Long
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 Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
#End If
Public Sub ExecCmd(ByVal cmdline As String)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
#If Win64 And VBA7 Then
Dim ReturnValueProcess As LongPtr
#Else
Dim ReturnValueProcess As Long
#End If
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ReturnValueProcess = CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
Dim ReturnValue As Long
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0)
DoEvents
Loop Until ReturnValue <> 258
ReturnValue = CloseHandle(proc.hProcess)
End Sub