如何用VBA移除Word XML中的RSID信息并批量转换Doc为Docx
批量转.doc为.docx并移除RSID标签的解决方案
VBA 实现方案
直接在Word中运行以下宏脚本,可批量处理指定文件夹下的所有.doc文件,同时清除RSID标签:
Sub BatchConvertDocToDocxRemoveRSID() Dim sourceFolder As String Dim targetFolder As String Dim docFile As String Dim doc As Document ' 设置源文件夹和目标文件夹(自行修改路径) sourceFolder = "C:\YourSourceFolder\" targetFolder = "C:\YourTargetFolder\" ' 确保目标文件夹存在 If Dir(targetFolder, vbDirectory) = "" Then MkDir targetFolder End If docFile = Dir(sourceFolder & "*.doc") Do While docFile <> "" ' 跳过已转换的docx文件 If Right(docFile, 4) = ".doc" Then Set doc = Documents.Open(sourceFolder & docFile) On Error Resume Next ' 接受所有修订 If doc.Revisions.Count > 0 Then doc.Revisions.AcceptAll End If ' 关闭修订追踪 doc.TrackRevisions = False ' 禁用保存时存储RSID doc.StoreRSIDOnSave = False ' 清除现有RSID(关键步骤) doc.RemovePersonalInformation = True doc.Save ' 另存为docx格式 doc.SaveAs2 _ FileName:=targetFolder & Left(docFile, Len(docFile) - 4) & ".docx", _ FileFormat:=wdFormatXMLDocument doc.Close SaveChanges:=wdDoNotSaveChanges On Error GoTo 0 End If docFile = Dir Loop MsgBox "转换完成!" End Sub
注意事项
- 替换代码中的
sourceFolder和targetFolder为实际路径 - 运行前关闭所有Word文档,避免冲突
- 若遇到文档打开报错,检查文件是否被锁定或损坏
Python 实现方案
使用pywin32库调用Word自动化服务,逻辑和VBA一致,适合熟悉Python的用户:
首先安装依赖:
pip install pywin32
然后运行以下脚本:
import os import win32com.client as win32 from win32com.client import constants def batch_convert_doc_to_docx(source_dir, target_dir): # 创建目标文件夹 if not os.path.exists(target_dir): os.makedirs(target_dir) # 初始化Word应用 word = win32.gencache.EnsureDispatch('Word.Application') word.Visible = False # 后台运行,不显示Word窗口 for filename in os.listdir(source_dir): if filename.lower().endswith('.doc') and not filename.lower().endswith('.docx'): doc_path = os.path.join(source_dir, filename) doc = word.Documents.Open(doc_path) # 接受所有修订 if doc.Revisions.Count > 0: doc.Revisions.AcceptAll() # 关闭修订追踪 doc.TrackRevisions = False # 禁用RSID存储 doc.StoreRSIDOnSave = False # 清除个人信息及RSID doc.RemovePersonalInformation = True doc.Save() # 另存为docx target_path = os.path.join(target_dir, os.path.splitext(filename)[0] + '.docx') doc.SaveAs2(FileName=target_path, FileFormat=constants.wdFormatXMLDocument) doc.Close(SaveChanges=False) word.Quit() print("转换完成!") # 替换为实际路径 source_directory = r"C:\YourSourceFolder" target_directory = r"C:\YourTargetFolder" batch_convert_doc_to_docx(source_directory, target_directory)
注意事项
- 确保系统已安装Word软件
- 脚本运行时不要手动操作Word,避免干扰
- 若出现权限问题,以管理员身份运行Python脚本
内容的提问来源于stack exchange,提问作者user2355903
相关产品推荐
相关产品推荐

