在Excel VBA中打开txt文件
嗨,我想打开一个txt文件(需要弹出“打开文件”对话框和与当前模板相同的文件夹) 那么这个文件需要读取为xlsx而不是txt 这是我当前的代码设置:在Excel VBA中打开txt文件,vba,excel,Vba,Excel,嗨,我想打开一个txt文件(需要弹出“打开文件”对话框和与当前模板相同的文件夹) 那么这个文件需要读取为xlsx而不是txt 这是我当前的代码设置: Private Sub CommandButton1_Click() Dim intChoice As Integer 'Select the start folder Application.FileDialog(msoFileDialogOpen _ ).InitialFileName = "I:\Group -*******Cha
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
Private子命令按钮1\u单击()
选择整数
'选择开始文件夹
Application.FileDialog(msoFileDialogOpen_
).InitialFileName=“I:\Group-*********此处更改******”
'使文件对话框对用户可见
intChoice=Application.FileDialog(msoFileDialogOpen.Show)
'确定用户所做的选择
如果选择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
希望这能有所帮助。亲切问候,或者您可以使用:
Dim fname
fname = Application.GetOpenFilename("Text Files (*.txt),*.txt", , , , True)
If IsArray(fname) Then Workbooks.OpenText fname(1)
要查看OpenText方法提供的可用参数,请参阅。HTH.那么您的代码片段到底有什么问题?Thx很多。这样就可以让我打开我等待的文件。但我想读取该文件并将其保存为xls而不是txt。有人能进一步帮助我吗?我很高兴它对你有用。如果您对答案感到满意,请接受并分别发布您的第二个问题。我们也会尽力在另一个问题上帮助你。感谢和问候,