【发布时间】: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