can a file obtained using fso be set to a worbook?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
to all
I am using fso object to obtain a file choice from the user. The file will always be an excel workbook
I need to copy -paste date form the selected xls file to another one (easy stuff)
but the question is: How can I SET this file to a workbook?

Thanks
John
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I am using fso object to obtain a file choice from the user.

What do you mean by that? Perhaps you can post your code showing how you are using FSO to obtain user input about a file.
 
Upvote 0
I asked the question while working on it- just snipped to test a few things. See if the following makes sense.

VBA Code:
        sFileSelected = SelectAfile("File choice", "*.xls;*.xlsx", "contain the data")
        'the function just use the normal fso call
            'normal use would be to:     Set file = fso.OpenTextFile(sFileToProcess, 1)
           
'but i want to do
    Dim wbkSource As Workbook 
    Set wbkSource = Workbooks.Open("thefullpathwithname")

    'so i can use the workbook object as normal
 
Upvote 0
VBA Code:
SelectAfile("File choice", "*.xls;*.xlsx", "contain the data")

SelectAfile is not an FSO method or VBA function (VBScript perhaps?). If you want to let the user browse for a file, use the Application.GetFileName function or Application.FileDialog

VBA Code:
Sub FileOpenTest1()
    Dim FilePathName As String
    Dim wbkSource As Workbook
    
    FilePathName = Application.GetOpenFilename("Excel Files (*.xls?), *.xls?", 1, "File Choice")
    
    If FilePathName = "False" Then
        FilePathName = ""
        MsgBox "User Cancelled"
        Exit Sub
    End If

Set wbkSource = Workbooks.Open(FilePathName)
End Sub

VBA Code:
Sub FileOpenTest2()
    Dim PickCancelled As Boolean
    Dim FilePathName As String
    Dim wbkSource As Workbook
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls*", 1
        .Title = "File Choice"
        .AllowMultiSelect = False
        
        PickCancelled = Not CBool(.Show)
        
        If PickCancelled Then
            FilePathName = ""
            MsgBox "User Cancelled"
            Exit Sub
        Else
            FilePathName = .SelectedItems(1)
        End If
    End With
    
    Set wbkSource = Workbooks.Open(FilePathName)
End Sub
 
Upvote 0
Solution
VBA Code:
SelectAfile("File choice", "*.xls;*.xlsx", "contain the data")

SelectAfile is not an FSO method or VBA function (VBScript perhaps?). If you want to let the user browse for a file, use the Application.GetFileName function or Application.FileDialog

VBA Code:
Sub FileOpenTest1()
    Dim FilePathName As String
    Dim wbkSource As Workbook
  
    FilePathName = Application.GetOpenFilename("Excel Files (*.xls?), *.xls?", 1, "File Choice")
  
    If FilePathName = "False" Then
        FilePathName = ""
        MsgBox "User Cancelled"
        Exit Sub
    End If

Set wbkSource = Workbooks.Open(FilePathName)
End Sub

VBA Code:
Sub FileOpenTest2()
    Dim PickCancelled As Boolean
    Dim FilePathName As String
    Dim wbkSource As Workbook
  
    With Application.FileDialog(msoFileDialogFilePicker)
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls*", 1
        .Title = "File Choice"
        .AllowMultiSelect = False
      
        PickCancelled = Not CBool(.Show)
      
        If PickCancelled Then
            FilePathName = ""
            MsgBox "User Cancelled"
            Exit Sub
        Else
            FilePathName = .SelectedItems(1)
        End If
    End With
  
    Set wbkSource = Workbooks.Open(FilePathName)
End Sub
Thanks
SelectAFile() is a function doing all the stuff you show between the With ... End with lines
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top