Doc批量转成Docx

Doc批量转成Docx

在工作中遇到需要将word文档中的doc转换成docx的需求,一共有大几百个文件,这种就不太可能一个个去转换了,文件太多效率太低了。

VBA环境

经过一顿查找之后确定使用Office的VBA(Microsoft Visual Basic for Applications)去做相应转换,它是Office自带的,一般不需要额外安装,转换之后兼容性也比较好。VBA环境开启步骤:

  1. 打开任意一个word文档,Office版本在这里插入图片描述
  2. 按下Alt+F11快捷键即可看到VBA的编译环境在这里插入图片描述

语法

这里需要一定的编程功底,如果不熟悉VBA的语法,需要一边学习、一边实现业务功能,下面给出相关资料:

转换程序

弹窗选择源路径

运行脚本,通过弹窗选择需要转换文件的根路径:

Sub ConvertDocToDocx()
'Updated by ExtendOffice 20181128
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim xFileName As String
    Application.ScreenUpdating = False
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    xFolder = xDlg.SelectedItems(1) + "\"
    xFileName = Dir(xFolder & "*.doc", vbNormal)
    While xFileName <> ""
        Documents.Open FileName:=xFolder & xFileName, _
            ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
            wdOpenFormatAuto, XMLTransform:=""
        ActiveDocument.SaveAs xFolder & Replace(xFileName, "doc", "docx"), wdFormatDocumentDefault
        ActiveDocument.Close
        xFileName = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub

存在的问题:

  1. 生成的docx文件与原有的doc文件混在一起
  2. 生成的第一个docx还被再次被执行一次转换,逻辑上存在bug

完整脚本

实现了一下的几个功能,在此之前需要添加引用:Microsoft Scripting Runtime在这里插入图片描述

  1. 生成的docx与原有的doc分开,存在一个同级的文件夹中
  2. 实现了文件夹的循环遍历,根目录下的所有doc都会被找出来执行转换
Attribute VB_Name = "模块2"
Sub main()
    Dim fso As New FileSystemObject  '定义一个文件系统对象
    Dim fld As Folder
    Dim xDlg As FileDialog
    Dim xDirNam As String
    
    Application.ScreenUpdating = False
    
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    
    xDirName = xDlg.SelectedItems(1)
    If fso.FolderExists(xDirName) Then        '判断文件是否存在
        Set fld = fso.GetFolder(xDirName)
        ScanDirs fld         '调用函数
    Else
        MsgBox "文件夹不存在"
    End If
    MsgBox "转换完成"
    
    Application.ScreenUpdating = True
End Sub

Sub ScanDirs(fld As Folder)
    '递归遍历文件夹
    Dim fil As File, outFld As Folder    '定义一个文件夹和文件变量
    Set subfiles = fld.Files()     '获取文件夹下所有文件
    Set SubFolders = fld.SubFolders      '获取文件夹下所有文件夹
    
    
    ConvertDocToDocx fld.Path    '检查根目录是否有需要转换的
    For Each outFld In SubFolders    '遍历文件夹
        ConvertDocToDocx outFld.Path
        ScanDirs outFld      '调用函数自身
    Next
End Sub

Sub ConvertDocToDocx(xDirName As String)
    'doc转换成docx
    Dim xFolder As Variant
    Dim xSaveFolder As Variant
    Dim xFileName As String
    
    xFolder = xDirName + "\"
    xSaveFolder = xDirName + "_docx\"
    If Dir(xFolder) <> "" And Dir(xSaveFolder) = "" Then MkDir xSaveFolder '判断文件夹是否存在,不存在则创建。
        
    xFileName = Dir(xFolder & "*.doc", vbNormal)
    While xFileName <> ""
        Documents.Open FileName:=xFolder & xFileName, _
        ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
        WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
        wdOpenFormatAuto, XMLTransform:=""
        
        ActiveDocument.SaveAs xSaveFolder & Replace(xFileName, "doc", "docx"), wdFormatDocumentDefault
        ActiveDocument.Close
        xFileName = Dir()
    Wend
End Sub

Word转换脚本(Doc批量转Docx),可以直接导入这个脚本运行,无需复制黏贴了。

一次选中单个、多个指定的doc文件转换

Attribute VB_Name = "模块5"
Sub doc2docx()  'doc文件转docx文件
Dim myDialog As FileDialog, oFile As Variant
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
With myDialog
        .Filters.Clear    '清除所有文件筛选器中的项目
        .Filters.Add "所有 WORD97-2003 文件", "*.doc", 1    '增加筛选器的项目为所有WORD97-2003文件
        .AllowMultiSelect = True    '允许多项选择
        If .Show = -1 Then    '确定
            For Each oFile In .SelectedItems    '在所有选取项目中循环
                With Documents.Open(oFile)
                .SaveAs FileName:=Replace(oFile, "doc", "docx"), FileFormat:=12
                .Close
                End With
            Next
        End If
End With
End Sub

Word转换脚本(Doc可多选),可以直接导入这个脚本运行,无需复制黏贴了。

后记

VBA还支持转txt、pdf、xls等不同类型文件的读取、解析等一些列操作,这些需要对VBA比较熟悉。

参考资料:

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值