tags:

views:

162

answers:

2

I managed to add custom properties (metadata) for MS Word from VBA, but how do I make it read only so that it cannot be changed easily?

+1  A: 

You can't.

Depending on what sort of scenario you're trying to avoid, you might be able to "obfuscate" the properties by encrypting the contents somehow. That would make it harder for a user to figure out how to change them to something useful - but wouldn't stop the user from "breaking" it.

Gary McGill
A: 

Instead of using document properties use document variables (http://msdn.microsoft.com/en-us/library/bb212231.aspx). You can only access them through code. There's no UI for them.

Here's some old VB6/VBA functions I used for them:

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
Tom Winter