Needing help converting .doc to .txt

G

Guest

I have console application where i can read line by line from a .txt file and
save it to a different file name in .txt format as well.
Here's the basic code:
using System.IO namespace
Dim fs As New FileStream(reportPath, FileMode.Open, FileAccess.Read)
Dim fs1 As New FileStream(writePath, FileMode.Create, FileAccess.Write)
Dim oReader As New StreamReader(fs)
Dim oWrite As New StreamWriter(fs1)

Dim sLine As String = ""
Do
sLine = oReader.ReadLine()
oWrite.WriteLine(sLine)
Loop Until sLine Is Nothing
oReader.Close()
oWrite.Close()

Problem now, I have a .doc file that I want to convert and saveas .txt format
How can I do similar like the above only applies to .doc format into .txt
format
 
H

Herfried K. Wagner [MVP]

try2nd said:
Problem now, I have a .doc file that I want to convert and saveas .txt
format
How can I do similar like the above only applies to .doc format into .txt
format

Quick and dirty (Word must be installed on the machine):

\\\
Private Function GetWordDocumentText( _
ByVal FileName As String _
) As String
With CreateObject("Word.Application")
.Visible = False
.Documents.Open(FileName, , True)
.WordBasic.EditSelectAll()
.WordBasic.SetDocumentVar("MyVar", .WordBasic.Selection)
GetWordDocumentText = _
Replace(.WordBasic.GetDocumentVar("MyVar"), vbCr, vbNewLine)
.Documents.Close(0)
.Quit()
End With
End Function
///
 
L

Larry Lard

Crouchie1998 said:
Where is the return value in your function? Surely, it should just be
a sub

Herfried's function includes the line
vbNewLine)

In VB6 and earlier, the way to set the return value of a function was
to assign to a pseudo-variable with the name and type of the function
itself. This syntax is still allowed in VB.NET, although we now also
have the Return statement. Note that in this example, the older syntax
is clearer as we capture the required value at the appropriate place in
the flow of activity. If we were to rewrite using Return in the
misguided pursuit of 'up-to-date' code, we would have to introduce a
temporary variable.
 
C

Cor Ligthert

Larry,

No a Sub can be used everything is done inside the procedure.

However Hefried wrote "Quick and Dirty", so this is in my opinion a peanut.

(Normally you would pass the variable as well of course to such a routine or
even better pass an arraylist with variables).

But therefore it is quick and dirty.

Just my thought,

Cor
 
G

Guest

Would it be possible to do it without having Word installed?
Like using the Windows built-in for example, like WordPad
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
No a Sub can be used everything is done inside the procedure.

However Hefried wrote "Quick and Dirty", so this is in my opinion a
peanut.

Well, tha sample was originally written in VB6...
 
G

Guest

A 3 line solution I found using VB.NET BUT needing Word application as a part
of the codes is:

'reportPath and writePath are strings to the name of origin files and
destination files respectively
Imports Microsoft.Office.Interop

Dim WordApp As Word.Application = New Word.Application
Dim WordDoc As Word.Document =
WordApp.Documents.Open(fileName:=reportPath, readonly:=True)
WordDoc.SaveAs(writePath, Word.WdSaveFormat.wdFormatDOSText)
WordApp.Quit()

Problem is, I wanted to try to do the saveas/conversion without having Word
installed on computer.
Anyway around this? like using wordpad to do the same thing? but as far as I
know .Net doesn't have support such as that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top