2009-07-28 246 views
0

我設法從VBA爲MS Word添加自定義屬性(元數據),但是如何使其只能讀取,以至於無法輕鬆更改?只讀自定義屬性

回答

2

你不能。

根據您試圖避免的情況,您可能可以通過以某種方式加密內容來獲得屬性"obfuscate"。這會讓用戶難以弄清楚如何將它們改爲有用的東西 - 但不會阻止用戶「打破」它。

0

而不是使用文檔屬性使用文檔變量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