How to Convert a DOC to a TXT in VB
Visual Basic, by itself, does not contain the necessary tools to work with the contents of Microsoft Word's "DOC" file format. However, if a copy of Microsoft Word is installed on the system along with Visual Basic, then Visual Basic will automatically be extended in this fashion as well.
Instructions
-
-
1
Create a new project in Visual Basic. Select "Console Application" when prompted for the application type. Later, you can use this same code in your own graphical user interface application, but selecting Console allows the tutorial to skip straight to the code that matters.
-
2
Paste the following on the very first line of the project (above the "Module Module1" line):
Imports Microsoft.Office.Interop
If Microsoft Word is not installed on your system, then the command will be underlined in green to inform you of that fact, and you cannot continue the tutorial.
-
-
3
Paste the following within the "Main" subroutine:
Dim wApp = New Word.Application
Dim wDoc = wApp.Documents.Open(fileName:="file.doc", readonly=True)
wDoc.SaveAs("file.txt", Word.WdSaveFormat.wdFormatDOSText)
wApp.Quit()
This opens a copy of Microsoft Word in the background, opens the document "file.doc" as read-only (a safety feature to prevent unintentional alterations to the document), and calls Word's "Save As" command to save it as a DOS text file named "file.txt." Finally, Microsoft Word is automatically closed.
-
1
Tips & Warnings
If this does not work, but you have Microsoft Office installed on your system, consult the resources at the bottom of this article for help on installing the "Word Primary Interop Assembly," the libraries that allow Visual Basic to interact directly with Microsoft Word.