只读自定义属性

发布于 2024-07-30 00:58:33 字数 55 浏览 1 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-08-06 00:58:33

使用文档变量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

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
栀子花开つ 2024-08-06 00:58:33

你不能。

根据您想要避免的情况,您也许可以“混淆” 通过以某种方式加密内容来获取属性。 这将使用户更难弄清楚如何将它们更改为有用的东西 - 但不会阻止用户“破坏”它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文