Application.wait for Word

时间:2019-11-13 12:55:53

标签: excel vba ms-word

我想休息一下,在进入下一个i之前,它会随机等待5-10分钟。

不幸的是,功能Application.Wait在Word中不起作用。

您还有其他解决方案吗? 我对宏不是很了解。

  

-   错误是:编译期间错误:

     

常量,固定长度的字符串,用户定义的数据字段和Declare语句不允许作为对象模块的公共元素。

     
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Sub Test_DE()
    If MsgBox("Wirklich senden?", vbYesNo, "Senden") <> vbYes Then Exit Sub
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument
      For i = 1 To .MailMerge.DataSource.RecordCount
        With .MailMerge
          .Destination = wdSendToEmail
          .MailSubject = "xxxx"
          .MailFormat = wdMailFormatHTML
          .MailAddressFieldName = "EMAIL"
          .SuppressBlankLines = True

          With .DataSource
            .FirstRecord = i
            .LastRecord = i
            .ActiveRecord = i
          End With
          .Execute Pause:=False
        End With
        Delay = Int((600000 - 300000 + 1) * Rnd + 300)
        Sleep (Delay)
        'CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:Delay"))
        'Dim PauseDelay As Long
        'PauseDelay = Int((600 - 300 + 1) * Rnd + 300) ' Stores the random interval between 300-480
        'Call Pause(PauseDelay) ' Calls Pause with the random interval
      Next i
    End With
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

如果您在Windows上工作...

打开一个 normal 模块(在VBE中按 Alt + I M ),然后将代码放在下面一开始。

#If VBA7 And Win64 Then
    ' For 64bit version of Excel
    Public Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As LongPtr)
#Else
    ' For 32bit version of Excel
    Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
#End If

然后,您可以使用sleep 1000等待一秒钟。