Private Sub Command1_Click() Open App.Path & "\temp_RTF.rtf" For Output As #1 Print #1, Str_Rtf Close #1 MsgBox "存文件完成!" End SubPrivate Sub Command2_Click() Me.RichTextBox1.LoadFile App.Path & "\temp_RTF.rtf" End SubPrivate Sub Command3_Click() Me.Text2.Text = Me.RichTextBox1.TextRTF End SubPrivate Sub Command4_Click() Me.Text3.Text = Me.RichTextBox1.Text End SubPrivate Sub Form_Load() Me.Text1.Text = Str_Rtf End Sub
用我这个函数吧:) Public Function GetTextFileToStr(zzg_strFileName As String) As String On Error GoTo GetTextFileToStr_Err Dim tmpID As Integer Dim tmpAllStr As String Dim tmpStr() As String tmpID = FreeFile Open zzg_strFileName For Binary As #tmpID tmpAllStr = Space(LOF(tmpID)) Get #tmpID, , tmpAllStr Close #tmpID GetTextFileToStr = tmpAllStr Exit Function GetTextFileToStr_Err: GetTextFileToStr = "" Exit Function End Function
跟读文本文件一个样啊,只要不碰上chr(0)就可以正常写入文本框
Private Sub Command1_Click() Text1.Text = Replace(ReadFile("REPVBTIM.DLL"), Chr(0), "") End SubFunction ReadFile(TXTFile As String) As String On Error GoTo XXX Dim fn As Integer: fn = FreeFile Open TXTFile For Binary Access Read As #fn ReadFile = Space(FileLen(TXTFile)) Get #fn, , ReadFile Close #fn: Exit Function XXX: ReadFile = "读文件时出错" End Function
最简单的方法 Open FileName$ For Binary As #1 Dim B() As Byte: ReDim B(LOF(1) - 1) Get #1, 1, B Close #1 text1.Text = StrConv(B, vbUnicode)
OpenAsTextStream 试试。
用它的load方法加载。
"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\lang2052\f0\fs20\'d5\'c5\'d5\'c5\'a3\'ac\'a3\'ac\'d6\'d0\'bb\'aa\'c8\'cb\'c3\'f1\'b9\'b2\'ba\'cd\'b9\'fa\par" & vbCrLf & _
"}"
Private Sub Command1_Click()
Open App.Path & "\temp_RTF.rtf" For Output As #1
Print #1, Str_Rtf
Close #1
MsgBox "存文件完成!"
End SubPrivate Sub Command2_Click()
Me.RichTextBox1.LoadFile App.Path & "\temp_RTF.rtf"
End SubPrivate Sub Command3_Click()
Me.Text2.Text = Me.RichTextBox1.TextRTF
End SubPrivate Sub Command4_Click()
Me.Text3.Text = Me.RichTextBox1.Text
End SubPrivate Sub Form_Load()
Me.Text1.Text = Str_Rtf
End Sub
小弟有很多不明白这处,你能解释一下吗?再就是.rtf是什么样的文件。
Public Function GetTextFileToStr(zzg_strFileName As String) As String
On Error GoTo GetTextFileToStr_Err
Dim tmpID As Integer
Dim tmpAllStr As String
Dim tmpStr() As String
tmpID = FreeFile
Open zzg_strFileName For Binary As #tmpID
tmpAllStr = Space(LOF(tmpID))
Get #tmpID, , tmpAllStr
Close #tmpID
GetTextFileToStr = tmpAllStr
Exit Function
GetTextFileToStr_Err:
GetTextFileToStr = ""
Exit Function
End Function
Text1.Text = Replace(ReadFile("REPVBTIM.DLL"), Chr(0), "")
End SubFunction ReadFile(TXTFile As String) As String
On Error GoTo XXX
Dim fn As Integer: fn = FreeFile
Open TXTFile For Binary Access Read As #fn
ReadFile = Space(FileLen(TXTFile))
Get #fn, , ReadFile
Close #fn: Exit Function
XXX: ReadFile = "读文件时出错"
End Function
Open FileName$ For Binary As #1
Dim B() As Byte: ReDim B(LOF(1) - 1)
Get #1, 1, B
Close #1
text1.Text = StrConv(B, vbUnicode)