gpt4 book ai didi

vba - 将多个 MS Word 文档转换为 txt 文件(少量额外位)

转载 作者:行者123 更新时间:2023-12-05 05:17:56 34 4
gpt4 key购买 nike

我目前正致力于将大量 MS Word 表单传输到数据库系统中。

目前我的做法是:

  1. 打开单个 word 文档
  2. 转到高级选项更改保存设置,以便仅将表单数据保存为带分隔符的文本文件
  3. 保存点击确定弹出提示窗口
  4. 使用cmd合并所有txt文件
  5. 导入 excel 并在那里处理

希望在这些阶段结束时,我将获得一个经过相当整理的 excel 信息文件,可以将其传输到数据库系统。

我的问题是,由于我有很多word文档(大约100个后我感觉自己像个机器人),我可以将1)2)和3)的过程自动化吗?

任何帮助将不胜感激,我之前用 python 编写过脚本并完成了一些简单的编程,但欢迎任何解决方案。

最佳答案

您可以使用 VBA 宏自动执行步骤 1-3。使用 Document.SaveAs 方法,您可以将表单数据仅保存到 txt 文件中:

ActiveDocument.SaveAs ActiveDocument.FullName & ".txt", _
WdSaveFormat.wdFormatText, SaveFormsData:=True

然后您可以调用此SaveAs 方法循环遍历给定文件夹中的所有文档:

Sub SaveAllFormData(path As String)
Dim doc As Document
Dim fileName As String

fileName = Dir(path & "*.doc")

' Loop through all .doc files in that path
Do While fileName <> ""
Set doc = Application.Documents.Open(path & fileName)

' Save form data
doc.SaveAs2 doc.FullName & ".txt", WdSaveFormat.wdFormatText, SaveFormsData:=True

doc.Close wdDoNotSaveChanges
fileName = Dir
Loop
End Sub

如果您在设置和运行宏方面需要帮助,请查看文档:Create or run a macro

关于vba - 将多个 MS Word 文档转换为 txt 文件(少量额外位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559646/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com