Word文档神秘写保护?
Word document mysteriously write protected?
我正在尝试对文件夹中的多个 Word 文档执行查找和替换操作。我编写了以下 VBScript 来做到这一点:
Option Explicit
Dim Word, Document, FolderPath, FileSystem, FileList, File, Doc, InfoString
Const ReadOnly = 1
Const wdFindContinue = 1
Const wdReplaceAll = 2
Const wdOriginalDocumentFormat = 1
Set FileSystem = CreateObject("Scripting.FileSystemObject")
FolderPath = FileSystem.GetAbsolutePathName(".")
Set FileList = FileSystem.GetFolder(FolderPath).files
Set Word = CreateObject("Word.Application")
Word.Visible = False
Word.DisplayAlerts = False
For Each File in FileList
If LCase(Right(File.Name,3)) = "doc" Or LCase(Right(File.Name,4)) = "docx" Then
If File.Attributes And ReadOnly Then
File.Attributes = File.Attributes - ReadOnly
End If
Set Doc = Word.Documents.Open(File.Path,,True)
' find and replace stuff
End If
Next
Word.Documents.Save True, wdOriginalDocumentFormat
Word.Quit
MsgBox("Done")
问题是,当它到达第Word.Documents.Save
行时,总是弹出另存为对话框。如果我单击“取消”,我会从 Windows Script Host 收到一条错误消息,指出该文件已被写保护,即使我在文件资源管理器中打开“属性”对话框时它并未显示为写保护。如果我点击保存,系统会提示我也保存所有其他文件。这里有什么问题?
我怀疑这是因为 Word 文档太旧了,好像是 1990 年代的。
Set Doc = Word.Documents.Open(File.Path,,True)
并查看对象浏览器中的文档。
Function Open(FileName, [ConfirmConversions], [ReadOnly], [AddToRecentFiles], [PasswordDocument], [PasswordTemplate], [Revert], [WritePasswordDocument], [WritePasswordTemplate], [Format], [Encoding], [Visible], [OpenAndRepair], [DocumentDirection], [NoEncodingDialog]) As Document
Member of Word.Documents
所以 True 表示打开只读。这是Word只读文件,与文件无关
我正在尝试对文件夹中的多个 Word 文档执行查找和替换操作。我编写了以下 VBScript 来做到这一点:
Option Explicit
Dim Word, Document, FolderPath, FileSystem, FileList, File, Doc, InfoString
Const ReadOnly = 1
Const wdFindContinue = 1
Const wdReplaceAll = 2
Const wdOriginalDocumentFormat = 1
Set FileSystem = CreateObject("Scripting.FileSystemObject")
FolderPath = FileSystem.GetAbsolutePathName(".")
Set FileList = FileSystem.GetFolder(FolderPath).files
Set Word = CreateObject("Word.Application")
Word.Visible = False
Word.DisplayAlerts = False
For Each File in FileList
If LCase(Right(File.Name,3)) = "doc" Or LCase(Right(File.Name,4)) = "docx" Then
If File.Attributes And ReadOnly Then
File.Attributes = File.Attributes - ReadOnly
End If
Set Doc = Word.Documents.Open(File.Path,,True)
' find and replace stuff
End If
Next
Word.Documents.Save True, wdOriginalDocumentFormat
Word.Quit
MsgBox("Done")
问题是,当它到达第Word.Documents.Save
行时,总是弹出另存为对话框。如果我单击“取消”,我会从 Windows Script Host 收到一条错误消息,指出该文件已被写保护,即使我在文件资源管理器中打开“属性”对话框时它并未显示为写保护。如果我点击保存,系统会提示我也保存所有其他文件。这里有什么问题?
我怀疑这是因为 Word 文档太旧了,好像是 1990 年代的。
Set Doc = Word.Documents.Open(File.Path,,True)
并查看对象浏览器中的文档。
Function Open(FileName, [ConfirmConversions], [ReadOnly], [AddToRecentFiles], [PasswordDocument], [PasswordTemplate], [Revert], [WritePasswordDocument], [WritePasswordTemplate], [Format], [Encoding], [Visible], [OpenAndRepair], [DocumentDirection], [NoEncodingDialog]) As Document
Member of Word.Documents
所以 True 表示打开只读。这是Word只读文件,与文件无关