您好我想打开一个txt文件(需要弹出打开文件对话框和当前模板相同的文件夹)
然后这个文件需要读作xlsx而不是txt。
这是我目前的代码设置:
Private Sub CommandButton1_Click()
Dim intChoice As Integer
'Select the start folder
Application.FileDialog(msoFileDialogOpen _
).InitialFileName = "I:\Group -*******Chages here******"
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
***** CODE HERE********
End If
End Sub
有人可以识别我的错误吗?
答案 0 :(得分:0)
您可以尝试以下代码段(它可以)并根据您的开发任务进行修改:
Private Sub CommandButton1_Click()
Dim fDialog As Office.FileDialog
' set up the File Dialog var
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'sample init file name
.InitialFileName = "C:\Text.txt"
' set the title of the Dialog box
.Title = "Please select the file to open"
' Clear the current and add some sample filters
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.Filters.Add "Excel Files", "*.xls"
.Filters.Add "All Files", "*.*"
'Show dialog box: if .Show method returns True then execute the code
If .Show = True Then
'Add code here
Else
MsgBox "File Open operation Canceled."
End If
End With
End Sub
希望这可能会有所帮助。亲切的问候,
答案 1 :(得分:0)
或者你可以使用它:
Dim fname
fname = Application.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If IsArray(fname) Then Workbooks.OpenText fname(1)
要查看可用参数 OpenText Method 商品,请参阅MSDN。 HTH。