Makro: Workbooks.Open - Making Excel File Type irrelevant

Gnijnan

New Member
Joined
Aug 29, 2014
Messages
2
Hello everyone,

I've got a macro which opens an excel workbook. This workbook is sometimes saved as xls and sometimes as xlsx. Therefore, as you can see below, i didn't specify a file type. On my laptop this works. The workbook gets opened, regardless of the filetype.
I send the macro and the folder structure on which it relies to a colleague, but when she runs the macro she gets an error that the file can't be found. If I add
HTML:
& ".xls"
to the macro the file gets opened. But then it doesn't work for xlsx files anymore. I'm 99% sure that the filetype is the error, not something else, like a wrong folder hierarchy.

Does anyone have a solution to correct my macro or that it, say:eek:pens the xlsx if it doesn't find an xls, etc?
Or does anyone have an idea where the error on my colleague's side is? I've got windows 7, she's got windows 8

My code:
HTML:
parentfolder = Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1)    
Workbooks.Open Filename:= _ 
      parentfolder & "\Reporting\Cognos\Order Income\" & Range("G2") & Range("H2") & " OI (EUR CONS) " & Range("F2")
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi and welcome to the board.

A couple of ways I can think of....

First is to ask the user to point to the relevant file:
Code:
' To Use    : vFile = GetFile()'           : vFile = GetFile("S:\Reporting\Cognos\Order Income\")
'---------------------------------------------------------------------------------------
Function GetFile(Optional startFolder As Variant = -1) As Variant
    Dim fle As FileDialog
    Dim vItem As Variant
    Set fle = Application.FileDialog(msoFileDialogFilePicker)
    With fle
        .Title = "Select a File"
        .AllowMultiSelect = False
        .Filters.Add "Order Income Excel Files", "*.xls; *.xlsx", 1
        If startFolder = -1 Then
            .InitialFileName = Application.DefaultFilePath
        Else
            If Right(startFolder, 1) <> "\" Then
                .InitialFileName = startFolder & "\"
            Else
                .InitialFileName = startFolder
            End If
        End If
        If .Show <> -1 Then GoTo NextCode
        vItem = .SelectedItems(1)
    End With
NextCode:
    GetFile = vItem
    Set fle = Nothing
End Function


Public Sub test()


    Dim vfile As Variant
    
    vfile = GetFile("S:\Reporting\Cognos\Order Income\")


End Sub

The other way is to check if the file exists first:
Code:
'----------------------------------------------------------------------
' FileExists
'
'   Returns True if specified file exists.
'----------------------------------------------------------------------
Public Function FileExists(ByVal FileName As String) As Boolean
    Dim FSO As Scripting.FileSystemObject
    Set FSO = New Scripting.FileSystemObject
    FileExists = FSO.FileExists(FileName)
End Function


Sub Test()


    MsgBox FileExists("S:\Bartrup-CookD\_Code Library\mdl_File_Functions.bas"), vbOKOnly


End Sub

If it returns TRUE - then go ahead an open the file, if it returns FALSE then open the other file type.
 
Last edited:
Upvote 0
You could also use Dir and pass ".xls?" as the extension to get the actual file name back.
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,401
Members
448,893
Latest member
AtariBaby

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