我的代码有问题,我不知道出了什么问题以及如何解决它。我正在使用 Access 2007。我有以下函数 ExecCmd,其目的是执行参数中的 cmdline,并等到执行完成后将控制权交还给 Access。当我调用此函数时,它返回 2 而不是 0 的退出代码,并且我要执行的批处理文件未执行。
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 PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const SW_HIDE = 0
Private Const STARTF_USESHOWWINDOW = &H1
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim Ret&
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
start.dwFlags = STARTF_USESHOWWINDOW
start.wShowWindow = SW_HIDE
' Start the shelled application:
Ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
' Wait for the shelled application to finish:
Ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, Ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = Ret&
End Function
真正奇怪的是,如果我在调用 ExecCmd 之前调用以下函数 OuvrirUnFichier 一切正常。
' Déclaration de l'API
Private Declare Sub PathStripPath Lib "shlwapi.dll" Alias "PathStripPathA" (ByVal pszPath As String)
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
' Structure du fichier
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
' Constantes
Private Const OFN_READONLY = &H1
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_SHOWHELP = &H10
Private Const OFN_ENABLEHOOK = &H20
Private Const OFN_ENABLETEMPLATE = &H40
Private Const OFN_ENABLETEMPLATEHANDLE = &H80
Private Const OFN_NOVALIDATE = &H100
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_EXTENSIONDIFFERENT = &H400
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_CREATEPROMPT = &H2000
Private Const OFN_SHAREAWARE = &H4000
Private Const OFN_NOREADONLYRETURN = &H8000
Private Const OFN_NOTESTFILECREATE = &H10000
Private Const OFN_SHAREFALLTHROUGH = 2
Private Const OFN_SHARENOWARN = 1
Private Const OFN_SHAREWARN = 0
Public Function OuvrirUnFichier(Handle As Long, _
titre As String, _
TypeRetour As Byte, _
Optional TitreFiltre As String, _
Optional TypeFichier As String, _
Optional RepParDefaut As String) As String
' OuvrirUnFichier est la fonction à utiliser dans votre formulaire pour ouvrir _
' la boîte de dialogue de sélection d'un fichier.
' Explication des paramètres
' Handle = le handle de la fenêtre
' Titre = titre de la boîte de dialogue
' TypeRetour (définit la valeur, de type String, renvoyée par la fonction)
' 1 = chemin complet + nom du fichier
' 2 = nom fichier seulement
' TitreFiltre = titre du filtre
' Exemple: fichier Access
' N'utilisez pas cet argument si vous ne voulez spécifier aucun filtre
' TypeFichier = extention du fichier (sans le .)
' Exemple: MDB
' N'utilisez pas cet argument si vous ne voulez spécifier aucun filtre
' RepParDefaut = répertoire d'ouverture par défaut
' Exemple: C:\windows\system32
' Si vous laissez l'argument vide, par défaut il se place dans le répertoire de votre application
Dim StructFile As OPENFILENAME
Dim sFiltre As String
' Construction du filtre en fonction des arguments spécifiés
If Len(TitreFiltre) > 0 And Len(TypeFichier) > 0 Then
sFiltre = TitreFiltre & " (" & TypeFichier & ")" & Chr$(0) & "*." & TypeFichier & Chr$(0)
End If
sFiltre = sFiltre & "Tous (*.*)" & Chr$(0) & "*.*" & Chr$(0)
' Configuration de la boîte de dialogue
With StructFile
.lStructSize = Len(StructFile) ' Initialisation de la grosseur de la structure
.hwndOwner = Handle ' Identification du handle de la fenêtre
.lpstrFilter = sFiltre ' Application du filtre
.lpstrFile = String$(254, vbNullChar) ' Initialisation du fichier '0' x 254
.nMaxFile = 254 ' Taille maximale du fichier
.lpstrFileTitle = String$(254, vbNullChar) ' Initialisation du nom du fichier '0' x 254
.nMaxFileTitle = 254 ' Taille maximale du nom du fichier
.lpstrTitle = titre ' Titre de la boîte de dialogue
.flags = OFN_HIDEREADONLY ' Option de la boite de dialogue
If ((IsNull(RepParDefaut)) Or (RepParDefaut = "")) Then
RepParDefaut = CurrentDb.Name
PathStripPath (RepParDefaut)
.lpstrInitialDir = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Mid$(RepParDefaut, 1, _
InStr(1, RepParDefaut, vbNullChar) - 1)))
Else: .lpstrInitialDir = RepParDefaut
End If
End With
If (GetOpenFileName(StructFile)) Then ' Si un fichier est sélectionné
Select Case TypeRetour
Case 1: OuvrirUnFichier = Trim$(Left(StructFile.lpstrFile, InStr(1, StructFile.lpstrFile, vbNullChar) - 1))
Case 2: OuvrirUnFichier = Trim$(Left(StructFile.lpstrFileTitle, InStr(1, StructFile.lpstrFileTitle, vbNullChar) - 1))
End Select
End If
End Function
有谁知道问题出在哪里?发生了什么或如何解决?是什么让 OuvrirUnFichier 解决了这个问题?非常感谢
我尝试使用 calc.exe 并且确实有效,但我想做的是执行我使用此代码创建的批处理文件:
Open vPath & "\FtpComm.txt" For Output As fNum
Connexion (fNum) 'function that prints lines to connect to FTP server
Print #fNum, "put " & vFile & " Temp.mdb" 'upload local filename to server file
Deconnexion (fNum) 'function that prints lines to disconnect from FTP server
Close fNum
Open vPath & "\doFtp.bat" For Output As batFileHandle
Print #batFileHandle, "ftp -s:FtpComm.txt >output.txt" '
Close batFileHandle
RetVal = ExecCmd(vPath & "\doFtp.bat")
.bat 文件已正确创建,如果我手动单击它,它会执行并在使用 ExecCmd 执行它时执行我想要的操作,但它不会