如何打开浏览器以及从中打开正确的文本文件? [英] How will I open the browser as well as open a proper text file from it?

查看:137
本文介绍了如何打开浏览器以及从中打开正确的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给出了两个选项的for循环,如果没有选择该文件会发生什么。

当我没有给出for循环然后它的工作但是如果我不选择一个文件它会显示错误



我尝试过:



im giving the for loop for two options as to what will happen if the file is not selected.
when i am not giving the for loop for then its working but then if i do not select a file it will show error

What I have tried:

Sub importedrfile()
Worksheets("sheet2").Activate
Dim scan As Integer
Dim myfilepath As String
myfilepath = Application.GetOpenFilename()
If myfilepath = True Then
Open myfilepath For Input As #1
x = 0
Do Until EOF(1)
Line Input #1, linefromfile
lineitems = Split(linefromfile, vbTab)
For scan = 0 To UBound(lineitems) - 1
        lineitems(scan) = Replace(lineitems(scan), Chr(34), "")
Next
ActiveCell.Offset(x, 0).Value = lineitems(0)
ActiveCell.Offset(x, 1).Value = lineitems(1)
ActiveCell.Offset(x, 2).Value = lineitems(2)
ActiveCell.Offset(x, 3).Value = lineitems(3)
ActiveCell.Offset(x, 4).Value = lineitems(4)
ActiveCell.Offset(x, 5).Value = lineitems(5)
ActiveCell.Offset(x, 6).Value = lineitems(6)
ActiveCell.Offset(x, 7).Value = lineitems(7)
ActiveCell.Offset(x, 8).Value = lineitems(8)
ActiveCell.Offset(x, 9).Value = lineitems(9)
ActiveCell.Offset(x, 10).Value = lineitems(10)
ActiveCell.Offset(x, 11).Value = lineitems(11)
ActiveCell.Offset(x, 12).Value = lineitems(12)

x = x + 1
Loop

Close #1
ElseIf myfilepath = False Then
MsgBox ("no file selected")
End If
End Sub

推荐答案

Application.GetOpenFilename Method(Excel) [ ^ ]显示标准的打开对话框并从用户处获取文件名而不实际打开任何文件。当用户取消操作时,此方法返回 False 。所以,下面的代码是错误的!

Application.GetOpenFilename Method (Excel)[^] displays the standard Open dialog box and gets a file name from the user without actually opening any files. When user cancel operation, this method returns False. So, below code is wrong!
Dim myfilepath As String
myfilepath = Application.GetOpenFilename()
If myfilepath = True Then
Open myfilepath For Input As #1





正如MSDN文档所述,您必须检查返回值是否不等于 False





As MSDN documentation states, you have to check if returned value is not equal to False.

fileToOpen = Application _ 
 .GetOpenFilename("Text Files (*.txt), *.txt") 
If fileToOpen <> False Then 
 MsgBox "Open " & fileToOpen 
End If





至于你的代码,我会这样修改:



As to your code, i'd modify it this way:

Dim myfilepath As String
myfilepath = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If myfilepath = CStr(False) Then Exit Sub





有关详细信息,请参阅:以编程方式选择用于Windows的Excel和用于Mac的Excel中的文件 [ ^ ]


这篇关于如何打开浏览器以及从中打开正确的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆