2

我正在尝试遍历循环中的选定文本(直到完成)并按顺序检索每个字符。

Word 中的对象似乎与 excel 中的对象大不相同,我可以很容易地做到这一点。任何人都有几行

  1. 捕获所选文本的长度。
  2. 遍历它并一次获取每个字符,直到我到达所选文本中的最后一个字符。
4

2 回答 2

5

我不会对 Word 进行很多编码,这很令人困惑,但我认为您也会看到一些相似之处。这是一些执行您要求的代码,即将所选文本中的字符数打印到即时窗口,然后打印每个字符:

Sub CountWordStuff()
Dim char As Range

With Selection
    Debug.Print "Character count: "; .Characters.Count
    Debug.Print "Words - kind of: "; .Words.Count; ""

    For Each char In Selection.Characters
        Debug.Print char.Characters(1)
    Next char
End With
End Sub

我也算了一笔Words账。它看起来Words包括分段符,可能还有其他项目。

于 2013-06-10T01:36:28.217 回答
0

如果这是您想要做的,您可以使用正则表达式将其转换为数组。

 Function TextIntoObject(InputString As String) As Object
'Reference set to Microsoft VBScript Regular Expressions 5.5
Dim RegExp As New RegExp
RegExp.Pattern = "."
RegExp.Global = True
Set TextIntoObject = RegExp.Execute(InputString)


End Function

Sub TestFunction()
Dim i As Integer
Dim x As Integer
Dim SampleText As String
SampleText = "This is the text I want to use"
x = TextIntoObject(SampleText).Count
MsgBox "The length of my text is " & x
For x = 0 To x - 1
    MsgBox TextIntoObject(SampleText)(x)
Next

End Sub
于 2013-06-09T22:09:47.767 回答