VB用API函数浏览打开TXT文本文件,并读取TXT文本文件里的内容换行显示在LIST1(0)里这个怎么修改,帮忙
PrivateDeclareFunctionGetOpenFileNameLib"comdlg32.dll"Alias"GetOpenFileNameA"(pOpenfi...
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "Text Files" & vbNullChar & "*.txt"
ofn.lpstrFile = Space(254) '这里可以改一个默认的文件名
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
'ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "请选择文章"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn <> 0 Then
List9(0).AddItem ofn.lpstrFile
Else
List9(0).AddItem "按了取消键!"
End If 展开
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "Text Files" & vbNullChar & "*.txt"
ofn.lpstrFile = Space(254) '这里可以改一个默认的文件名
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
'ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "请选择文章"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn <> 0 Then
List9(0).AddItem ofn.lpstrFile
Else
List9(0).AddItem "按了取消键!"
End If 展开
1个回答
展开全部
你的意思是吧txt里面的数据全部分行显示到LIST1(0)里面吗?
在里面增加一段代码:
Dim s() As String, a As String
Open ofn.lpstrFile For Binary As #1 '把打开的数据读取到A
a = String(LOF(1), " ")
Get #1, , a
Close #1
s = Split(a, vbCrLf) '转化为数组
For i = 0 To UBound(s)
List1(0).AddItem s(i) '全部写入到list(0)
Next
完整源码下载地址:http://www.hilaiba.cn/Software.asp?id=96
在里面增加一段代码:
Dim s() As String, a As String
Open ofn.lpstrFile For Binary As #1 '把打开的数据读取到A
a = String(LOF(1), " ")
Get #1, , a
Close #1
s = Split(a, vbCrLf) '转化为数组
For i = 0 To UBound(s)
List1(0).AddItem s(i) '全部写入到list(0)
Next
完整源码下载地址:http://www.hilaiba.cn/Software.asp?id=96
更多追问追答
追问
对,但是 ofn.lpstrFilter = "Text Files" & vbNullChar & "*.txt"这句怎么修改是打开它呢,我晕你的能浏览我的电脑吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询