【问题标题】:VB 2013, modify word document property/custom propertyVB 2013,修改word文档属性/自定义属性
【发布时间】:2015-12-02 19:18:10
【问题描述】:

我希望制作一个插件来更新 Word 文档属性。我已经准备好使用插件/功能区/等,因为我还有其他功能区功能正在运行。我使用 MSVS 向导创建了 word 功能区项目。

我不知道该怎么做;访问活动的 Word 文档,并访问属性/自定义属性。我无法弄清楚;声明、调用、库等。我无法使任何 MSDN 示例工作……。我完全错过了一些东西。

例如:“ActiveDocument.CustomDocumentProperties”不起作用。

免责声明 - 我不是程序员。这一切都与 vba 一起工作,我正在尝试将其移植到 vb。我还在阅读发布的帮助,并尝试示例。

任何建议将不胜感激。亲切的问候,

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    我明白了。

    模块外声明:

    Imports moDoc = Microsoft.Office.Interop.Word
    

    在子内 - 这将打开的应用程序与一个对象相关联:

    Dim oActiveApp As moDoc.Application
    oActiveApp = GetObject(, "Word.Application")
    

    现在将打开的应用程序关联为文档:

    Dim mocCustProperties As Microsoft.Office.Core.DocumentProperties
    Dim odpProp As Office.DocumentProperty
    

    现在 odpProp 可用于读取/添加属性:

    For Each odpProp In mocCustProperties
      If odpProp.Name = “something” Then
       ‘do dtuff
      End If
    Next
    

    必须有一种方法可以将活动文档引用为文档而不是应用程序,但我无法完成这项工作。 干杯,

    【讨论】:

      【解决方案2】:

      Drat - 上面漏掉了几行 - 请忽略。

      我明白了。 模块外声明:

      Imports moDoc = Microsoft.Office.Interop.Word
      

      在子中,这将打开的应用程序与对象相关联

      Dim oActiveApp As moDoc.Application
      oActiveApp = GetObject(, "Word.Application")
      oDocCustomProperty = oActiveApp.ActiveDocument.CustomDocumentProperties
      

      现在将打开的应用程序关联为文档

      Dim mocCustProperties As Microsoft.Office.Core.DocumentProperties
      Dim odpProp As Office.DocumentProperty
      mocCustProperties = CType(oDocCustomProperty, Office.DocumentProperties)
      

      现在 odpProp 可用于读取/添加属性

      For Each odpProp In mocCustProperties
        If odpProp.Name = “something” Then
          ‘do dtuff
        End If
      Next
      

      【讨论】:

        【解决方案3】:

        学到了更多。 我不再需要:

        Imports moDoc = Microsoft.Office.Interop.Word
        

        使用 Office.Core。而不是 Office.Core.Interop

        Dim Prop As Microsoft.Office.Core.DocumentProperty
        Dim oBuiltInProperties As Microsoft.Office.Core.DocumentProperties
        Dim oCustomProperties As Microsoft.Office.Core.DocumentProperties
        
        oBuiltInProperties = DirectCast(Globals.DocSelect.Application.ActiveDocument.BuiltInDocumentProperties, Microsoft.Office.Core.DocumentProperties)
        oCustomProperties = DirectCast(Globals.DocSelect.Application.ActiveDocument.CustomDocumentProperties, Microsoft.Office.Core.DocumentProperties)
        
        For Each Prop In oBuiltInProperties
            'do stuff
            Prop.Name = sx
            sy=Prop.Value.ToString
        next
        
        'create properties
        sx="New Property"
        sy="New Property Value"
        oCustomProperties.Add(sx, False, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, sy)
        

        现在一切正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-16
          • 1970-01-01
          • 1970-01-01
          • 2022-06-12
          相关资源
          最近更新 更多