Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA to copy a file from one directory to another

Tags:

copy

vba

So I have an access file that I regularly need copied to another directory, replacing the last version. I would like to use an Excel macro to achieve this, and would also like to rename the file in the process.

E.g.

   fileName = "X:\Database\oldName.accdb"    copyDestination = "Y:\dbstore\"    newName = "newName.accdb" 

Is there an easy way of doing this?

like image 523
harryg Avatar asked Jun 05 '13 14:06

harryg


People also ask

How do I copy a file from one directory to another in VB net?

Use the CopyFile method to copy a file, specifying a source file and the target directory. The overwrite parameter allows you to specify whether or not to overwrite existing files.


2 Answers

This method is even easier if you're ok with fewer options:

FileCopy source, destination 
like image 156
Jon Avatar answered Sep 22 '22 18:09

Jon


Use the appropriate methods in Scripting.FileSystemObject. Then your code will be more portable to VBScript and VB.net. To get you started, you'll need to include:

Dim fso As Object Set fso = VBA.CreateObject("Scripting.FileSystemObject") 

Then you could use

Call fso.CopyFile(source, destination[, overwrite] ) 

where source and destination are the full names (including paths) of the file.

See https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/copyfile-method

like image 39
Bathsheba Avatar answered Sep 20 '22 18:09

Bathsheba