ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: zzpsx

txt文档批量转换成word文档

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-10 12:54 | 显示全部楼层
liulang0808 发表于 2024-3-10 12:06
具体模拟的结果在那里?

image.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-10 13:42 | 显示全部楼层
本帖最后由 zzpsx 于 2024-3-11 18:39 编辑

以下是我用ai写的代码,运行有点慢,单词统计个数不正确,还需要修改,需要删除冗余代码。为何这个代码在win 7中可以使用,在win 10或11中不能用,会导致模板相关的问题。




Sub ConvertTxtToWordAndCountEnglishLinesAndWords()
Rem 亲测可用,2024年3月10日
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim objFile As Object
    Dim txtFileName As String
    Dim docFileName As String
    Dim doc As Document
    Dim wordApp As Object
    Dim englishLineCount As Long
    Dim englishWordCount As Long
    Dim paragraph As paragraph
    Dim word As String
    Dim i As Long
   
    ' 创建文件系统对象
    Set objFSO = CreateObject("Scripting.FileSystemObject")
   
    ' 设置当前文件夹
    Set objFolder = objFSO.GetFolder(Application.ActiveDocument.Path)
   
    ' 遍历子文件夹
    For Each objSubFolder In objFolder.SubFolders
        ' 遍历子文件夹中的文件
        For Each objFile In objSubFolder.Files
            ' 检查文件扩展名是否为 txt
            If objFSO.GetExtensionName(objFile.Name) = "txt" Then
                ' 设置 txt 文件名和 Word 文件名
                txtFileName = objFile.Path
                docFileName = objFSO.BuildPath(objSubFolder.Path, objFSO.GetBaseName(objFile.Name) & ".docx")
               
                ' 创建 Word 应用程序对象
                Set wordApp = CreateObject("Word.Application")
               
                ' 打开 txt 文件并另存为 Word 文件
                wordApp.Documents.Open txtFileName
                wordApp.ActiveDocument.SaveAs docFileName, 12 ' 12 表示 wdFormatXMLDocument
                wordApp.ActiveDocument.Close
               
                ' 关闭 Word 应用程序
                wordApp.Quit
               
                ' 清理对象
                Set wordApp = Nothing
            End If
        Next objFile
    Next objSubFolder
   
    ' 重新设置当前文件夹
    Set objFolder = objFSO.GetFolder(Application.ActiveDocument.Path)
   
    ' 遍历子文件夹
    For Each objSubFolder In objFolder.SubFolders
        ' 遍历子文件夹中的文件
        For Each objFile In objSubFolder.Files
            ' 检查文件扩展名是否为 docx 或 doc
            If objFSO.GetExtensionName(objFile.Name) = "docx" Or objFSO.GetExtensionName(objFile.Name) = "doc" Then
                ' 创建 Word 应用程序对象
                Set wordApp = CreateObject("Word.Application")
               
                ' 打开 Word 文档
                Set doc = wordApp.Documents.Open(objFile.Path)
               
                ' 初始化计数器
                englishLineCount = 0
                englishWordCount = 0
               
                ' 遍历文档中的段落
                For Each paragraph In doc.Paragraphs
                    ' 检查段落是否包含英文字符
                    If paragraph.Range.Text Like "*[!a-zA-Z]*" Then
                        ' 计算英文行数
                        englishLineCount = englishLineCount + 1
                        
                        ' 计算英文单词数
                        For i = 1 To paragraph.Range.words.count
                            word = paragraph.Range.words(i).Text
                            If word Like "*[!a-zA-Z]*" Then
                                englishWordCount = englishWordCount + 1
                            End If
                        Next i
                    End If
                Next paragraph
               
                ' 在文档末尾添加统计结果
                doc.Range.InsertAfter "朗读包含 " & englishLineCount & " 行英文," & englishWordCount & " 个单词。"
               
                ' 保存并关闭文档
                doc.Save
                doc.Close
               
                ' 关闭 Word 应用程序
                wordApp.Quit
               
                ' 清理对象
                Set wordApp = Nothing
                Set doc = Nothing
            End If
        Next objFile
    Next objSubFolder
   
    ' 清理对象
    Set objFSO = Nothing
    Set objFolder = Nothing
    Set objSubFolder = Nothing
    Set objFile = Nothing
   
    MsgBox "已成功统计所有 Word 文档中的英文行数和英文单词数!"
End Sub



TA的精华主题

TA的得分主题

发表于 2024-3-10 14:01 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-11 08:49 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我修改了12楼的代码,请各位高手斧正!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 20:26 , Processed in 0.034207 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表