MS Access copy and rename file

Hi

How can I copy and rename an existing file using Access vba?

Many thanks

Ken
kenabbottAsked:
Who is Participating?
 
Yossarian-22Connect With a Mentor Commented:
Use this:
FileCopy "c:\test\copythis.txt", "c:\new\target.txt"
0
 
kenabbottAuthor Commented:
That works a treat except if I try it on a Word document that I have created from a template via vba, I get runtime error 53
0
 
Yossarian-22Commented:
Runtime error 53 means "file not found"

Try debugging and accessing the file from the variable you store the string of the file location in.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kenabbottAuthor Commented:
I've checked this numerous times - the file names etc are correct.  It is something to do with initial creating the files in VBA.  I am using the code below.

If I create a file with this called abc.doc via the code below then FileCopy will not work (even after a reboot).  However if I create a file from scratch called abc.doc and save it in the same folder then its works!

Any ideas?




'creat object and populate bookmarks
        Set objWord = CreateObject("Word.Application")
        objWord.Documents.Add strTemplate
        objWord.Visible = False
    
        With objWord.ActiveDocument.Bookmarks
            .Item("Address").Range.Text = strAddress
            .Item("Date").Range.Text = Format(Date, "dd/mm/yy")
        End With
        
    'save and open
        
        objWord.ActiveDocument.SaveAs strFinalPath

Open in new window

0
 
kenabbottAuthor Commented:
UPDATE

I ahve now sorted this the problem is with the code - it is missing objWord.quit so it was leaving an instance of Word open and this seems to be causing the problem.  I also found an unrelated problem that was causing this still to fail after reboot

many thanks
0
 
Yossarian-22Commented:
FileCopy cannot access files in use. Close the active document and set the object to Nothing before you attempt the FileCopy.
From within Word you may use the method mentioned here: http://word.mvps.org/FAQs/MacrosVBA/CopyOpenFile.htm


Otherwise try this method:


Dim objFso As Object
Set objFso = CreateObject("Scripting.FileSystemObject")
 
objFso.CopyFile source, destination, True
 
Set objFso = Nothing

Open in new window

0
All Courses

From novice to tech pro — start learning today.