如何遍历word文档的各个部分?

时间:2015-11-22 20:46:54

标签: vba ms-word word-vba

出于调试目的而出于好奇,我想调查word文档的结构。作为一个实际应用,我想象一下从Word到其他格式的基于VBA的转换器。

问题:我如何理解段落中存储的内容?

例如:

enter image description here

在VBA的帮助下,我想找到该文档包含:

  • 字母A
  • 按钮B
  • 字母C
  • 段落的结尾

我试着按字符逐个查看文档:

Private Sub CommandButton1_Click()
    Dim r As Range
    Dim i As Integer
    For i = 0 To ActiveDocument.Range.End - 1
        Set r = ActiveDocument.Range(i, i + 1)
        Debug.Print "The symbol: " & r.Text
    Next
End Sub

结果是:

The symbol: A
The symbol: 
The symbol:  
The symbol: C
The symbol: O
The symbol: N
The symbol: T
The symbol: R
The symbol: O
The symbol: L
The symbol:  
The symbol: F
The symbol: o
The symbol: r
The symbol: m
The symbol: s
The symbol: .
The symbol: C
The symbol: o
The symbol: m
The symbol: m
The symbol: a
The symbol: n
The symbol: d
The symbol: B
The symbol: u
The symbol: t
The symbol: t
The symbol: o
The symbol: n
The symbol: .
The symbol: 1
The symbol:  
The symbol: \
The symbol: s
The symbol:  
The symbol: 
The symbol: 
The symbol: 
The symbol: C
The symbol: 

按钮分布在一个范围内。我无法找到范围对象属性,这样我就可以区分按钮和通常的文本。

0 个答案:

没有答案