使用Excel VBA从Word文档中提取内容控件属性

使用Excel VBA从Word文档中提取内容控件属性,vba,excel,ms-word,Vba,Excel,Ms Word,我的问题与这篇文章有些相似 我希望提取内容控件属性,而不是MSWord的内置文档属性 我是VBA的新手,任何可以从MS Word文档中提取内容控件属性的代码片段都会对我有很大帮助 提前谢谢 >作为初学者:考虑到大量的内容控制属性(Word),因此在您的问题中更具体,将有助于并且明确地显示对代码的尝试。 这里给出的列表概述了不同的属性,并给出了读写状态的指示。我不会发布链接摘要,但会指出,它以更清晰的方式详细说明了您在对象浏览器中可以看到的内容 这给出了每种类型的一般说明和限制。它更能描述控件的实

我的问题与这篇文章有些相似

我希望提取内容控件属性,而不是MSWord的内置文档属性

我是VBA的新手,任何可以从MS Word文档中提取内容控件属性的代码片段都会对我有很大帮助


提前谢谢

>作为初学者:考虑到大量的<代码>内容控制属性(Word),因此在您的问题中更具体,将有助于并且明确地显示对代码的尝试。

这里给出的列表概述了不同的属性,并给出了读写状态的指示。我不会发布链接摘要,但会指出,它以更清晰的方式详细说明了您在对象浏览器中可以看到的内容

这给出了每种类型的一般说明和限制。它更能描述控件的实际使用情况,可能有助于优化代码

可以将控件作为集合循环。我写这篇文章是为了在Word中实现;您提供的链接显示了如何在Excel Word中实现此功能

Public Sub test()

Dim ContentControl As ContentControl

For Each ContentControl In ThisDocument.ContentControls

   Debug.Print ContentControl.Type

Next ContentControl

End Sub
您可以编写一些代码,在检查存在的类型(如上所述)之后,将属性(如果适用)写入文本文件或工作表

探索这一点的最好方法可能是打开对象浏览器,读取类中每个成员的相关信息,并在该上下文中编写代码

如果希望以与提供的链接相同的方式访问控件,请查看可用的

下面是一个检索项目的简单示例(通过@DavidZemens从和中调整代码)。为您提供了10个如何从Excel访问的入门知识。请注意,这是针对特定枚举而定制的(
8
for
wdContentControlCheckBox
),它返回一个布尔值;这就是为什么更好地理解您的目标并看到您的尝试会有所帮助

Option Explicit

Public cc As Object

Public Sub Testing()

  Dim PropVal As Boolean
  Const wdContentControlCheckBox As Long = 8

  PropVal = ReadProp(wdContentControlCheckBox, "C:\Users\User\Desktop\Test.docm")

  MsgBox PropVal

End Sub

Function ReadProp(ByVal sPropName As Long, ByVal FileName As String) As Boolean

    Dim wdApp As Object 'Word.Application
    Dim doc As Object 'Word.Document

    Set wdApp = CreateObject("Word.Application")
    Set doc = wdApp.Documents.Open(FileName, ReadOnly:=True)

    Dim bCustom As Boolean
    Dim sValue As String

    On Error GoTo ErrHandlerReadProp
  'Try the built-in properties first
  'An error will occur if the property doesn't exist
    sValue = doc.BuiltinDocumentProperties(sPropName).Value
    ReadProp = CBool(sValue)

Exit Function

ContinueCustom:
  bCustom = True

Custom:
  sValue = doc.CustomDocumentProperties(sPropName).Value
  ReadProp = CBool(sValue)
  Exit Function

ErrHandlerReadProp:
  Err.Clear
  'The boolean bCustom has the value False, if this is the first
  'time that the errorhandler is runned
  If Not bCustom Then
    'Continue to see if the property is a custom documentproperty
    Resume ContinueCustom
  Else
    'The property wasn't found, return an empty string
   ' ReadProp = "" ''Commented out by QHarr
    Msgbox "Property not found"
    Exit Function
  End If

End Function

Stackoverflow不会为您编写代码。请先尝试解决此问题。或者,至少,请举例说明您希望访问哪些类型的内容控件的属性。。。