Vba 如何根据if-else条件将光标放置在某个样式位置

Vba 如何根据if-else条件将光标放置在某个样式位置,vba,ms-word,Vba,Ms Word,我正在编写一个脚本来检查word文件中是否只有“允许的样式”。目前,我可以相应地返回true或false 但是,如果不是允许的样式,我也希望将光标放在该特定样式上 这是脚本的相关部分: Function AllStylesInArray(Arr() As Variant) As Boolean Dim doc As Document, s As Style Set doc = ActiveDocument AllStylesInArray = True Fo

我正在编写一个脚本来检查word文件中是否只有“允许的样式”。目前,我可以相应地返回true或false

但是,如果不是允许的样式,我也希望将光标放在该特定样式上

这是脚本的相关部分:

Function AllStylesInArray(Arr() As Variant) As Boolean
    Dim doc As Document, s As Style

    Set doc = ActiveDocument

    AllStylesInArray = True

    For Each s In doc.Styles
        If s.InUse = True Then
            With doc.Content.Find
                .ClearFormatting
                .Text = ""
                .Style = s
                .Execute Format:=True
                If .Found = True Then
                    AllStylesInArray = IsInArray(s.NameLocal, Arr)
                    If AllStylesInArray = False Then Exit For
                End If
            End With
        End If
    Next s
End Function
编辑:这里,函数IsInArray检查样式是否在数组中

Private Function IsInArray(valToBeFound As Variant, Arr As Variant) As Boolean
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: Function to check if a value is in an array of values
'INPUT: Pass the function a value to search for and an array of values of any data type.
'OUTPUT: True if is in array, false otherwise
Dim element As Variant
On Error GoTo IsInArrayError: 'array is empty
    For Each element In Arr
        If element = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next element
Exit Function
IsInArrayError:
On Error GoTo 0
IsInArray = False
End Function
因此,假设样式测试不在数组中,而是在word文档中。我希望光标位置位于带有样式测试的文本处。如果有多个样式不在一个数组中,则光标可以位于第一个样式上

提前谢谢

编辑:这是我检查是否只使用允许的样式的地方:

Sub StyleExists()
Dim Arr() As Variant
Dim i As Integer
i = 0

Open "C:\Users\Bla\Downloads\styles.txt" For Input As #1
Do While Not EOF(1) ' Loop until end of file.
    ReDim Preserve Arr(i) ' Redim the array for the new element
    Line Input #1, Arr(i) ' read next line from file and add text to the array
    i = i + 1
Loop
Close #1
MsgBox AllStylesInArray(Arr)
End Sub

编辑2:文件styles.txt包含样式名称,每行一个

如果使用
范围
对象(而不是
文档内容
),则
查找将包含查找到的内容。因此,选择
范围
非常简单

在这种情况下,需要在每次迭代时将范围重置为整个文档,以确保在整个文档中搜索样式,而不仅仅是“找到的”范围,或者从“找到的”范围一直搜索到最后

比如说

Function AllStylesInArray(Arr() As Variant) As Boolean
    Dim doc As Document, s As Style
    Dim rngFound as Range

    Set doc = ActiveDocument

    AllStylesInArray = True

    For Each s In doc.Styles
        Set rngFound = doc.Content
        If s.InUse = True Then
            With rngFound.Find
                .ClearFormatting
                .Text = ""
                .Style = s
                .Execute Format:=True
                If .Found = True Then
                    AllStylesInArray = IsInArray(s.NameLocal, Arr)
                    If AllStylesInArray = False Then
                        rngFound.Select
                        Exit For
                    End If
                End If
            End With
        End If
    Next s
End Function

@BlueMango完成了,并解释了为什么这条线必须移动——很抱歉,我的逻辑最初太弱了。如果我可以测试的话,我会看到它,但是没有足够的信息来实际测试它。谢谢你一直和我在一起:-)