![](/img/trans.png)
[英]Closing application (Acrobat, Word, Excel) after closing the last document
[英]How to make a word document readonly in runtime after opening without closing it before
是否可以在以 ReadWrite 模式打开 Word 文档后在运行时将其设为 ReadOnly 而无需先关闭它?
这是我尝试做的一个例子:
Sub ProcessWordDoc()
Dim WordApp As New Word.Application
Dim Doc As Word.Document
WordApp.Visible = True
Set Doc = WordApp.Documents.Open(Filename:="C:\myWordTemplate.docx", ReadOnly:=False)
With Doc
.Unprotect
.FormFields("Firstname").Result = Me.Firstname
.FormFields("Lastname").Result = Me.Lastname
.Protect wdAllowOnlyReading
End With
' Make Doc ReadOnly after the document has been processed.
' Somthing like, but it does not work, since the ReadOnly property is read-only ;-)
Doc.ReadOnly = True
End Sub
简而言之:是否可以通过 VBA 将 word 文档的模式从 ReadWrite 切换为 ReadOnly?
感谢您的帮助。
扩展我的评论/替代想法。
这将(应该)从您的模板文件创建一个新文档,在插入数据后对其进行保护,然后“保存”该文档,以便可以在不提示保存的情况下关闭它,因此不会保存新文档并且您的模板永远不会编辑。
Sub Main()
Dim WordApp As New Word.Application
Dim Doc As Word.Document
WordApp.Visible = True
Set Doc = WordApp.Documents.Add(Template:="C:\myWordTemplate.docx")
With Doc
.Unprotect
.FormFields("Firstname").Result = Me.Firstname
.FormFields("Lastname").Result = Me.Lastname
.Protect wdAllowOnlyReading
.Saved = True
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.