How can i batch convert a bunch of DOCX and DOC files into plain t

K

KC

Hi there, I would like to convert a bunch of DOCX and DOC files to text
format form the command line. Does MS Office have any such batch conversion
tools? If not, what are my options?

Thanks,
- KC.
 
G

Graham Mayor

You cannot do it from the command line without the aid of some third party
converter that can handle both formats, but you can from a Word macro. I
posted a version of the following yesterday for another user who wanted to
save a batch as HTML. This version will save as Windows Text format. There
are however other text formats, which may be better suited to your
requirements.

wdFormatDOSText Microsoft DOS text format.
wdFormatDOSTextLineBreaks Microsoft DOS text with line breaks preserved.
wdFormatEncodedText Encoded text format.
wdFormatText Microsoft Windows text format.
wdFormatTextLineBreaks Windows text format with line breaks
preserved.
wdFormatUnicodeText Unicode text format.

Replace as necessary for FileFormat:=wdFormatText
http://www.gmayor.com/installing_macro.htm

Sub SaveAllAsTXT()
Dim strFileName As String
Dim strDocName() As String
Dim strPath As String
Dim oDoc As Document
Dim Response As Long
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", , "Save all as Text"
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
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
strDocName = Split(ActiveDocument.FullName, ".")
If ActiveDocument.SaveFormat = 0 Or _
ActiveDocument.SaveFormat = 12 Then
ActiveDocument.SaveAs _
FileName:=strDocName(0) & ".txt", _
FileFormat:=wdFormatText
End If
oDoc.Close SaveChanges:=wdDoNotSaveChanges
strFileName = Dir$()
Wend
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Steve Yandl

KC,

You can do something similar to what Graham Mayor describes with a vbScript
file rather than VBA from Word. As a rule, it makes more sense to do what
Graham described. The one situation where I'd consider the vbScript
approach would be if you wanted to be able to use 'SendTo' from the Windows
Explorer context menu to send a folder for processing (or drag and drop into
a shortcut to the scriipt) or you wanted to be able to type in the name of
the folder as part of the command line and have all the docx and doc files
processed. In those cases, you would be able to take advantage of the
Wscript arguments collection through script.

Is there a particular reason why you wanted to be able to do the conversion
from a command line?


Steve Yandl
 

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