Solved

MS Access copy and rename file

Posted on 2008-11-03
6
2,364 Views
Last Modified: 2013-11-27
Hi

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

Many thanks

Ken
0
Comment
Question by:kenabbott
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
Yossarian-22 earned 500 total points
ID: 22867754
Use this:
FileCopy "c:\test\copythis.txt", "c:\new\target.txt"
0
 

Author Comment

by:kenabbott
ID: 22867927
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
 
LVL 3

Expert Comment

by:Yossarian-22
ID: 22868015
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:kenabbott
ID: 22868381
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
 

Author Comment

by:kenabbott
ID: 22868556
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
 
LVL 3

Expert Comment

by:Yossarian-22
ID: 22868629
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now