View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Edit Macro script


Hi,

Not sure if you want to do it this way but you could use the FilePicker
dialog box and set the filter to text files and allow the user to pick the
required file.

I have included commented out code for extracting the filename only from the
Path and filename just in case you want it but it is not required in this
case because you need both path and filename.

Note that I have used space and underscores (which are line breaks in
otherwise single lines of code) so that the code will not break at incorrect
places in this post and can be copied directly into your VBA editor without
having to fix broken line problems.

Sub OpenTxtFile()
Dim myTitle As String
Dim sFile As String 'Path and file name
Dim ShortName As String 'FileName only

myTitle = "Select the required text file"
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Text files", "*.txt", 1
.Title = myTitle
If .Show = False Then
Exit Sub
End If
sFile = .SelectedItems(1)
End With

'Following line of code extracts the
'filename only if required
'ShortName = Right(sFile, Len(sFile) _
- InStrRev(sFile, "\"))

Workbooks.OpenText Filename:= _
sFile, _
Origin:=437, _
StartRow:=1, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=True, _
Comma:=False, _
Space:=True, _
Other:=True, _
OtherChar:=":", _
FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), _
Array(4, 1), Array(5, 1), _
Array(6, 1), Array(7, 1), _
Array(8, 1), _
Array(9, 1)), _
TrailingMinusNumbers:=True

End Sub


--
Regards,

OssieMac