Alternative solution to forced stop in my code when pasting from another document

Jay Zahn 1 Reputation point
2022-01-07T20:48:50.687+00:00

I have written a Word document with VBA that uses logic to assemble a form email. The contents of the email will vary based on fields of a database.

At one point the end of the letter is always the same. The ending statements and the footer, with logo and custom formatting, are copied in from a different Word document that does not change.

As far as why I don't just include this footer in the original document, the amount of formatting required makes it easy for format errors to creep in, and it's easier to program and maintain if the entire letter is assembled from scratch.

Anyway at the point where this footer is copied in, sometimes the footer doesn't get copied in properly. This only happens in my VPN environment. When I run it on my virtual machine, it runs properly.

My (hopefully temporary) fix is to put a couple of non-response message boxes in the program to slow it down, or get it to run how it's supposed to.

Code below:

MsgBox ("Stop 1")
Selection.InsertParagraphAfter
Documents("Mailing Footer.docx").Activate
Selection.WholeStory
Selection.Copy
Documents("Mailing.docm").Activate
Selection.EndKey wdStory
Selection.PasteAndFormat wdPasteDefault
MsgBox ("Stop 2")

The code also works if I use a breakpoint in the code. No matter how fast I run it, how fast I click "OK" to MsgBox, the code ALWAYS works. However Sleep does not work, no matter how long the Sleep is for.

Is there some way I can replicate this sort of forced stop so I or the users are not required to enter a pointless response?

{count} votes