从文本文件中获取所有文本并在UserForm文本框中显示

时间:2015-09-17 08:22:52

标签: excel vba

我正在使用下面的代码从文本文件中读取文本并将所有文本放在我的UserForm文本框中。我遇到的问题是当文本显示在文本框中时,它显示未格式化,即文本除以换行符,如:

The cat went east
The dog went south

所有读取都在一行上,并在文本中包含这些奇怪的“P”符号,如:

'P'The cat went east 'P'The dog went south'P'

所以我的一个问题是如何恢复要显示的文本格式?:

    The cat went east
    The dog went south

最终我的文字将被截断在文本框的视野之外,因为文本框只有很大但我的文字可能是多行,如:

The cat went east
The dog went south
The cat went east
The dog went south
The cat went east
The dog went south
The cat went east
The dog went south

那么如何在文本框中添加向上/向下滚动功能呢?

代码:

Private Sub UserForm_Activate()

  m1 = Month(Range("C" & ActiveCell.Row).Value)
M = MonthName(m1, True)
Y = Year(Range("C" & ActiveCell.Row).Value)

Dim Total As String
Dim FilePath As String
Dim strLine As String
FilePath = "\\MI-FILESERVE1\Shared Folders\Shared_Business_Dev\Tenders\" & Range("G" & ActiveCell.Row).Value & "\" & Range("E" & ActiveCell.Row).Value & "\" & Range("F" & ActiveCell.Row).Value & " - " & M & " - " & Y & "\log.txt"
Open FilePath For Input As #1
While EOF(1) = False
    'read the next line of data in the text file
    Line Input #1, strLine
    Total = Total & vbNewLine & strLine
    'increment the row counter
    i2 = i2 + 1
Wend
Close #1

TextBox1 = Total
End Sub

0 个答案:

没有答案