使用宏在Word文档中查找不同格式的文本块

发布于 2024-10-17 21:08:48 字数 793 浏览 2 评论 0原文

我正在努力在 Microsoft Word (2007) 中为包含如下文本的文档创建宏:

(1) 粗体标题。 普通文本。

对于此文本,我想对该文本的第一部分 - (1) 粗体标题。 执行一些转换。

而“(1)”和“粗体标题”。具有一致的风格(粗体和 Arial),两者之间的空格没有(它是 Times New Roman,非粗体)。

我认为搜索以下内容是可行的,没有任何格式限制。

"^13(\([0-9]@\)) (?@)."

不幸的是,也存在文本如下所示的情况:

(1) 普通文本。

对于这样的块,我想完全跳过文本。

不幸的是,我的通配符搜索也会找到这些实例,除非我可以通过字体样式限制它。

如果我可以规范第一种情况下的空格,那么我可以在通配符搜索中添加字体限制以获取正确的内容。

.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True

但是,我需要能够在搜索中抓取两个不同格式的项目来标准化该空间,但根据我有限的 VBA 知识,这似乎是不可能的。

有没有办法在Word宏中查找不同格式的文本?

谢谢!

I'm working on creating a macro in Microsoft Word (2007) for a document that contains text such as this:

(1) Bold heading. Normal text.

With this text I'd like to perform a number of transformations upon the first part - (1) Bold heading. - of that text.

While "(1)" and "Bold heading." have a consistent style (bold and Arial), the space between the two does not (it's Times New Roman, non-bold).

I thought a search for the below would work, without any format restrictions.

"^13(\([0-9]@\)) (?@)."

Unfortunately, there's also cases where text is as follows:

(1) Normal text.

For blocks like this, I want to completely skip the text.

Unfortunately, my wildcard search is going to find these instances too, unless I can restrict it by font styles.

If I could normalize the space in the first case, then I could add the Font restrictions on my wildcard search to grab the correct content.

.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True

But, I'd need to be able to grab two differently formatted items in a search to normalize that space, which, from my limited knowledge of VBA, doesn't appear to be possible.

Is there a way to find text with different formatting, in a Word macro?

Thanks!

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

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

发布评论

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

评论(2

≈。彩虹 2024-10-24 21:08:48

我想知道这样的东西是否适合:

Dim s As Range
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = Word.Documents("Doc2.doc")

For Each s In doc.Sentences
    If s.Words(1).Bold = True Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            Debug.Print s
        End If
    End If
Next

请注意,Word 有一个非常讨厌的习惯,即不计算数字,它认为它们是自动的。

I wonder if something like this would suit:

Dim s As Range
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = Word.Documents("Doc2.doc")

For Each s In doc.Sentences
    If s.Words(1).Bold = True Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            Debug.Print s
        End If
    End If
Next

Note that Word has a nasty enough habit of not counting the numbers, it sees them as automatic.

白云悠悠 2024-10-24 21:08:48

Remou 的答案正是我所需要的,但由于 StackOverflow 是一个很棒的资源,因此我最终针对我们的特定情况对其进行了调整:

特别是,文本位于段落的第一句话内。不幸的是,这似乎并没有涵盖我们所有的情况,但它抓住了大多数情况,并让用户大部分到达那里。

(下面的一些评论包含在我发现的外部资源中,因此它们是否真的有必要值得怀疑,但是......它有效。)

' Do our bold heading replacements
Dim s As Range, p As Paragraph
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = ActiveDocument

For Each p In doc.Paragraphs
    Set s = p.Range.Sentences(1)
    If s.Words(1).Bold = True And s.Words(1).Characters(1) = "(" Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) (?@)."

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1 \2."
                .Replacement.Font.SmallCaps = True
                .Replacement.Font.Name = "Times New Roman"
                .Replacement.Font.Bold = False

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With

            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) "

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1" & vbTab
                .Replacement.Font.SmallCaps = False
                .Replacement.Font.Name = "Arial"
                .Replacement.Font.Bold = True

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With
        End If
    End If
Next

Remou's answer is exactly what I needed, but since StackOverflow is a great resource, this is what I ended up tweaking it to for our particular case:

In particular, the text is within the first sentence of a paragraph. Unfortunately this doesn't seem to catch all of our cases, but it grabs most of them, and gets the user most of the way there.

(Some of the comments below were included on external resources I'd found, so whether they're actually necessary is questionable, but ... it works.)

' Do our bold heading replacements
Dim s As Range, p As Paragraph
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document

Set doc = ActiveDocument

For Each p In doc.Paragraphs
    Set s = p.Range.Sentences(1)
    If s.Words(1).Bold = True And s.Words(1).Characters(1) = "(" Then
        BoldHead = True
        For Each wd In s.Words
            If Trim(wd) <> vbNullString _
                And wd <> "." _
                And wd.Bold = False Then
                BoldHead = False
            End If
        Next
        If BoldHead Then
            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) (?@)."

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1 \2."
                .Replacement.Font.SmallCaps = True
                .Replacement.Font.Name = "Times New Roman"
                .Replacement.Font.Bold = False

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With

            With s.Find
                ' Clear all previously set formatting for Find dialog box.
                .ClearFormatting
                .Text = "(\([0-9]@\)) "

                ' Clear all previously set formatting for Replace dialog box.
                .Replacement.ClearFormatting
                .Replacement.Text = "\1" & vbTab
                .Replacement.Font.SmallCaps = False
                .Replacement.Font.Name = "Arial"
                .Replacement.Font.Bold = True

                ' The following parameters must be set as follows to find only text formatted for the specified font.
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False

                .Execute Replace:=wdReplaceOne
            End With
        End If
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文