After this line:
filepath = SelectFolder("Please Choose the location of the .txt files")
Add this:
Dim x As Long
Dim sTemp As String
For x = 1 To Len(filepath)
If Mid$(filepath, x, 1) < Chr$(0) Then
sTemp = sTemp & Mid$(filepath, x, 1)
End If
Next
' let's see what we got:
If Len(sTemp) 0 Then
Debug.Print sTemp
Else
Debug.Print "User canceled"
exit sub
End If
' Then resume with the rest of the code
In article .com, Rob wrote:
Hi Mike
Thanks for the suggestion but that doesn't work. I tried it woth out
the quotes too and just got a type mismatch error as it's a string not
boolean. Here's the code, maybe that'll shed some light.
Const BIF_RETURNONLYFSDIRS = 1
Const BIF_NEWDIALOGSTYLE = &H40
Const MAX_PATH = 260
Type BrowseInfo
hWndOwner As Long
pidlRoot As Long
pszDisplayName As Long
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Integer
End Type
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function SHBrowseForFolder Lib "shell32" _
(pBrInfo As BrowseInfo) As Long
Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Declare Sub CoTaskMemFree Lib "ole32.dll" _
(ByVal pMem As Long)
Option Explicit
Sub Open_Files()
Dim filepath As String
filepath = SelectFolder("Please Choose the location of the .txt files")
Workbooks.OpenText Filename:= _
filepath + "\file1.txt" _
, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited,
TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1,
1), _
TrailingMinusNumbers:=True
'repeat open for files 2 and 3
End Sub
Public Function SelectFolder(sTitle) As String
Dim nPos As Long
Dim pidList As Long
Dim nResult As Long
Dim sPath As String
Dim pBInfo As BrowseInfo
sPath = String(MAX_PATH, Chr(0))
sTitle = sTitle & Chr(0)
With pBInfo
'Set the owner window (current active Window)
.hWndOwner = GetActiveWindow()
.lpszTitle = sTitle
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
End With
pidList = SHBrowseForFolder(pBInfo)
If pidList < 0 Then
SHGetPathFromIDList pidList, sPath
CoTaskMemFree pidList
nPos = InStr(sPath, Chr(0))
If nPos 0 Then
sPath = Left(sPath, nPos - 1)
End If
End If
SelectFolder = sPath
End Function
Alls you have to do to get this to work is copy the code to a new
worksheet/module as normal. Then copy the following into notepad and
save it as 'file1.txt', then run the macro and navigate to wherever you
saved it.
Time Volt drop Battery current
40 0.005 19.8
180 0.002 9.9
Thanks again
Rob
--
Steve Rindsberg, PPT MVP
PPT FAQ:
www.pptfaq.com
PPTools:
www.pptools.com
================================================