DOC to TXT

M

Mike Berger

Hello Newsgroup!

I have a foldertree with 3 underfolders, that include about 500 .doc
documents.

I will open the .doc automatily and then saving as .txt and if possible
delete the .doc file from the disc.

I can open one file with VBA, when I have the filename in the code.

It's possible to make that with VBA?

Thank you.
Mike
 
G

Graham Mayor

The following macro will convert all the doc files in a given folder to TXT

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

It would of course have helped had I included the macro :eek:(

Sub SaveAllAsTXT()
Dim strFileName As String
Dim strDocName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)

strDocName = ActiveDocument.FullName
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".txt"
oDoc.SaveAs FileName:=strDocName, _
FileFormat:=wdFormatText
oDoc.Close SaveChanges:=wdDoNotSaveChanges
strFileName = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Mike,

i'm assuming my answer in the german groups was a bit too complicated.
The easiest way do get all *.doc documents' folders and names is that, I
think:
In the cmd-shell, navigate to your start folder,
lets say c:\test.
There you enter:
dir *.doc /b/s > c:\testdir.txt
This will create a text-file,
containing all fullnames of all docs in the actual folder and all subfolders.
You may check how german characters like ß,ü,ä,ö are displayed,
using word or any editor.

Then it goes like this:
Dim MyDoc as document
Dim stmp as string
Dim sDoc as string
Open "c:\Testdir.txt" For Input As #1
While Not EOF(1)
Line Input #1, stmp ' Read line of data.
Set mydoc = Documents.Add(stmp, Visible:=false)
sDos = Left(stmp, Len(stmp) - 3) & "txt"
mydoc.SaveAs sDos, FileFormat:=wdFormatDOSText
mydoc.Close
' Kill stmp ' careful
Wend
Close #1

Note, that you may not be allowed to write to c:\,
so you may use another directory for the output of dir *.doc /b/s >.
Beware of typos.
I can't test the code right now.
 
M

Mike Berger

Hello Graham,

thank you very much for your help.
Your program code is going very good.

Mike
 
M

Mike Berger

Hello Helmut,

thank you very much for your help.
Your program code is going very good.
The text file I have to change the ÄÖÜ, but then are no problem

Mike
 
K

Kyle.Zhang

You can use FileSystemObject to browse files and folders in your specified
folder, and use it to delete the document.

Dim doc As Word.Document
Dim fso, folder, subFolder, docFile
Dim sPath As String

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("E:\Training\docs")

For Each subFolder In folder.SubFolders
For Each docFile In subFolder.Files
If UCase(Right(docFile.Name, 4)) = ".DOC" Then
sPath = docFile.Path
Set doc = Documents.Open(sPath, , , False, , , , , , , ,
False)
sPath = docFile.Path & ".txt"
doc.SaveAs sPath, Word.wdFormatText
doc.Close False
docFile.Delete
End If
Next
Next
 

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