vba如何更新字段

时间:2010-10-20 11:59:58

标签: vba field

我想从VBA宏更新Word字段 Word Doucument在下面的代码中是wrdDoc 当我运行代码时,我在检索属性时遇到类型不匹配错误。

如果有人能提供帮助,我会非常感激。

Dim objCustomProperties As CustomProperties
Set objCustomProperties = wrdDoc.CustomDocumentProperties
For i = 1 To objCustomProperties.count
    objCustomProperty = objCustomProperties.Item(i)
Next

2 个答案:

答案 0 :(得分:1)

CustomDocumentProperties是一个DocumentProperties集合,因此它需要的类型(也可以是VariantObject)。

Dim wrdDoc As Document: Set wrdDoc = ActiveDocument
Dim objCustomProperties As DocumentProperties
''# Dim objCustomProperties As Variant ''# This also works
''# Dim objCustomProperties As Object ''# This also works
Set objCustomProperties = wrdDoc.CustomDocumentProperties
For i = 1 To objCustomProperties.Count
    ''# objCustomProperty = objCustomProperties.Item(i) ''# Your code
    Debug.Print objCustomProperties.Item(i).Name & ": " & objCustomProperties.Item(i).Value
Next

答案 1 :(得分:0)

你会满意吗:

Dim objCustomProperties As Object