rafi_07max
10-28-2010, 04:56 AM
4794
From the picture we can see a combobox and a button. What I need to do now is that once I select a combobox and hit the button. I should able to retrieve a file by the name that is exactly same as the combobox name. For e.g. when I select apple combobox, I should able to select a file name that must be apple.txt. And the user has the freedom to choose the location of the file (e.g. the file can be in C drive or D drive or any other folder). I managed to create these codes,
Private Sub CommandButton1_Click()
On Error GoTo ErrorHandler
If ComboBox1.Text = "apple" Then
myFile = Application.GetOpenFilename("Text Files,*.txt")
Workbooks.OpenText Filename:= _
myFile, Origin _
:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array _
(4, 1), Array(10, 1), Array(26, 1), Array(27, 1), Array(50, 1), Array(58, 1)), _
TrailingMinusNumbers:=True
ActiveSheet.Move After:=Workbooks("retriveFile.xls").Sheets(1)
ActiveWindow.WindowState = xlMaximized
Exit Sub
End If
ErrorHandler:
MsgBox "Plese select a file", vbInformation, "unable to continue" '& Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
From this code, the user can choose the file from any location. But at the same time he can also any .txt file, which what I want to avoid.
Important:
* File I want to retrieve is in .txt format
* the user must have full freedom in choosing the file location
* But he can only select a .txt file name that is exactly same as to the combobox he select (e.g. if he select apple for the combobox then, he can only open a file name that goes by apple.txt)
From the picture we can see a combobox and a button. What I need to do now is that once I select a combobox and hit the button. I should able to retrieve a file by the name that is exactly same as the combobox name. For e.g. when I select apple combobox, I should able to select a file name that must be apple.txt. And the user has the freedom to choose the location of the file (e.g. the file can be in C drive or D drive or any other folder). I managed to create these codes,
Private Sub CommandButton1_Click()
On Error GoTo ErrorHandler
If ComboBox1.Text = "apple" Then
myFile = Application.GetOpenFilename("Text Files,*.txt")
Workbooks.OpenText Filename:= _
myFile, Origin _
:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array _
(4, 1), Array(10, 1), Array(26, 1), Array(27, 1), Array(50, 1), Array(58, 1)), _
TrailingMinusNumbers:=True
ActiveSheet.Move After:=Workbooks("retriveFile.xls").Sheets(1)
ActiveWindow.WindowState = xlMaximized
Exit Sub
End If
ErrorHandler:
MsgBox "Plese select a file", vbInformation, "unable to continue" '& Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
From this code, the user can choose the file from any location. But at the same time he can also any .txt file, which what I want to avoid.
Important:
* File I want to retrieve is in .txt format
* the user must have full freedom in choosing the file location
* But he can only select a .txt file name that is exactly same as to the combobox he select (e.g. if he select apple for the combobox then, he can only open a file name that goes by apple.txt)