+ Reply to Thread
Results 1 to 11 of 11

VBA FSO copyfile error

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2011
    Location
    Brighton, UK
    MS-Off Ver
    Excel 2010 & Office 365
    Posts
    41

    VBA FSO copyfile error

    Hi All,

    I am having an issue implementing the FileSystemObject in a piece of code and wondered if anyone could help....!

    The code I am successful with is:

    Dim fso
    Set fso = CreateObject("scripting.filesystemobject")
    fso.copyfile "C:\Users\Me\Folder\file1.pdf" , "C:\Users\Me\Folder\Archived\file2.pdf"
    However when I try and use this code as an alternative it falls over with a 'Run-time Error 5

    Dim fso
    Dim Filename
    Dim Sourcename
    
    Set fso = CreateObject("scripting.filesystemobject")
    Set Filename = Me.Sheets("sheet1").Cells("A1")               'this cell contains the same path as file1 above
    Set Sourcename = Me.Sheets("sheet1").Cells("A2")              'this cell contains the same path as file2 above
    
    fso.copyfile Filename , Sourcename

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    You're not getting objects off the sheet, you're getting text, which is what copyfile is expecting.

    Remove the 2 x Set for the file names.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-09-2011
    Location
    Brighton, UK
    MS-Off Ver
    Excel 2010 & Office 365
    Posts
    41

    Re: VBA FSO copyfile error

    Hi TMShucks,

    Thanks for replying. Do mean as follows:

    Dim fso
    Dim Filename
    Dim Sourcename
    
    Set fso = CreateObject("scripting.filesystemobject")
    Filename = Me.Sheets("sheet1").Cells("A1")              
    Sourcename = Me.Sheets("sheet1").Cells("A2")            
    
    fso.copyfile Filename , Sourcename
    I tried that and I'm getting the same error but at the 'filename' line

    Regards, witham

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    Lose the "Me"


    Regards, TMS

  5. #5
    Registered User
    Join Date
    11-09-2011
    Location
    Brighton, UK
    MS-Off Ver
    Excel 2010 & Office 365
    Posts
    41

    Re: VBA FSO copyfile error

    I think my limited knowledge is hindering me here...!

    I've tried with the set removed and the me removed and with the set in and the me removed and I'm getting the same error.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    Maybe the copyfile doesn't like the variant variables. Try Dimming them as String.

    You use Set when you are dealing with objects. So, when you say:

    Set Filename = Me.Sheets("sheet1").Cells("A1")               'this cell contains the same path as file1 above
    Set Sourcename = Me.Sheets("sheet1").Cells("A2")              'this cell contains the same path as file2 above

    You are setting the variables to be the range objects.

    "Me" in relation to a worksheet would be the workbook.


    If you look back at the original code, you were passing copyfile two strings which were fully qualified source and destination locations/filenames. So, you need to pass it two string variables each containing a fully qualified location/filename.

    I hope that clarifies and solves the problem.

    If not, please post a sample workbook with your code.


    Regards, TMS

  7. #7
    Registered User
    Join Date
    11-09-2011
    Location
    Brighton, UK
    MS-Off Ver
    Excel 2010 & Office 365
    Posts
    41

    Re: VBA FSO copyfile error

    I kind of understand what you are saying TMSchucks (and really appreciate your help),
    but er..... I've attached the file!

    TestArchivingBook.xlsm

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    Oh, and be careful about using what might be Excel/VBA reserved words. Hence, sFileName instead of just FileName, etc

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    You know, sometimes you can't see the wood for the trees:

    Sub sCopyFiles()
    
    ' "C:\Users\witham\Google Drive\TESTFOLDER\CS-RAG.pdf", "C:\Users\witham\Google Drive\TESTARCHIVEFOLDER\CS-RAG-june.pdf"
    
    Dim fso
    Dim sFileName As String
    Dim sSourceName As String
    
    ' problem was Cells("A1")
    ' use
    sFileName = Sheets("sheet1").Range("A1")
    sSourceName = Sheets("sheet1").Range("A2")
    ' or
    sFileName = Sheets("sheet1").Cells(1, 1)
    sSourceName = Sheets("sheet1").Cells(2, 1)
    
    Set fso = CreateObject("scripting.filesystemobject")
    fso.copyfile sFileName, sSourceName
    
    End Sub

    Regards, TMS

  10. #10
    Registered User
    Join Date
    11-09-2011
    Location
    Brighton, UK
    MS-Off Ver
    Excel 2010 & Office 365
    Posts
    41

    Re: VBA FSO copyfile error

    TMS,

    Thank you. It works perfectly.
    Notes on naming conventions received and understood!

    Have a great weekend. Witham.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,432

    Re: VBA FSO copyfile error

    You're welcome. Thanks for the rep.


    You have a good one too. We're going to Fountain's Abbey tomorrow so I hope the weather improves!

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1