在VBA Excel的dos命令中运行7Z会导致警告警报
我目前正在使用以下代码从在VBA Excel的dos命令中运行7Z会导致警告警报,vba,warnings,alert,7zip,Vba,Warnings,Alert,7zip,我目前正在使用以下代码从VBA运行dos命令,如下所示 Set objShell = CreateObject("WScript.Shell") dos_command="\\\10.xx.xx.xx\test\7z.exe a -r " etc etc etc result = objShell.Run(dos_command, 0, True) Set objShell =nothing 所有运行良好,唯一的问题是,我收到一个恼人的警告窗口框,提示程序正在我的计算机中运行,请按“确
VBA
运行dos
命令,如下所示
Set objShell = CreateObject("WScript.Shell")
dos_command="\\\10.xx.xx.xx\test\7z.exe a -r " etc etc etc
result = objShell.Run(dos_command, 0, True)
Set objShell =nothing
所有运行良好,唯一的问题是,我收到一个恼人的警告窗口框,提示程序正在我的计算机中运行,请按“确定”或“取消”
我必须使用“objshell”
,因为我需要VBA
等待DOS
命令完成
有没有办法避免警告框从VBA中出现或向DOS命令中添加一些附加参数
7z.exe文件正在服务器(而不是本地PC)中运行,因此我认为这就是问题所在
我无法在每台计算机上使用或安装7z.exe。调用
Microsoft®Windows®脚本主机
会导致Windows显示该消息。相反,试试这个
Public Sub test()
Dim dos_command$, lRet&
dos_command = """\\xxx.xxx.xxx.xxx\xxx\xxx\7z.exe"" a test.zip ""\\xxx.xxx.xxx.xxx\xxx\xxx\*.log"" -r"
lRet = Shell(dos_command, vbMaximizedFocus)
MsgBox lRet
End Sub
更新
您可以执行以下操作并使用代码:
- 打开开始|运行并键入
。单击“确定”gpedit.msc
- 用户配置>>管理模板>>Windows组件>>附件管理器
- 将7z.exe添加到中等风险文件类型设置的包含列表中
Hpe这有助于以下是三个选项,按从最快/最脏到最强健的顺序排列:
Shell
运行它(而不是objShell
):
这将在7-zip代码完成后创建一个名为TempFileName
的文本文件。在运行shell命令之前,只需确保TempFileName
不存在,然后运行该命令并等待TempFileName
文件存在OpenProcess
和GetExitCodeProcess
API:使用API调用启动命令行,该调用提供对新进程的访问(请注意,Shell
函数返回已启动进程的进程ID)。然后使用ProcessID坐在循环中,并通过轮询流程。有关声明:
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Private Const STILL_ACTIVE = &H103
Private Const PROCESS_QUERY_INFORMATION = &H400
'---------------------------------------------------------------------------------------vv
' Procedure : ShellWait
' DateTime : 2/15/2008 10:59
' Author : Mike
' Purpose : Executes a shell command and waits for it to complete.
' Notes : Runs the shell as a batch file, allowing the user to pass a string with
' line breaks to execute a multi-line command.
'
' : Provides two means to break out of the loop.
' 1) Provide a timeout in seconds.
' The code breaks out once it reaches the timeout.
' 2) Provide a flag to tell the procedure to stop running.
' To use this option, you would need to pass the procedure a global flag
' that the user has the ability to change through the interface.
' Update (5/23/2008):
' - Uses a progressive sleep timer to allow fast processes to run quickly
' and long processes to get increasing clock cycles to work with.
' - Changed default window mode to hidden.
'---------------------------------------------------------------------------------------
'^^
Public Function ShellWait(DosCmd As String, _
Optional StartIn As String = "WINDOWS TEMP FOLDER", _
Optional WindowStyle As VbAppWinStyle = vbHide, _
Optional TimeOutSeconds As Long = -1, _
Optional ByRef StopWaiting As Boolean = False) 'vv
On Error GoTo Err_ShellWait
Dim hProcess As Long, RetVal As Long, StartTime As Long
Dim BatName As String, FileNum As Integer, SleepTime As Long
StartTime = Timer
BatName = TempFileName(StartIn, "bat")
FileNum = FreeFile()
Open BatName For Output As #FileNum
ChDrive Left(BatName, 1)
ChDir Left(BatName, InStrRev(BatName, "\"))
Print #FileNum, DosCmd
Close #FileNum
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(BatName, WindowStyle))
SleepTime = 10
Do
'Get the status of the process
GetExitCodeProcess hProcess, RetVal
DoEvents: Sleep SleepTime
If TimeOutSeconds <> -1 Then
If Timer - StartTime > TimeOutSeconds Then Exit Do
End If
If StopWaiting Then Exit Do
'Progressively increase the SleepTime by 10%
' This allows a quick process to finish quickly, while providing
' a long process with increasingly greater clock cycles to work with
SleepTime = SleepTime * 1.1
Loop While RetVal = STILL_ACTIVE
Kill BatName
Exit_ShellWait:
Exit Function
Err_ShellWait:
MsgBox Err.Description
Resume Exit_ShellWait
End Function
'---------------------------------------------------------------------------------------vv
' Procedure : TempFileName
' DateTime : 12/9/08
' Author : Mike
' Purpose : Returns an unused file name but does not create the file. Path can be
' passed with or without the trailing '\'.
' Requires : TempPath() function
'---------------------------------------------------------------------------------------
'^^
Function TempFileName(Optional ByVal Path As String = "WINDOWS TEMP FOLDER", _
Optional Ext As String = "txt", _
Optional Prefix As String = "temp") As String 'vv
Dim TempFName As String, i As Integer
If Path = "WINDOWS TEMP FOLDER" Then Path = TempPath
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Not (Path Like "?:\*" Or Path Like "\\*") Then
Err.Raise 52 '"Bad file name or number."
ElseIf Dir(Path, vbDirectory) = "" Then
Err.Raise 76 '"Path not found."
End If
TempFName = Path & Prefix & "." & Ext
For i = 1 To 500
If Dir(TempFName) = "" Then
TempFileName = TempFName
GoTo Exit_TempFileName
End If
TempFName = Path & Prefix & "_" & Format(i, "000") & "." & Ext
Next i
TempFileName = ""
End Function
'---------------------------------------------------------------------------------------
' Procedure : TempPath
' Author : Mike
' Date : 8/12/2008
' Purpose : Returns something like:
' C:\DOCUME~1\BGRAND~1\LOCALS~1\Temp\
'---------------------------------------------------------------------------------------
'^^
Function TempPath() As String 'vv
Const TemporaryFolder = 2
Static TempFolderPath As String
Dim fs As Object
If Len(TempFolderPath) = 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
TempFolderPath = fs.GetSpecialFolder(TemporaryFolder) & "\"
End If
TempPath = TempFolderPath
End Function
私有声明函数OpenProcess Lib“kernel32”_
(ByVal Dw希望访问,只要_
ByVal bInheritHandle只要_
ByVal dwProcessId As Long)As Long
私有声明函数GetExitCodeProcess Lib“kernel32”_
(ByVal HPPROCESS,只要_
lpExitCode As Long)As Long
私有常量仍处于活动状态=&H103
Private Const PROCESS\u QUERY\u INFORMATION=&H400
“----------------------------------------------------------------------------------------vv
'程序:ShellWait
'日期时间:2/15/2008 10:59
作者:迈克
'目的:执行shell命令并等待其完成。
'注意:以批处理文件的形式运行shell,允许用户通过
'换行以执行多行命令。
'
':提供两种方法来打破循环。
“1)以秒为单位提供超时。
'一旦达到超时,代码就会中断。
'2)提供一个标志,告知过程停止运行。
'要使用此选项,您需要向过程传递一个全局标志
'用户有能力通过界面进行更改。
更新(5/23/2008):
“-使用渐进式睡眠计时器允许快速进程快速运行
'和长进程,以获得越来越多的时钟周期来工作。
“-将默认窗口模式更改为隐藏。
'---------------------------------------------------------------------------------------
'^^
公共函数ShellWait(DosCmd作为字符串_
可选的StartIn As String=“WINDOWS临时文件夹”_
可选窗口样式为VbAppWinStyle=vbHide_
可选的TimeOutSeconds长度=-1_
可选ByRef StopWaiting As Boolean=False)'vv
在出错时转到Err_ShellWait
Dim HPProcess尽可能长,RetVal尽可能长,StartTime尽可能长
Dim BatName为字符串,FileNum为整数,休眠时间为长
开始时间=计时器
BatName=TempFileName(起始,“bat”)
FileNum=FreeFile()
打开BatName作为#FileNum输出
ChDrive Left(蝙蝠名称,1)
ChDir左(BatName,InStrRev(BatName,“\”))
打印#FileNum,DosCmd
关闭#FileNum
hProcess=OpenProcess(进程\查询\信息,False,Shell(BatName,WindowStyle))
睡眠时间=10
做
'获取进程的状态
GetExitCodeProcess HPProcess,RetVal
睡眠时间
如果TimeOutSeconds为-1,则
如果定时器-StartTime>TimeOutSeconds,则退出Do
如果结束
如果停止等待,则退出
“逐步增加睡眠时间10%
“这允许快速流程快速完成,同时提供
“这是一个漫长的过程,需要处理的时钟周期越来越长
睡眠时间=睡眠时间*1.1
返回时循环=仍处于活动状态
杀死蝙蝠名
退出等待:
退出功能
请稍候:
MsgBox错误说明
继续退出,请稍候
端函数
“----------------------------------------------------------------------------------------vv
'过程:TempFileName
'日期时间:12/9/08
作者:迈克
'用途:返回未使用的文件名,但不创建文件。路径可以是
'已传递,带或不带尾随'\'。
'需要:TempPath()函数
'---------------------------------------------------------------------------------------
'^^
函数TempFileName(可选的ByVal路径为String=“WINDOWS临时文件夹”_
可选Ext As String=“txt”_
可选前缀为String=“temp”)为String“vv”
Dim TempFName作为字符串,i作为整数
如果Path=“WINDO
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Private Const STILL_ACTIVE = &H103
Private Const PROCESS_QUERY_INFORMATION = &H400
'---------------------------------------------------------------------------------------vv
' Procedure : ShellWait
' DateTime : 2/15/2008 10:59
' Author : Mike
' Purpose : Executes a shell command and waits for it to complete.
' Notes : Runs the shell as a batch file, allowing the user to pass a string with
' line breaks to execute a multi-line command.
'
' : Provides two means to break out of the loop.
' 1) Provide a timeout in seconds.
' The code breaks out once it reaches the timeout.
' 2) Provide a flag to tell the procedure to stop running.
' To use this option, you would need to pass the procedure a global flag
' that the user has the ability to change through the interface.
' Update (5/23/2008):
' - Uses a progressive sleep timer to allow fast processes to run quickly
' and long processes to get increasing clock cycles to work with.
' - Changed default window mode to hidden.
'---------------------------------------------------------------------------------------
'^^
Public Function ShellWait(DosCmd As String, _
Optional StartIn As String = "WINDOWS TEMP FOLDER", _
Optional WindowStyle As VbAppWinStyle = vbHide, _
Optional TimeOutSeconds As Long = -1, _
Optional ByRef StopWaiting As Boolean = False) 'vv
On Error GoTo Err_ShellWait
Dim hProcess As Long, RetVal As Long, StartTime As Long
Dim BatName As String, FileNum As Integer, SleepTime As Long
StartTime = Timer
BatName = TempFileName(StartIn, "bat")
FileNum = FreeFile()
Open BatName For Output As #FileNum
ChDrive Left(BatName, 1)
ChDir Left(BatName, InStrRev(BatName, "\"))
Print #FileNum, DosCmd
Close #FileNum
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(BatName, WindowStyle))
SleepTime = 10
Do
'Get the status of the process
GetExitCodeProcess hProcess, RetVal
DoEvents: Sleep SleepTime
If TimeOutSeconds <> -1 Then
If Timer - StartTime > TimeOutSeconds Then Exit Do
End If
If StopWaiting Then Exit Do
'Progressively increase the SleepTime by 10%
' This allows a quick process to finish quickly, while providing
' a long process with increasingly greater clock cycles to work with
SleepTime = SleepTime * 1.1
Loop While RetVal = STILL_ACTIVE
Kill BatName
Exit_ShellWait:
Exit Function
Err_ShellWait:
MsgBox Err.Description
Resume Exit_ShellWait
End Function
'---------------------------------------------------------------------------------------vv
' Procedure : TempFileName
' DateTime : 12/9/08
' Author : Mike
' Purpose : Returns an unused file name but does not create the file. Path can be
' passed with or without the trailing '\'.
' Requires : TempPath() function
'---------------------------------------------------------------------------------------
'^^
Function TempFileName(Optional ByVal Path As String = "WINDOWS TEMP FOLDER", _
Optional Ext As String = "txt", _
Optional Prefix As String = "temp") As String 'vv
Dim TempFName As String, i As Integer
If Path = "WINDOWS TEMP FOLDER" Then Path = TempPath
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Not (Path Like "?:\*" Or Path Like "\\*") Then
Err.Raise 52 '"Bad file name or number."
ElseIf Dir(Path, vbDirectory) = "" Then
Err.Raise 76 '"Path not found."
End If
TempFName = Path & Prefix & "." & Ext
For i = 1 To 500
If Dir(TempFName) = "" Then
TempFileName = TempFName
GoTo Exit_TempFileName
End If
TempFName = Path & Prefix & "_" & Format(i, "000") & "." & Ext
Next i
TempFileName = ""
End Function
'---------------------------------------------------------------------------------------
' Procedure : TempPath
' Author : Mike
' Date : 8/12/2008
' Purpose : Returns something like:
' C:\DOCUME~1\BGRAND~1\LOCALS~1\Temp\
'---------------------------------------------------------------------------------------
'^^
Function TempPath() As String 'vv
Const TemporaryFolder = 2
Static TempFolderPath As String
Dim fs As Object
If Len(TempFolderPath) = 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
TempFolderPath = fs.GetSpecialFolder(TemporaryFolder) & "\"
End If
TempPath = TempFolderPath
End Function