Search code examples
vbaautomationms-wordclipboardcopy-paste

Copy and paste protection when running macro


I wrote a macro in Word's VBA, the macro should run for 10-20 minutes, and the macro uses copy and paste operations from one document to another.

I'm afraid the user will copy and paste in other software on the computer while my macro is running, and so the macro will crash, doing unexpected and incorrect things.

I want to warn macro users to be careful not to copy.

But my question is can I prevent users from copying, and that only my macro can run and copy from the Word document and paste in another Word document without issue?

I was looking for ways not to use the macro in copy and paste, but I did not find and this is the only way I know to do what the macro should do.

It should be noted that I can also run the macro in C# via the VSTO plugin.


Solution

  • If you just want to transfer text between the documents without formatting the most efficient way is to use targetRange.Text = sourceRange.Text.

    If you want to transfer text and formatting then use targetRange.FormattedText = sourceRange.FormattedText