繁体   English   中英

Word对象的VBA范围

[英]Word VBA Range from Words Object

上下文:我正在写一个Word VBA宏,该宏循环遍历文档中的每个单词,标识缩写词,并创建一个缩写词列表作为新文档。 下一步是确定首字母缩写词首次出现时是否在括号中(意味着可能已拼出)。 因此,我想扩展范围以找出单词两侧的字符是否为“(”和“)”。

问题:我不知道如何将单词的范围分配给可以扩展的范围变量。 使用“ rngWord = ActiveDocument.Words(k)”(其中k是计数器变量)将得到错误#91,对象变量或未设置块变量。 因此,大概是我缺少的Word的方法或属性。 但是,基于Microsoft的VBA参考,Words集合的成员已经是范围,因此我很困惑为什么不能为范围变量分配一个。

Dim intArrayCount As Integer
Dim booAcroMatchesArray As Boolean
Dim intNextAcro As Integer
Dim strAcros(1000) As String
Dim strContext(1000) As String
Dim booAcroDefined(1000) As Boolean
Dim strTestMessage As String

i = 1
booAcroMatchesArray = False
intNextAcro = 1
For k = 1 To ActiveDocument.Words.Count
    strWord = ActiveDocument.Words(k).Text
    rngWord = ActiveDocument.Words(k) //The line that's missing something
    MsgBox strWord
    rngWord.Expand Unit:=wdCharacter
    strWordPlus = rngWord
    MsgBox strWordPlus



    strWord = Trim(strWord)

    If strWord = UCase(strWord) And Len(strWord) >= 2 And IsLetter(Left(strWord, 1)) = True Then
        'MsgBox ("Word = " & strWord & " and Length = " & Len(strWord))
        For intArrayCount = 1 To 1000
            If strWord = strAcros(intArrayCount) Then booAcroMatchesArray = True
        Next intArrayCount

        'MsgBox ("Word = " & strWord & " Match = " & booAcroMatchesArray)

        If booAcroMatchesArray = False Then
            strAcros(intNextAcro) = strWord
            intNextAcro = intNextAcro + 1



        End If

        booAcroMatchesArray = False

    End If

Next k

需要使用Set分配对象变量。 代替:

rngWord = ActiveDocument.Words(k)

采用

Set rngWord = ActiveDocument.Words(k)

这个小样本正常工作:

Sub WordRangeTest()

Dim rngWord As Range
Set rngWord = ActiveDocument.Words(1)
MsgBox (rngWord.Text)

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM