0

I would like to have a break where it randomly waits between 5-10 minutes before going to the next i.

Unfortunately the function Application.Wait does not work in Word.

Do you have another solution for me? I don't know much about macros.

- The Error is: Error during compilation:

Constants, character strings of fixed length, user-defined data fields and Declare statements are not permitted as public elements of object modules.


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
PaichengWu
  • 2,649
  • 1
  • 14
  • 28
Fabian Breuer
  • 39
  • 1
  • 7
  • 6
    https://stackoverflow.com/questions/50957565/there-is-no-wait-method-associated-with-application-in-visualbasic-word (see TinMan's answer). – BigBen Nov 13 '19 at 12:57
  • How can I put my random Number in his code`?CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:05")) – Fabian Breuer Nov 13 '19 at 13:03
  • Don't use `Excel.Application`, use `Sleep`. – BigBen Nov 13 '19 at 13:04
  • I've heard that it can crash with "Sleep", the program will run for about 6-9 hours. Will this cause problems? – Fabian Breuer Nov 13 '19 at 13:06
  • I am not sure, you'll have to try it. – BigBen Nov 13 '19 at 13:06
  • Could you do the Sleep example on my code, please? I got an error during compilation – Fabian Breuer Nov 13 '19 at 13:10
  • Can you [edit] your question with the revised code you're using, and the specific error? – BigBen Nov 13 '19 at 13:10
  • 1
    done. the error is translated from german – Fabian Breuer Nov 13 '19 at 13:16
  • 1
    https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/constants-fixed-length-strings-arrays-user-defined-types-and-declare-statements. See the last point (change `Public` to `Private`). – BigBen Nov 13 '19 at 13:18
  • The code works with the change from public to private, but the window freezes, making a crash probable. Isn't there another way? – Fabian Breuer Nov 13 '19 at 13:31
  • "The window freezes" - yes! That's what you've asked for. You've requested Word halts execution for a period of time. I suspect what you're really after is more along the lines of the macro stopping for a while and Word sits idle. That can be done with timers etc. but is a very different process. – CLR Nov 13 '19 at 14:35

1 Answers1

1

If you work on Windows...

Open a normal module (in VBE press Alt+I, M) then place the code below at the beginning.

#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

Then you could use sleep 1000 to wait for one second.

PaichengWu
  • 2,649
  • 1
  • 14
  • 28