PDA

View Full Version : Run-Time error -- GetOpenFilename / Open Browse Box



dkelb
11-23-2005, 08:03 AM
I am gettting an error:

Method 'GetOpenFilename' of object '_Application' failed

The strange thing is that it works fine on a PC. Same versions of Office.

Here is my code:

Dim fname As Variant
Dim FNUM As Long
Dim SLINE As String
Dim I As Long
Dim RW As Long
fname = Application.GetOpenFilename(filefilter:="TEXT FILES (*.TXT), *.TXT")
If fname <> False Then
FNUM = FreeFile()
Open fname For Input As #FNUM
Do While Not EOF(FNUM)
Line Input #FNUM, SLINE
RW = RW + 1
For I = 1 To Len(SLINE)
Cells(RW, I).Value = Mid(SLINE, I, 1)
Next
Loop

dkelb
11-23-2005, 08:55 AM
How can I get an Open File Dialog Browse box using VBA in Excel on a Mac???

I try the following but I get an error:

fname = Application.GetOpenFilename(filefilter:="TEXT FILES (*.TXT), *.TXT")

and

fn = Application.GetOpenFilename(filefilter:="EXCEL FILES (*.XLS), *.XLS")

both produces errors.

Works fine on a PC but not on a Mac.

Thanks!!

Zack Barresse
11-23-2005, 09:04 AM
How about just using the native OpenFileDialog dialog box? I'm not aware of a Mac issue with this. If need be, we can move this topic to the Mac Help forum.

dkelb
11-23-2005, 09:07 AM
Could you give me the syntax for the OpenFileDialog?

Thanks!

vonpookie
11-23-2005, 10:04 AM
Cross-posted (with responses)
http://www.mrexcel.com/board2/viewtopic.php?t=181031

Zack Barresse
11-23-2005, 10:20 AM
Stick to ONE thread please. http://vbaexpress.com/forum/showthread.php?t=6144

And always note your cross posts.

BlueCactus
11-23-2005, 10:25 AM
The strange thing is that it works fine on a PC. Same versions of Office.
You on a Mac? (Although the Office versions cannot be the same.) Arguments for GetOpenFilename are different on Mac.

BlueCactus
11-23-2005, 10:49 AM
From the Help File:

FileFilter Optional Variant. A string specifying file filtering criteria.
In Windows, this string consists of pairs of file filter strings followed by the MS-DOS wildcard file filter specification, with each part and each pair separated by commas. Each separate pair is listed in the Files of type drop-down list box. For example, the following string specifies two file filters, text and addin: "Text Files (*.txt),*.txt,Add-In Files (*.xla),*.xla".

....

On the Macintosh, this string is a list of comma-separated file type codes (for example, "TEXT,XLA5,XLS4"). Spaces are significant and shouldn't be inserted before or after the comma separators unless they're part of the file type code. If omitted, this argument defaults to all file types.

Note that file type codes are rather irritating since almost all interesting file types in Excel are TEXT, which does not allow you to distinguish between .txt, .csv, etc...

Edit: Please note that I was the one who moved / merged / deleted your threads. Also, you should advise those reading your thread on MrExcel that you have cross-posted here.

Zack Barresse
11-23-2005, 10:49 AM
I wonder which line is erroring out?

What if you changed this ..

If fname <> False Then

.. to this ..

If TypeName(fname) <> "Boolean" Then

Here is a working example ..

Option Explicit

Sub TestGOFN()
Dim fname
fname = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If TypeName(fname) = "Boolean" Then
MsgBox "Pressed cancel."
Else
MsgBox "Confirmed file " & fname & "."
End If
End Sub

dkelb
11-23-2005, 12:50 PM
Thanks for the help. But I get the same error as before.

BlueCactus
11-23-2005, 01:59 PM
Please see my post #8 above. You cannot specify filefilter the same way on Mac as you do on Windows. That is where your error is coming from.