批量转换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的宏里面运行。

Syna 发表于 2010-5-23 类别: 「软件分享」 | 标签: , ,

若未注明,本博客文章均为原创,转载请以链接形式标明本文地址
本文地址:http://skday.com/archives/418/

10条留言 立即发表评论

  1. #7chairman China Google Chrome Windows @ 2011-9-6 16:19 回复

    麻烦把上两个留言删除吧,我本来把全部代码粘贴上来的,发现显示不了,就这一行改了一下,存成了unicodebigendian类型的及时本,否则音标保存不了。没有unicode类型,看了word 宏帮助。
    wordDoc.SaveAs destination(i), 7,,,,,,,,,,1201

    • Syna China Mozilla Firefox Windows @ 2011-9-6 16:24 回复

      解决问题了就好,呵呵~

      • chairman China Google Chrome Windows @ 2011-9-6 16:35 回复

        :razz:

  2. #6chairman China Google Chrome Windows @ 2011-9-6 11:15 回复

    请问您知道所有wordDoc.SaveAs destination(i), 7的参数列表吗?比如转成rtf的参数?谢谢

    • Syna China Mozilla Firefox Windows @ 2011-9-6 12:57 回复

      一般常用的有以下几个:
      1 doc
      6 rtf
      7 txt
      8 htm
      9 mht
      10 htm(单文件)
      11 xml

  3. #5淘宝啦 Mr.ing China Internet Explorer Unknow Os @ 2010-5-29 19:13 回复

    过来看看,好久没来了

  4. #4济南网站优化 China Internet Explorer Unknow Os @ 2010-5-25 18:29 回复

    博主写的不错,学习中!o(∩_∩)o

  5. #3真钱游戏 Philippines Internet Explorer Unknow Os @ 2010-5-25 14:01 回复

    不错的博客,支持!

  6. #2济南网站优化 China Internet Explorer Unknow Os @ 2010-5-25 10:14 回复

    顶!o(∩_∩)o

  7. #1b2b zone trade China Internet Explorer Unknow Os @ 2010-5-23 15:49 回复

    鉴定:此篇文章没有错别字。

评论


 ≮本博客已开启垃圾留言过滤,留言人工审核≯  在评论中显示个性头像>>

无觅相关文章插件,快速提升流量