Opening a file

G

Guest

Trying to create a macro that prompts the user to open a txt file.

FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
FileNum = FreeFile()
Open FileName For Input As #FileNum

But in place of having them enter the file name, I want to have them browse
for the file then select it. This would be identical to File - Open, and the
Open Browser window appears.

Thanks
 
D

Dave Peterson

Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum
 
G

Guest

Thanks - works great!

Dave Peterson said:
Dim myFileName as variant
dim FileNum as long

myfilename = application.getopenfilename("Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

filenum = freefile()
open myfilename for input as #filenum
 
G

Guest

Is there a way to automatically run the File Wizard on the imported file with
the spacing I want. (The files that will be imported will always appear the
same).

Thanks
 
D

Dave Peterson

No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub
 
G

Guest

Thanks for the input I will try it out.

Dave Peterson said:
No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub
 
G

Guest

Thanks. I'll try it.

Dave Peterson said:
No, but you could record a macro that parses the file when you do it once.

Then modify that code a bit.

I'd put the code into a separate workbook and drop a giant button from the Forms
toolbar onto the only worksheet in that workbook. Assign your macro to that
button.

Kind of...

Option Explicit
Sub Testme01()

Dim myFileName As Variant

myFileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Pick a File")

If myFileName = False Then
MsgBox "Ok, try later" 'user hit cancel
Exit Sub
End If

Workbooks.OpenText Filename:=myFileName '....rest of recorded code here!

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top