只读自定义属性

时间:2009-07-28 11:03:03

标签: vba ms-word

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

2 个答案:

答案 0 :(得分:2)

你不能。

根据您尝试避免的场景类型,您可以通过以某种方式加密内容来"obfuscate"属性。这将使用户更难以弄清楚如何将它们更改为有用的东西 - 但不会阻止用户“破坏”它。

答案 1 :(得分:0)

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

这是我用过的一些旧的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