Any one can help on how to delete a particular string from the contents in a document file

Recommended Answers

All 4 Replies

The easy way is to read all the document into a mulitline textbox, find the text, delete it and then write the text back. i.e.

Private Sub WriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteButton.Click
        My.Computer.FileSystem.WriteAllText("c:\Documents\test.txt", TextBox1.Text, False)
    End Sub

    Private Sub ReadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
        TextBox1.Text = My.Computer.FileSystem.ReadAllText("c:\Documents\test.txt")
    End Sub

If you want to automate it, you have to have a string with the text you want in it. Read the text into a variable, find the text, remove it and rewrite the file. i.e.

Dim FindText As String = "Find this string"
    Dim ThisText As String = ""

    Private Sub ReadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
        ThisText = My.Computer.FileSystem.ReadAllText("c:\Documents\test.txt")
        Dim pos As Integer = ThisText.IndexOf(FindText)
        ThisText = ThisText.Remove(pos, FindText.Length)
        My.Computer.FileSystem.WriteAllText("c:\Documents\test.txt", TextBox1.Text, False)
    End Sub

Great Idea...

but you r doin this for a text file...
i need it for a doc file..
im able to open the doc file...also read the contents into a multiline text box
now my need is to delete a particular string from it(its occurence could be multiple times) so i cant go to each location and delete it
what i want is to search d whol text in textbox,delete , save n wen i reopen d strings shudnt be present

If you are reading the file into a textbox then it is a text file with a doc extension. All you need to do is add a loop to the search & remove portion.
If you are really reading a Word doc file you will have to use Word to do the search and I can't help you there.

Dim FindText As String = "Find this string"
    Dim ThisText As String = ""

    Private Sub ReadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
        ThisText = My.Computer.FileSystem.ReadAllText("c:\Documents\test.doc")
        Dim pos As Integer = ThisText.IndexOf(FindText)
        Do While pos <> -1
            ThisText = ThisText.Remove(pos, FindText.Length)
            pos = ThisText.IndexOf(FindText)
        Loop
        My.Computer.FileSystem.WriteAllText("c:\Documents\test.doc", ThisText, False)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.