Consulting

Results 1 to 5 of 5

Thread: Solved: Copying a file when the source file is open.

  1. #1
    VBAX Regular
    Joined
    May 2008
    Posts
    46
    Location

    Solved: Copying a file when the source file is open.

    Is there a way to copy a file when the file is open?

    I have the following, but it will error when the file is open:

    [vba]FileCopy "U:\folder\file_name.xls", "U:\folder2\file_name.xls"[/vba]
    ________________________________________
    The more questions I ask and the more I learn, I realize that I don't know squat!!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Const FileName1 As String = "U:\folder\file_name.xls"
    Const FileName2 As String = "U:\folder2\file_name.xls"

    CreateObject("Scripting.FileSystemobject").CopyFile FileName1, FileName2
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    May 2008
    Posts
    46
    Location
    Thanks XLD! It seems to have worked.

    If I want to copy 2 files or more would I just need to duplicate the script, or would it cause issues with something?
    ________________________________________
    The more questions I ask and the more I learn, I realize that I don't know squat!!!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, don't duplicate it, it will instantiate a filesystemobject each time.

    Rather use this approach

    [vba]
    Dim fso As Object

    Set fso = CreateObject("Scripting.FileSystemobject")
    fso.CopyFile FileName1, FileName2
    fso.CopyFile FileName3, FileName4
    'rest of code

    Set fso = Nothing
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    May 2008
    Posts
    46
    Location
    Works great, thanks!
    ________________________________________
    The more questions I ask and the more I learn, I realize that I don't know squat!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •