你指的是打开流文件还是运行文件啊若是运行文件,用SHELL函数若是流文件,使用 open 路径+文件名 for 模式 #文件号然后根据#文件号来操作即可比如:'读取SQL语句,放到字符串数组 'path为文件的绝对路径 Private Function ReadSQL(ByVal path As String) As Variant Dim strSQL() As String Dim inputData As String Dim i As Integer i = 0 ReDim Preserve strSQL(i) '打开文件 Open Trim(path) For Input As #1 '读取数据 Do While Not EOF(1) '以行读取数据 Line Input #1, inputData '标识符"go"作为判断是否为新的SQL语句 '当行为标识符"go"并且数组不为空时,增加数组长度 If Trim(UCase(inputData)) = "GO" And strSQL(i) <> "" Then i = i + 1 ReDim Preserve strSQL(i) End If If Trim(inputData) <> "go" Then strSQL(i) = Trim(strSQL(i)) + " " + Trim(inputData) Loop '关闭文件 Close #1 '返回数据 ReadSQL = strSQL End Function
同时打开多个数据文件(.txt),然后依次处理
Private Sub Command1_Click() Dim name, fn, i CommonDialog1.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer CommonDialog1.MaxFileSize = 32767 CommonDialog1.ShowOpen name = CommonDialog1.FileName fn = Split(name, Chr(0)) For i = 1 To UBound(fn) Debug.Print fn(0) & IIf(Right(fn(0), 1) = "\", "", "\") & fn(i) '加上你的对单个文件的处理流程 Next End Sub
'path为文件的绝对路径
Private Function ReadSQL(ByVal path As String) As Variant Dim strSQL() As String
Dim inputData As String
Dim i As Integer
i = 0
ReDim Preserve strSQL(i)
'打开文件
Open Trim(path) For Input As #1
'读取数据
Do While Not EOF(1)
'以行读取数据
Line Input #1, inputData
'标识符"go"作为判断是否为新的SQL语句
'当行为标识符"go"并且数组不为空时,增加数组长度
If Trim(UCase(inputData)) = "GO" And strSQL(i) <> "" Then
i = i + 1
ReDim Preserve strSQL(i)
End If
If Trim(inputData) <> "go" Then strSQL(i) = Trim(strSQL(i)) + " " + Trim(inputData)
Loop
'关闭文件
Close #1
'返回数据
ReadSQL = strSQL
End Function
Dim name, fn, i
CommonDialog1.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer
CommonDialog1.MaxFileSize = 32767
CommonDialog1.ShowOpen
name = CommonDialog1.FileName
fn = Split(name, Chr(0))
For i = 1 To UBound(fn)
Debug.Print fn(0) & IIf(Right(fn(0), 1) = "\", "", "\") & fn(i)
'加上你的对单个文件的处理流程
Next
End Sub