Word VBA 选择一个句子

发布于 2024-10-16 23:15:30 字数 46 浏览 2 评论 0原文

我想在Word文档中选择以粗体字符开头的句子。

我该怎么做呢?

I want to choose sentences in a Word document which start with a bold character.

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2024-10-23 23:15:30

怎么样:

Dim s As Range
For Each s In ActiveDocument.Sentences
    If s.Characters(1).Bold = True Then
        Debug.Print s
    End If
Next

我不太明白你所说的“选择”是什么意思,你可以使用s.Select,但它只能用于一个句子。

编辑评论

非常粗略地:

Dim s As Range
Dim doc1 As Document
Dim doc2 As Document

Set doc1 = Word.Documents("Doc1.doc")
Set doc2 = Word.Documents("Doc2.doc")

For Each s In doc1.Sentences
    If s.Characters(1).Bold = True Then
        Debug.Print s
        doc2.Select
            Selection.Find.ClearFormatting
            With Selection.Find
                ''.Text cannot be set to more than 256 characters
                .Text = s
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            a = Selection.Find.Execute
            If a = True Then
                Selection.Characters(1).Font.Bold = True
                ''Or for the whole sentence
                Selection.Font.Bold = True
            End If
        doc1.Select
    End If
Next

编辑评论 #2

要比较的两个文档是相同的,所以也许:

Dim doc1 As Document
Dim doc2 As Document
Dim i As Integer

Set doc1 = Word.Documents("Doc1.doc")
Set doc2 = Word.Documents("Doc2.doc")

If doc1.Sentences.Count <> doc2.Sentences.Count Then
    MsgBox "These two documents do not match."
    Exit Sub
End If

For i = 1 To doc1.Sentences.Count
    If doc1.Sentences(i).Characters(1).Bold = True Then
        ''Debug.Print doc1.Sentences(i)
        If doc1.Sentences(i).Text = doc2.Sentences(i).Text Then
            doc2.Sentences(i).Bold = True
        Else
            MsgBox "Sentences do not match."
        End If
    End If
Next

How about:

Dim s As Range
For Each s In ActiveDocument.Sentences
    If s.Characters(1).Bold = True Then
        Debug.Print s
    End If
Next

I do not quite see what you mean by 'choose', you can use s.Select, but it will only work for one sentence.

EDIT re comments

Very roughly:

Dim s As Range
Dim doc1 As Document
Dim doc2 As Document

Set doc1 = Word.Documents("Doc1.doc")
Set doc2 = Word.Documents("Doc2.doc")

For Each s In doc1.Sentences
    If s.Characters(1).Bold = True Then
        Debug.Print s
        doc2.Select
            Selection.Find.ClearFormatting
            With Selection.Find
                ''.Text cannot be set to more than 256 characters
                .Text = s
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            a = Selection.Find.Execute
            If a = True Then
                Selection.Characters(1).Font.Bold = True
                ''Or for the whole sentence
                Selection.Font.Bold = True
            End If
        doc1.Select
    End If
Next

EDIT re Comments #2

The two documents to be compared are identical, so perhaps:

Dim doc1 As Document
Dim doc2 As Document
Dim i As Integer

Set doc1 = Word.Documents("Doc1.doc")
Set doc2 = Word.Documents("Doc2.doc")

If doc1.Sentences.Count <> doc2.Sentences.Count Then
    MsgBox "These two documents do not match."
    Exit Sub
End If

For i = 1 To doc1.Sentences.Count
    If doc1.Sentences(i).Characters(1).Bold = True Then
        ''Debug.Print doc1.Sentences(i)
        If doc1.Sentences(i).Text = doc2.Sentences(i).Text Then
            doc2.Sentences(i).Bold = True
        Else
            MsgBox "Sentences do not match."
        End If
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文