Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Word Wait 5 Seconds: How to Pause Your Macro

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To make a VBA macro in Microsoft Word wait for 5 seconds, the most reliable method is using the Application.Wait command or the Windows API Sleep function. For most users, the Application.Wait method is preferred because it is built directly into the Office environment and does not require complex declarations. This pause is essential when your macro interacts with external applications, waits for a document to save, or allows a user time to read a message before the screen changes.

What You'll Need

  • Microsoft Word installed on a Windows PC.
  • The Developer tab enabled in the Word Ribbon.
  • Basic access to the Visual Basic for Applications (VBA) editor.
  • A document saved as a Word Macro-Enabled Document (.docm) to ensure the code is preserved.

Step-by-Step

  1. Open the Developer Tab: If you do not see the Developer tab at the top of your Word window, right-click anywhere on the Ribbon and select "Customize the Ribbon". In the right-hand column, check the box next to "Developer" and click OK. Labels may vary slightly depending on your version of Word.
  2. Enter the VBA Editor: Click the "Visual Basic" button on the far left of the Developer tab, or press Alt + F11 on your keyboard. This opens the VBA IDE (Integrated Development Environment).
  3. Insert a New Module: In the Project Explorer window on the left, right-click on your document name (e.g., "Project (Document1)"), hover over "Insert", and select "Module". This creates a clean space to write your code away from the default document objects.
  4. Write the Wait Procedure: In the blank module window, type the following code to create a simple macro that pauses for 5 seconds:
    Sub PauseMacro()
            MsgBox "The macro will now pause for 5 seconds."
            Application.Wait (Now + TimeValue("0:00:05"))
            MsgBox "5 seconds have passed!"
        End Sub
  5. Understand the Logic: The Application.Wait command takes a specific time as its argument. Now represents the current system time, and TimeValue("0:00:05") adds exactly five seconds to that current time. The macro will freeze all Word activity until the system clock reaches that calculated time.
  6. Run the Macro: You can run the code directly from the editor by pressing F5 or by returning to the Word document, clicking "Macros" on the Developer tab, selecting "PauseMacro", and clicking "Run".
  7. Alternative Method (The Sleep API): If you need a more precise pause that doesn't rely on the system clock, you can use the Windows API. At the very top of your module (above all Sub procedures), declare the Sleep function:
    #If VBA7 Then
        Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
        #Else
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
        #End If
    Then, inside your macro, simply use Sleep 5000 (since Sleep uses milliseconds, 5000 equals 5 seconds).

Common Mistakes

One of the most frequent errors is using the wrong time format within the TimeValue function. If you enter "5" instead of "0:00:05", VBA will throw a Type Mismatch error because it expects a string that looks like a time (hours:minutes:seconds). Always ensure the format is strictly adhered to.

Another common mistake is forgetting the PtrSafe attribute when using the Sleep API on 64-bit versions of Microsoft Office. Modern versions of Word are typically 64-bit; if you use the old 32-bit declaration, the macro will crash immediately upon opening the document. Using the #If VBA7 conditional compilation block, as shown in the steps above, prevents this by detecting the version of VBA automatically.

Users also often confuse Application.Wait with a "background" pause. It is important to realize that Application.Wait freezes the entire Word application. You cannot type in the document or click buttons while the wait is active. If you need the application to remain responsive, you would need to use the DoEvents loop, though this is significantly more complex and can consume high CPU resources if not handled correctly.

Tips

To make your code more flexible, avoid hard-coding the "5 seconds" directly into the Wait command. Instead, create a variable at the start of your macro. For example, Dim pauseDuration As String: pauseDuration = "0:00:05". This allows you to change the wait time in one place rather than searching through hundreds of lines of code for every instance of the pause.

When using pauses to wait for an external process—such as a PDF converter or a web page to load—remember that 5 seconds is a guess. Some computers are faster than others. A better approach is to combine a short wait with a loop that checks if the external process is finished. However, for simple UI delays or giving the user a moment to breathe, a fixed 5-second wait is perfectly sufficient.

If you are using the Sleep method, remember that the application will appear "Not Responding" to Windows if the sleep duration is too long (usually over 10-20 seconds). For very long pauses, Application.Wait is slightly more stable, though both will essentially lock the UI.

Troubleshooting

If the macro does not seem to pause and jumps straight to the next line, check if you have any other active processes or add-ins that might be overriding the VBA execution. Ensure that your system clock is functioning correctly, as Application.Wait relies entirely on the system time.

If you receive a "Compile Error: Sub or Function not defined" when using Sleep, it means the declaration at the top of the module is missing or incorrectly typed. Ensure the Declare statement is in the General Declarations section (the very top of the module) and not inside a Sub or Function block.

If Word freezes completely and will not recover after the 5 seconds, you may have created an infinite loop elsewhere in your code that is interacting poorly with the pause. Try stepping through the code one line at a time by pressing F8 in the VBA editor to identify exactly where the freeze occurs.

In cases where the macro works on your machine but fails on another, check the Office bit-version. A 32-bit declaration of the Sleep API will fail on a 64-bit machine. Re-verify that you used the #If VBA7 wrapper to ensure cross-compatibility.

Frequently Asked Questions

Does Application.Wait stop other programs from running?

No, it only pauses the Microsoft Word instance where the macro is running. Other applications like Chrome, Outlook, or Excel will continue to function normally.

Can I use a wait command to wait for a specific file to appear?

While you can use a 5-second wait as a buffer, it is better to use a Do While Dir("path_to_file") = "" loop with a small DoEvents call to check for the file's existence in real-time.

Which is better: Application.Wait or Sleep?

Application.Wait is better for beginners because it requires no API declarations and is easier to read. Sleep is better for high-precision timing (milliseconds) and is generally faster to execute.

Will the wait command make my computer slow?

No, the wait command tells the processor to stop executing the macro for a set time. It does not put a heavy load on the CPU; it simply halts the sequence of events in Word.

Can I pause a macro for 1 second instead of 5?

Yes, simply change the TimeValue string to "0:00:01" or, if using the Sleep function, change the value to 1000.