locked
Add content to a existent rtf file from RichTextBox - (VB.NET 2015) RRS feed

  • Question

  • Hi. Thanks for your attention. I need to save the Richtextbox content on a existent rtf file (whit data).
    I need that the file accumulates data.
    I am using the next code, but this overrides the file existent.

                Dim CarpetaUsuario As String = CapetaLogs & "\" & ppSuUsuario
                Dim NombreArchivo As String = CarpetaUsuario & "\" & Format(Now, "yyMMdd") & ".rtf"
    
                If Not Directory.Exists(CarpetaUsuario) Then
                    My.Computer.FileSystem.CreateDirectory(CarpetaUsuario)
                End If
    
                rtb_chat.SaveFile(NombreArchivo, RichTextBoxStreamType.RichText)


    Tuesday, August 13, 2019 5:48 PM

Answers

  • Hi

    I am not sure of this. Here is some code to try to illustrate.

    This code uses 2 rtf files - first one is ONLY a data source for this example. The second one would be the equivalent of your  NombreArchivo, and will accumulate everything from your rtb_chat. The accumulation is invoked by the Button Click.

    It only makes sense if the rtb_chat is cleared every time the Button is clicked otherwise the accumulated text in NombreArchivo would repeat everything each time.

    If you want to try this out, you will need to add a RTB called rtb_chat, and a Button1. For this example you will need a test RTF file called ReadMe.rtf (it really is only a few lines of RTF including images and formatted text). Then set the paths in the code to suit.

    When running, each time you click Button1, it will append the contents of rtb_chat to the NombreArchivo  file and clear rtb_chat ready for new text.images etc.

    Give it a try and see if it is in the right direction for your needs.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim fn As String = "C:\Users\lesha\Desktop\ReadMe.rtf"
      Dim NombreArchivo As String = "C:\Users\lesha\Desktop\NombreArchivo.rtf"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rtb_chat.LoadFile(fn)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim TempRTB As New RichTextBox
        Dim obj As New DataObject
        If IO.File.Exists(NombreArchivo) Then
          TempRTB.LoadFile(NombreArchivo)
        End If
        TempRTB.AppendText(vbCrLf)
        obj.SetData(DataFormats.Rtf, rtb_chat.Rtf)
        Clipboard.SetDataObject(obj)
        TempRTB.SelectionStart = TempRTB.TextLength
        TempRTB.Paste()
        TempRTB.SaveFile(NombreArchivo)
        rtb_chat.Clear()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, August 13, 2019 8:51 PM
    • Marked as answer by CM16 Wednesday, August 14, 2019 5:45 PM
    Tuesday, August 13, 2019 8:50 PM

All replies

  • Hi

    I am not sure of this. Here is some code to try to illustrate.

    This code uses 2 rtf files - first one is ONLY a data source for this example. The second one would be the equivalent of your  NombreArchivo, and will accumulate everything from your rtb_chat. The accumulation is invoked by the Button Click.

    It only makes sense if the rtb_chat is cleared every time the Button is clicked otherwise the accumulated text in NombreArchivo would repeat everything each time.

    If you want to try this out, you will need to add a RTB called rtb_chat, and a Button1. For this example you will need a test RTF file called ReadMe.rtf (it really is only a few lines of RTF including images and formatted text). Then set the paths in the code to suit.

    When running, each time you click Button1, it will append the contents of rtb_chat to the NombreArchivo  file and clear rtb_chat ready for new text.images etc.

    Give it a try and see if it is in the right direction for your needs.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim fn As String = "C:\Users\lesha\Desktop\ReadMe.rtf"
      Dim NombreArchivo As String = "C:\Users\lesha\Desktop\NombreArchivo.rtf"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rtb_chat.LoadFile(fn)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim TempRTB As New RichTextBox
        Dim obj As New DataObject
        If IO.File.Exists(NombreArchivo) Then
          TempRTB.LoadFile(NombreArchivo)
        End If
        TempRTB.AppendText(vbCrLf)
        obj.SetData(DataFormats.Rtf, rtb_chat.Rtf)
        Clipboard.SetDataObject(obj)
        TempRTB.SelectionStart = TempRTB.TextLength
        TempRTB.Paste()
        TempRTB.SaveFile(NombreArchivo)
        rtb_chat.Clear()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, August 13, 2019 8:51 PM
    • Marked as answer by CM16 Wednesday, August 14, 2019 5:45 PM
    Tuesday, August 13, 2019 8:50 PM
  • Hi,

    use StreamWriter to write to rtf file

    Imports System.IO
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim CarpetaUsuario As String = "D:\Test"
            Dim NombreArchivo As String = CarpetaUsuario & "\" & Format(Now, "yyMMdd") & ".rtf"
    
            If Not Directory.Exists(CarpetaUsuario) Then
                My.Computer.FileSystem.CreateDirectory(CarpetaUsuario)
            End If
    
            Dim sw As StreamWriter = File.AppendText(NombreArchivo)
            sw.WriteLine(RichTextBox1.Text)
            sw.Close()
            MsgBox("Insert success!")
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 2:07 AM
  • Perfect! thanks for your help.
    Wednesday, August 14, 2019 5:45 PM