Opening an Excel file with VBA

Erich Duff

Board Regular
Joined
Apr 20, 2005
Messages
129
All,

I have the following code in a master workbook that I use to open a folder and then extract a file from it, but it seems to do some weird things: it treats the opened file as if it's another application and, for instance, I can't do a vlookup between that file and another one. There are other inter-workbook functions that it won't let me use as well. Any ideas about why that's happening? Is there other code I could be using? Anybody else have this problem?

Code:
Private Declare Function ShellExecute _
    Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton7_Click()
Dim flOpen As String

ChDrive "G"
ChDir "G:\Inventory Turn Report"
flOpen = Application.GetOpenFilename( _
    fileFilter:="All Files (*.*), *.*")
If Not flOpen = "False" Then
    myDir = Left(test, InStrRev(test, "\"))
    Call ShellExecute(Application.hwnd, "Open", _
        Dir(flOpen), vbNullString, _
        myDir, 3)
End If
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Erich

Why the API function?

Couldn't you just use Workbooks.Open?
 
Upvote 0
Hi Norie,

I use that code because the folder may contain other types of files that I want to open as well, like PDF's or Word docs.

Erich
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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