3

私は現在、次のコードを使用して、dosから次のようにコマンドを実行していますVBA

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

すべてが正常に実行されます。唯一の問題は、プログラムがコンピューターで実行しようとしていることを通知する迷惑な警告Windowsボックスが表示されることです。[OK]または[キャンセル]を押してください。

コマンドが完了するまで待つ"objshell"必要があるので、使用する必要があります。VBADOS

警告ボックスがVBA内から表示されたり、DOSコマンドにいくつかの追加パラメーターが追加されたりしないようにする方法はありますか?

7z.exeファイルは(ローカルPCではなく)サーバーで実行されているので、それが問題だと思います。

各マシンで7z.exeを使用またはインストールできません。

4

2 に答える 2

2

以下に、最も速い/最も汚いものから最も堅牢なものの順に3つのオプションを示します。

  1. コマンドラインの一部としてテキストファイルを作成し、その存在を待ちます。コマンドラインを次のように変更し、Shell(ではなくobjShell)を使用して実行します。

    dos_command = "\\\10.xx.xx.xx\test\7z.exe  a -r " etc etc etc
    dos_command = dos_command & " && echo > " & TempFileName
    

    これにより、7-zipコードの完了にTempFileName ちなんで名付けられたテキストファイルが作成されます。TempFileNameシェルコマンドを実行する前に存在しないことを確認してから、コマンドを実行してTempFileNameファイルが存在するのを待つ必要があります。

  2. 使用OpenProcessGetExitCodeProcessAPI :新しいプロセスへのアクセスを提供するOpenProcess API呼び出しを使用してコマンドラインを起動します(Shell関数は起動されたプロセスのProcessIDを返すことに注意してください)。次に、ProcessIDを使用してループに入り、GetExitCodeProcessを介してプロセスをポーリングします。関連する宣言:

    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
    
  3. CreateProcessおよびWaitForSingleObjectAPIを使用します。CreateProcessについては、このヘルプページの「スーパーシェル」の例を参照してください。

于 2012-07-06T14:03:27.190 に答える
1

呼び出すMicrosoft® Windows® Script Hostと、ウィンドウにメッセージが表示されます。代わりにこれを試してください

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ます。[OK]をクリックします
  • ユーザー構成>>管理用テンプレート>>Windowsコンポーネント>>添付ファイルマネージャー
  • 中リスクのファイルタイプ設定の包含リストに7z.exeを追加します。

これは役に立ちます

于 2012-07-06T10:12:49.870 に答える