只读自定义属性

问题描述:

我设法从VBA为MS Word添加自定义属性(元数据),但是如何使其只能读取,以至于无法轻松更改?只读自定义属性

你不能。

根据您试图避免的情况,您可能可以通过以某种方式加密内容来获得属性"obfuscate"。这会让用户难以弄清楚如何将它们改为有用的东西 - 但不会阻止用户“打破”它。

而不是使用文档属性使用文档变量http://msdn.microsoft.com/en-us/library/bb212231.aspx)。您只能通过代码访问它们。他们没有UI。

这里的一些老VB6/VBA功能我用他们:

Public Sub SetVariable(oDocument As Word.Document, sName As String, sValue As String) 

     Dim oVariable As Word.Variable 

     Set oVariable = LocateVariable(oDocument, sName) 

     If Not oVariable Is Nothing Then 

      oVariable.Value = sValue 

     Else 

      oDocument.Variables.Add sName, sValue 

     End If 

End Sub 

Public Function GetVariable(oDocument As Word.Document, sName As String) As String 

     Dim oVariable As Word.Variable 

     Set oVariable = LocateVariable(oDocument, sName) 

     If Not oVariable Is Nothing Then 

      GetVariable = oVariable.Value 

     Else 

      GetVariable = "" 

     End If 

End Function 

Public Function LocateVariable(oDocument As Word.Document, sName As String) As Word.Variable 

     Dim oVariable As Word.Variable 

     For Each oVariable In oDocument.Variables 

      If StrComp(oVariable.Name, sName, vbTextCompare) = 0 Then 

       Set LocateVariable = oVariable 

       Exit Function 

      End If 

     Next 

     Set LocateVariable = Nothing 

End Function