幻影风格的单词

时间:2018-05-30 08:28:54

标签: vba ms-word

我有一个Word文档,我在其上运行一个宏来查找样式Regular

我的代码如下:

If styleExisted = False Then
    For Each oRange1 In ActiveDocument.StoryRanges
        If oRange1.Characters.count > 0 And found = False Then
            oRange1.Select

            With Selection.Find
                .Style = "Regular"                  
            End With

            Selection.Find.Execute
            If Selection.Find.found = True Then
                test = Selection.range.Text

                found = True
                styleExisted = True
            End If
        End If
    Next oRange1
End If

但是,这个宏给了我一个误报,说文档中存在带有样式的内容。当我使用具有所选样式的“查找”对话框时,找不到任何内容。

在调试代码时,test获取文档中的第一个文本块。 当我明确地添加了一些带有样式的文本时,正在按预期选择新文本。

将文件保存为.docx并检查document.xml文件时,我会找到以下内容。

<w:tc>
    <w:tcPr>
        <w:tcW w:w="1074" w:type="pct"/>
    </w:tcPr>
    <w:p w:rsidR="004B7F7E" w:rsidRPr="006E4FD7" w:rsidRDefault="004B7F7E" w:rsidP="006E4FD7">
        <w:pPr>
            <w:pStyle w:val="TableHead"/>
            <w:spacing w:after="0"/>
            <w:jc w:val="left"/>
            <w:rPr>
                <w:rStyle w:val="Regular"/>
                <w:sz w:val="24"/>
                <w:szCs w:val="24"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="006E4FD7">
            <w:rPr>
                <w:bCs/>
                <w:sz w:val="24"/>
                <w:szCs w:val="24"/>
            </w:rPr>

我怎样才能解决这个误报?

这种情况只发生在一些文件上,这真是令人费解。

编辑:当我从其他文档添加一些正常内容并将其放在开头时,新内容中的第一个块正在被选中。

此外,在Regular样式的开头会有一些占位符文本(来自自定义模板);在准备文件时,这将被删除。

1 个答案:

答案 0 :(得分:0)

我已使用以下代码解决了该问题:

'' remove empty paragraphs
With Selection.Find
    .Text = "^13{2,}"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
'' remove empty paragraphs

感谢@GSerg提供有关段落字形的指针。

编辑: 使用.Style时,我仍然得到误报;所以我必须使用通配符<*>

更改我的代码,如下所示
With Selection.Find
    .Font.Italic = True
    .Font.Color = 32768
    .Text = "<*>"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With