none
Run-time error 75. Path/File Access Error copying files from openfiledialog RRS feed

  • Question

  • Hi,

    I'm trying to copy files selected in the openfiledialog to c:\Rawdata.

    The code below causes the Run-time error 75.


    Public Sub GetFiles()
    Dim fDialog As Office.FileDialog
    Dim fso As New FileSystemObject
    Dim Path As String
    Dim Filename As String
    Dim varFile As Variant
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

    With fDialog
    .AllowMultiSelect = True
    .Title = "Please select one or more files"
    .Filters.Clear
    .Filters.Add "CSM Files", "*.TXT"
    Path = "C:\RawData"

    If .Show = True Then
    For Each varFile In .SelectedItems
    Filename = varFile
    FileCopy varFile, Path
    Next
    Else
    MsgBox "You clicked Cancel in the file dialog box."
    End If
    End With
    End Sub

     

    From what i've read on-line the error relates to permissions or lack of source file. The file clearly exists and I can copy and paste the file to the destination directory from within the open file dialog window so I can't see a permissions issue. I'm very new to VBA, and I'm probably missing something that should be obvious.

    Any advice much appreciated

    Tuesday, July 5, 2011 8:08 AM

All replies

  • FileCopy requires a valid path and name as the second argument. Change

    For Each varFile In .SelectedItems
    Filename = varFile
    FileCopy varFile, Path
    Next

    to

    For Each varFile In .SelectedItems
    varFile = Split(varFile, "\")
    FileCopy varFile, Path & "\" & varFile(UBound(varFile))
    Next


    HTH, Bernie
    Wednesday, July 6, 2011 4:49 PM
  • Hi Bernie,

    Thanks for your response.

    I see what you mean about the syntax.

    I've tried your suggestion, but for some reason I get

    Run-time error 13 Type mismatch.

    I'll have another look a it later. I've probably done something daft,

    I should probably get some sleep I've been on this all night again :-)

    Wednesday, July 6, 2011 6:11 PM
  • Try troubleshooting - do the values look OK when  you change to this?

    For Each varFile In .SelectedItems
    varFile = Split(varFile, "\")
    Msgbox varFile

    Msgbox  Path & "\" & varFile(UBound(varFile))
    FileCopy varFile, Path & "\" & varFile(UBound(varFile))
    Next


    HTH, Bernie
    Wednesday, July 6, 2011 7:35 PM
  • Hi Bernie,

    Thanks for your help.

    MsgBox varFile gives a type mismatch error.

    I'm guessing maybe it doesn't like Variant datatype, Although if I comment out that line

    the second msg box displays correct destination path and filename.

    The filecopy still gives Type mismatch.

    However I've managed to get the code below to work using a function to strip out the filename.

    Public Sub GetFiles()

    Dim fDialog As Office.FileDialog
    Dim fso As New FileSystemObject
    Dim Path As String
    Dim filename As String
    Dim varFile As Variant

    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)


    With fDialog
    .AllowMultiSelect = True
    .Title = "Please select one or more files"
    .Filters.Clear
    .Filters.Add "CSM Files", "*.TXT"

    Path = "C:\RawData\"

      
    If .Show = True Then
    For Each varFile In .SelectedItems
    filename = GetJustTheFileName(varFile)
    FileCopy varFile, Path & filename
    Next

    Else
    MsgBox "You clicked Cancel in the file dialog box."
    End If
    End With
    End Sub

    It's probably not the most graceful of solutions but it seems to work.

    Many thanks

    Thursday, July 7, 2011 1:21 PM
  • For me, varFile was a string, the fullname of the selected files.

    Bernie


    HTH, Bernie
    Thursday, July 7, 2011 5:59 PM