Word文档神秘地写保护?

时间:2015-01-30 15:58:54

标签: vbscript ms-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脚本宿主收到一个错误,说文件是写保护的,即使我在文件资源管理器中打开属性对话框时没有显示为写保护。如果单击“保存”,系统会提示我保存所有其他文件。这里有什么问题?

我怀疑它是由Word文档很老化引起的,就像20世纪90年代一样。

1 个答案:

答案 0 :(得分:1)

  

设置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的只读,与文件无关。