Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Word To Text File 1

Status
Not open for further replies.

valdosta

MIS
Dec 18, 2002
50
US
Hi, How can I use VB to open a word documnet and save it as a text file? Thanks in advance!
 
You will need to add a reference to word (assuming you have this on your machine, otherwise it will become very complicated)

Then simply
'create a new instance of word, and a placeholder for the document
Dim wrdApp As New Word.Application
Dim wrdDoc As Word.Document

'open the document you want
Set wrdDoc = wrdApp.Documents.Open("D:\MyDoc.doc", True)
'save the document specifying the format
wrdDoc.SaveAs "c:\mydoc.txt", wdFormatText
'dont forget to tidy up
wrdDoc.Close

Hope this is ok.
 
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim filename as string

Set WordApp = CreateObject("Word.Application")
Set WordDoc = CreateObject("Word.Document")

Set WordDoc = WordApp.Documents.Add
WordApp.Documents.Open(FileName:=filename)
WordDoc.SaveAs (Filename:filename FileFormat:=wdFormatText)



Rob
 
hmckillop & rdavis: Thank you both very much for your help! I got "error 5154, You cannot Save a template file to non-template format". Maybe this word documnet is special. Please help. Thanks again!
 
What is the file that you are openning? Is it a template?



Rob
 
Rob: It's a data source file used to do mail merge. Basically I want to count how many records in the file --- I can do it for a text file. Is there any way to count how many lines or how many new lines in this file? Thanks!
 
Did you try saving it as a Document first, then save as a text file?



Rob
 
Yes, Rob. The same error message. But it worded when I moved this to another machine. I guess this has something to do with Word setting on this computer. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top