Read .rtf file and save as .txt
-
Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code
Dim word_app As New Microsoft.Office.Interop.Word.Application
Dim word_doc As New Microsoft.Office.Interop.Word.Documentword_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
word_doc.ActiveWindow.Selection.WholeStory()
word_doc.ActiveWindow.Selection.Copy()
str = My.Computer.Clipboard.GetData(GetType(String).ToString)
'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)doc.Close(False)
word_server.Quit()Dim sr As New IO.StreamWriter(out_file, False)
sr.Write(str)
sr.Close()this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:
-
Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code
Dim word_app As New Microsoft.Office.Interop.Word.Application
Dim word_doc As New Microsoft.Office.Interop.Word.Documentword_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
word_doc.ActiveWindow.Selection.WholeStory()
word_doc.ActiveWindow.Selection.Copy()
str = My.Computer.Clipboard.GetData(GetType(String).ToString)
'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)doc.Close(False)
word_server.Quit()Dim sr As New IO.StreamWriter(out_file, False)
sr.Write(str)
sr.Close()this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:
Hi, the line which reads
doc.Close(false)
should that be
word_doc.Close(false)
And also, you have an object named word_server, where is that declared?
-
Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code
Dim word_app As New Microsoft.Office.Interop.Word.Application
Dim word_doc As New Microsoft.Office.Interop.Word.Documentword_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
word_doc.ActiveWindow.Selection.WholeStory()
word_doc.ActiveWindow.Selection.Copy()
str = My.Computer.Clipboard.GetData(GetType(String).ToString)
'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)doc.Close(False)
word_server.Quit()Dim sr As New IO.StreamWriter(out_file, False)
sr.Write(str)
sr.Close()this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:
I suggest you forget about Word and experiment with a RichTextBox. You could set its Rtf property to load the RTF file content; then get its Text property. I haven't done this at any length, however the doc seems to suggest it would return the document content without any markup. BTW: IMO the RTB does not have to be visible, nor part of any Form for this. PS: As there are different levels of the RTF specification, it could happen that Word is more capable than an RTB, so complex documents might fail. I just don't know, I wish I did. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi, the line which reads
doc.Close(false)
should that be
word_doc.Close(false)
And also, you have an object named word_server, where is that declared?
hi, sorry i tried to make it more readable for you guys. i seemed to have mis-renamed the var on this snippet. thanks
-
Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code
Dim word_app As New Microsoft.Office.Interop.Word.Application
Dim word_doc As New Microsoft.Office.Interop.Word.Documentword_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
word_doc.ActiveWindow.Selection.WholeStory()
word_doc.ActiveWindow.Selection.Copy()
str = My.Computer.Clipboard.GetData(GetType(String).ToString)
'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)doc.Close(False)
word_server.Quit()Dim sr As New IO.StreamWriter(out_file, False)
sr.Write(str)
sr.Close()this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:
agreed, here is the function you can use..
Sub convertRTFtoText(ByVal in_file_path As String, ByVal out_file_path As String)
Dim rtb As New RichTextBox
rtb.LoadFile(in_file_path) 'this is your rtf file path
IO.File.WriteAllText(out_file_path, rtb.Text) 'this is your txt file path
End Sub -
agreed, here is the function you can use..
Sub convertRTFtoText(ByVal in_file_path As String, ByVal out_file_path As String)
Dim rtb As New RichTextBox
rtb.LoadFile(in_file_path) 'this is your rtf file path
IO.File.WriteAllText(out_file_path, rtb.Text) 'this is your txt file path
End Subthanks, i have used something similar.
Me.RichTextBox1.LoadFile(in_file)
Dim sr As New IO.StreamWriter(out\_file, False) sr.Write(Me.RichTextBox1.Text) sr.Close()
thanks guys! :)