为什么传递参数会生成错误参数类型ByRef?

时间:2019-10-16 08:54:14

标签: excel vba ms-word

我编写了VBA中邮件合并的延迟。
我以前从未使用过VBA。

邮件合并工作正常。现在,我想在Call Pause(15)中输入一个随机数而不是一个固定的延迟。

Sub Letter_EN()
' Merges one record at a time to email with a pre-defined delay between messages.
' Sourced from: https://www.msofficeforums.com/mail-merge/38282-email-merge-delay.html

If MsgBox("Wirklich an Company_EN 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 = "Company wishes you a merry Christmas!"
      .MailFormat = wdMailFormatHTML
      .MailAddressFieldName = "EMAIL"
      .SuppressBlankLines = True

      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
      End With
      .Execute Pause:=False
    End With
    Call Pause(15)
  Next i
End With
Application.ScreenUpdating = True
End Sub

Public Function Pause(Delay As Long)
Dim Start As Long
Start = Timer
If Start + Delay > 86399 Then
  Start = 0: Delay = (Start + Delay) Mod 86400
  Do While Timer > 1
    DoEvents ' Yield to other processes.
  Loop
End If
Do While Timer < Start + Delay
  DoEvents ' Yield to other processes.
Loop
End Function

我知道300至480秒之间的随机数的公式为:

Dim MyValue As Integer
zahl= Int((480 - 300 + 1) * Rnd + 300)

但是,如果我插入而不是15-MyValue,则会出现错误参数类型ByRef

1 个答案:

答案 0 :(得分:1)

假设您要用300-480范围内的值替换下面的15:

Call Pause(15)

这应该变成:

Dim PauseDelay As Integer
PauseDelay = Int((480 - 300 + 1) * Rnd + 300) ' Stores the random interval between 300-480
Call Pause(PauseDelay) ' Calls Pause with the random interval

我有什么要注意的吗?这段代码看起来很简单,您似乎已经有了答案