Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Copying a File in VBA.

Copying a File in VBA

Written by Allen Wyatt (last updated November 10, 2018)
This tip applies to Word 97, 2000, 2002, and 2003


2

Word uses a macro programming language known as Visual Basic for Applications, or VBA. Many of the tasks you can perform in regular Visual Basic you can also perform in VBA. One such task is to copy files. You can do so by using the FileCopy command, as follows:

FileCopy source, target	

Of course, you need to provide the appropriate specifications for both the source and target designations. For instance, if you wanted to copy a file named MyFile.Doc from the current directory to the F: drive (perhaps where you have a thumb drive) you could do so:

FileCopy myfile.doc, f:

If you try to copy a file that is currently open (either by Word or through a different program), VBA generates an error.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (821) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Copying a File in VBA.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of the Paste Options Box

Paste something in a Word document, and you may notice a dynamic little set of options appear right next to what you ...

Discover More

Printing without Opening

Want to print one or more workbooks without the need of actually opening the file? It's easy to do when you rely on ...

Discover More

Limiting Who Can Delete Data

Excel allows you to protect your worksheet data in several different ways. Unfortunately, it doesn't allow you to protect ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Selecting a Bookmark in a Macro

Bookmarks can be very handy in a document. Word provides a VBA command you can use to easily select any of those bookmarks.

Discover More

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object ...

Discover More

Counting Characters in a Selection with VBA

Need to figure out the number of characters in a range of selected text? Here's how to do it in VBA.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2020-04-03 13:17:04

Rami

Hello Allen,

I've been trying to create a macro that will help me copy and transfer text from one document to another. After selecting text in the first document (Document A), I would like the macro to copy and paste that text into the second document (Document B).
I'm not sure if this is even possible. If it's not, please let me know, and I'll stop researching how to do this.

Thanks!

Rami.


2018-12-31 07:52:04

V.S.Rawat

https://wordmvp.com/FAQs/MacrosVBA/CopyOpenFile.htm

How to copy an open file using VBA

Article contributed by Dave Rado

The VBA FileCopy statement will not copy files that are open. However, the WordBasic equivalent will (this is what is known as progress!).

Unfortunately, the syntax of WordBasic equivalent is different in Word 97 and Word 2000!

The following works even if the file being copied is open:

If Left$(Application.Version, 1) = "8" Then
'Word 97
WordBasic.CopyFile FileName:="c:\OldDirectory\Temp.doc", _
Directory:="C:\NewDirectory\Temp.doc"
Else
'Word 2000 and above
WordBasic.CopyFileA FileName:="c:\OldDirectory\Temp.doc", _
Directory:="C:\NewDirectory\Temp.doc"
End If


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.