批量转换Word为txt或其他格式
本篇文章将会使用VBS的方法,将Office Word支持的所有格式:doc、[docx]、txt、htm、rtf等,批量互相转换。首先请确定你的电脑上安装了Word软件。
如果要将“D:\我的doc”文件夹下的所有doc转换成txt。打开记事本,复制以下代码:
Dim array1(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Dim source(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Dim destination(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("D:\我的doc\") '②此处表示doc所在的文件夹,请根据需要自行修改
Set fc = f.Files
i = 0
For Each f1 In fc
array1(i) = f1.Name
Ext = fs.GetExtensionName(array1(i))
Ext = LCase(Ext)
If Ext = "doc" Then '③此处表示转换类型
source(i) = f & "\" & array1(i)
destination(i) = Left(source(i), Len(source(i)) - 4) & ".txt"
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(source(i))
wordDoc.SaveAs destination(i), 7
wordDoc.Close
Set wordDoc = Nothing
wordApp.Quit
Set wordApp = Nothing
End If
i = i + 1
Next
然后保存为format.vbs(一定要保证扩展名为vbs),保存位置任意。然后双击format.vbs运行即可。
注意:运行以后请等待一段时间直到全部文档转换完毕,整个操作过程中不要进行打开Word操作。如果希望中途停止,在任务管理器里结束WScript.exe进程即可。
如果要将“D:\我的doc”文件夹下的所有rtf转换成htm:
Dim array1(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Dim source(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Dim destination(100) '①100表示该文件夹最多的文件数,如果文件数大于100请自行修改
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("D:\我的doc\") '②此处表示doc所在的文件夹,请根据需要自行修改
Set fc = f.Files
i = 0
For Each f1 In fc
array1(i) = f1.Name
Ext = fs.GetExtensionName(array1(i))
Ext = LCase(Ext)
If Ext = "rtf" Then '③此处表示转换类型
source(i) = f & "\" & array1(i)
destination(i) = Left(source(i), Len(source(i)) - 4) & ".htm"
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(source(i))
wordDoc.SaveAs destination(i), 8
wordDoc.Close
Set wordDoc = Nothing
wordApp.Quit
Set wordApp = Nothing
End If
i = i + 1
Next
其他的大家举一反三吧。另外,以上代码也可以在Word的宏里面运行。
若未注明,本博客文章均为原创,转载请以链接形式标明本文地址
本文地址:http://skday.com/archives/418/


10条留言 立即发表评论
麻烦把上两个留言删除吧,我本来把全部代码粘贴上来的,发现显示不了,就这一行改了一下,存成了unicodebigendian类型的及时本,否则音标保存不了。没有unicode类型,看了word 宏帮助。
wordDoc.SaveAs destination(i), 7,,,,,,,,,,1201
解决问题了就好,呵呵~
请问您知道所有wordDoc.SaveAs destination(i), 7的参数列表吗?比如转成rtf的参数?谢谢
一般常用的有以下几个:
1 doc
6 rtf
7 txt
8 htm
9 mht
10 htm(单文件)
11 xml
过来看看,好久没来了
博主写的不错,学习中!o(∩_∩)o
不错的博客,支持!
顶!o(∩_∩)o
鉴定:此篇文章没有错别字。