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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting all Word Documents to Plain Text(.txt) format in a "Specific" folder

Status
Not open for further replies.

fsdcoder

Programmer
Mar 2, 2017
1
CA
Hey,

So, I was able to convert one word document to .txt format (See Code below).
Code:
Dim objWord, objDoc

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\VBS\Testing.docx")
objDoc.SaveAs "C:\VBS\Testing.txt", 2
objDoc.Close
objWord.Quit

Set objDoc = Nothing
Set objWord = Nothing

msgbox "Word Document Converted to Text."

Now, I don't want to specify the Exact name of the word document. I would like the script to identify the word document in a Particular Folder and then convert it to .txt
Here is What I have so far:

Code:
Dim objWord, objDoc, objFSO, objFolder, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
Set objFolder = objFSO.GetFolder("C:\VBS\")

For Each objFile In objFolder.Files

Set objDoc = objWord.Documents.Open("C:\VBS\")
objDoc.SaveAs "C:\VBS\", 2
objDoc.Close
objWord.Quit

Set objDoc = Nothing
Set objWord = Nothing
Next

msgbox "Word Document Converted to Text."

Any help will be greatly appreciated.
 
Set objDoc = objWord.Documents.Open(objFile)



Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Wouldn't it be more like:

For Each objFile In objFolder.Files
Set objDoc = objWord.Documents.Open("C:\VBS\"& objFile.Name & "." & objFile.Extension)​
objDoc.SaveAs "C:\VBS\" & objFile.Name & ".txt", 2​
objDoc.Close​
objWord.Quit​
Set objDoc = Nothing​
Set objWord = Nothing​
Next

I can't recall off the top of my head whether objFile.Name returns just the file name (ie: C:\VBS\testing.docx would return "testing") or the file name with file extension (ie: C:\VBS\testing.docx would return "testing.docx") or even the full path with filename and extension, as in "C:\VBS\testing.docx".

You could always comment out all the lines in your For...Next loop and add these lines inside the loop...

WScript.Echo objFile
WScript.Echo objFile.Name
WScript.Echo objFile.Extension

...just to see what you get. Then you'd know which syntax to use for sure.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top