Fabian Breuer
Fabian Breuer

Reputation: 39

Why does passing a parameter generate error argumenttype ByRef?

I code a delay for mail merge in VBA.
I have never worked with VBA before.

Mail merge works fine. Now I would like to enter a random number instead of a fixed delay in 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

I know that the formula for a random number between 300 - 480 seconds is:

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

However, if I insert instead of the 15 - MyValue the error argumenttype ByRef comes

Upvotes: 0

Views: 49

Answers (1)

Martin
Martin

Reputation: 16433

Assuming that you are looking to replace the 15 in the following with a value in the range 300 - 480:

Call Pause(15)

This should become:

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

Is there something I am overlooking? This code looks straight-forward and you already seem to have the answer

Upvotes: 1

Related Questions