【问题标题】:Application.wait is not executing where it has been writtenApplication.wait 没有在它被写入的地方执行
【发布时间】:2020-06-10 20:36:18
【问题描述】:

我想在宏的两个语句之间延迟 10 秒。我正在使用 excel vba 向其他软件发出命令

使用 wsActive

ExportPath = "C:\"
sTempFileNamevbs = ExportPath & Trim(.Name) & ".vbs"
iFileNumvbs = FreeFile
Open sTempFileNamevbs For Output As #iFileNumvbs
Print #iFileNumvbs, "Set Processes = GetObject(" & Chr(34) & "winmgmts:" & Chr(34) & ").InstancesOf(" & Chr(34) & "Win32_Process" & Chr(34) & ")"
Print #iFileNumvbs, "For Each Process In Processes"
Print #iFileNumvbs, "If StrComp(Process.Name, " & Chr(34) & "abcd.exe" & Chr(34) & ", vbTextCompare) = 0 Then"
Print #iFileNumvbs, "With CreateObject(" & Chr(34) & "WScript.Shell" & Chr(34) & ")"
Print #iFileNumvbs, ".AppActivate Process.ProcessId"
orty = w.Cells(j, 4).Value

**'that time delay is executing here before running of below statements**

If (Trim(orty) = "this") Then

Print #iFileNumvbs, ".SendKeys " & Chr(34) & "{Esc}" & Chr(34) & ""
Print #iFileNumvbs, ".SendKeys " & Chr(34) & "+{F3}" & Chr(34) & ""
Print #iFileNumvbs, ".SendKeys " & Chr(34) & "{Esc}" & Chr(34) & ""
Print #iFileNumvbs, ".SendKeys " & Chr(34) & "+{F3}" & Chr(34) & ""
End If

inst = w.Cells(j, 15).Value
nsenfo = w.Cells(j, 1).Value
limitsl = w.Cells(j, 5).Value

Application.Wait (Now + TimeValue("0:00:10")) '这里没有执行这个时间延迟

If (Trim(nsenfo) = "NSE") And (Trim(limitsl) = "SL") Then

Print #iFileNumvbs, ".SendKeys"; Spc(1); "" & Chr(34) & ""; "+{TAB 4}"; "" & Chr(34) & ""
Print #iFileNumvbs, ".SendKeys"; Spc(1); "" & Chr(34) & ""; w.Cells(j, 5).Value; "" & Chr(34) & ""

【问题讨论】:

  • 延迟的意义何在?它在此代码中没有任何用途。
  • 更一般的说明 - 您是否动态创建 VBScript 代码以便可以从 VBA 宏中执行它?如果是,请不要这样做。
  • 此代码在股票市场软件中下订单。通过按 shift+F3,它会打开订单窗口并填充从工作表中获取的所有值。但是在 shift+F3 之后出现一些时间窗口需要 1 或 2 秒,但是值填充会立即开始,这会导致错误。因此,在按下 shift+F3 后需要一些延迟以确保订单窗口已打开。但此延迟是在打开订单窗口之前执行的。
  • 其实这段代码不是我写的,我不是程序员。我只是在编辑它。
  • 是的,这行不通。此 VBA 代码生成 不同 代码 (VBScript) 并将其存储在文件中。稍后(我假设)这个 VBScript 文件被执行。在 VBA 宏中等待的时间不会使 VBScript 代码变慢。

标签: vba


【解决方案1】:

此 VBA 宏生成不同代码 (VBScript) 并将其存储在文件中。后来这个 VBScript 文件似乎是单独执行的。在 VBA 宏中等待的时间不会使 VBScript 代码变慢。

您真正需要做的是丢弃所有创建和执行 VBS 文件的代码。所有这些工作都可以而且应该在 VBA 中完成。像这样 - 代码基本完全相同,但less,因为它不需要写入文件的部分:

Dim Processes, Process As Variant
Dim Shell As Object

Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
Set Shell = CreateObject("WScript.Shell")

For Each Process In Processes
  If StrComp(Process.Name, "abcd.exe", vbTextCompare) = 0 Then
    Shell.AppActivate Process.ProcessId

    If Trim(w.Cells(j, 4).Value) = "this" Then
      Application.Wait Now + TimeValue("0:00:10")

      With Shell
        .SendKeys "{Esc}"
        .SendKeys "+{F3}"
        .SendKeys "{Esc}"
        .SendKeys "+{F3}"
      End If
    End If

    Exit For
  End If
Next

但是,如果您害怕进行这种更改,您可以从当前代码中删除 Application.Wait 行,而是添加:

Print #iFileNumvbs, "WScript.Sleep(10000)"

将等待的指令添加到 VBScript 文件中。 (WScript.Sleep 需要几毫秒)。

【讨论】:

  • 这适用于所有版本的 excel 吗?并且使用以前的代码,我得到了一个 EULA.bat 文件,该文件具有一些注册表编辑,具有术语“ENABLEeUAC”。这个文件有什么用。
  • @user1447820 是的,这两种方法都适用于所有版本的 Excel。不知道 .bat 文件。
  • 面临这个新代码的问题,即使程序 abcd.exe 没有运行此代码发送密钥并且这些密钥是在我按下按钮调用此宏的 excel 上键入的。
  • 您需要将按键移动到For 循环中,请参阅修改后的答案。 (无论如何 - 你不应该复制我的代码,而应该以同样的信念修改你的代码
  • 再查询一次,我想给出 0.5(半)秒的间隔,那么如何使用 Application.Wait Now + TimeValue("0:00:01")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 2022-11-12
  • 2019-05-04
  • 1970-01-01
相关资源
最近更新 更多