Tuesday, July 9, 2013

Copy an open file through Excel VBA

To copy a open file through VBA; FileCopy statement will  not work; instead follow below steps to copy an open file

1. Go to VBA Editor

2.  Add a reference to the Microsoft Scripting Runtime (Tools -References - Microsoft Scripting Runtime)

3. Below is the sample Code 

Sub test()    
   Dim CopyOpenFile As New FileSystemObject     
   CopyOpenFile.CopyFile "c:\test\asd.xlsx", "c:\test2\asd.xlsx", True
End Sub 

c:\test\asd.xlsx - Source 
c:\test2\asd.xlsx - Destination 
True - Overwrite (False - Do not Overwrite)