我的一个函数中有一个布尔问题。它被称为这样
TxtFile True, FileDir, 1, "Hello"
,功能是
TxtFile(WritetoFile As Boolean, FileDir As String, line As Variant, What As String)
。
该函数的第一件事是使用简单的If
和elseif
语句测试true或false。当我运行我的功能时,Watch会告诉我WritetoFile As Boolean
是false
。但是,如果我调用相同的函数并通过DeBug逐步执行它,请注意它是True
。我在另一个表单上以不同的方式调用函数,没有任何问题。
编辑:整体功能
Option Compare Database
Public ReadOptionsOutput As String
Public FSO As New FileSystemObject
Public TxtFileOutPut As String
Public Function TxtFile(WritetoFile As Boolean, FileDir As String, line As Variant, What As String)
Dim FileContent() As Variant
Dim Idx As Integer
Dim Txtstream As Object
If WritetoFile = True Then
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False)
Idx = 0
On Error GoTo Err1 'To catch the last blank line. Dont see another way to see if .ReadLine is blank
Do 'Build an array to edit lines in Text file
Idx = Idx + 1
ReDim Preserve FileContent(1 To Idx)
FileContent(Idx) = Txtstream.ReadLine
Loop
Err1:
Open FileDir For Output As #1: Close #1 'Delet all text inside of File
Set Txtstream = Nothing
Set Txtstream = FSO.OpenTextFile(FileDir, ForAppending, False)
FileContent(line) = What 'Edit line in the array
For Idx = 1 To Idx - 1
Txtstream.WriteLine (FileContent(Idx)) 'Write everything back to textfile
Next
ElseIf WritetoFile = False Then 'Reads Line in file only
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False)
NextLine = 1
Do 'Loop thru to selected line and read it
TxtLine = Txtstream.ReadLine
If NextLine = line Then
TxtFileOutPut = TxtLine
End If
NextLine = NextLine + 1
Loop Until NextLine > line
End If
Txtstream.Close
End Function
答案 0 :(得分:0)
尝试在Sub中分配一个调用该函数的变量。像这样:
Dim WritetoFile As Boolean
WritetoFile = True
TxtFile WritetoFile, FileDir, 1, "Hello"
希望这有帮助。