从Word文件中提取特定文本

发布于 2024-10-09 16:24:33 字数 385 浏览 3 评论 0原文

我有一个Word文档,字体大小为14和18,文档有1500页。

我必须对字体 14 和字体 18 进行特定更改,因此在搜索后,我发现了 VBA for Word,它可以让我轻松地完成此操作。

由于我以前从未做过VBA,所以我尝试了这个:

Sub tryIt()

If Selection.Font.Size = 18 Then
MsgBox ("test")

End If
End Sub

但是它不起作用... msgbox() 只是为了看看它是否正确识别了文本。

那么如何在Word文档中分离/区分字体大小14和18并在vb脚本中实现呢?

有没有办法提取 14 和 18 大小的文本或搜索它,以便我可以进行查找/替换?

I have a Word document that has font sizes of 14 and 18, and the document is of 1500 pages.

I have to make specific changes to the font 14 and font 18, and so after searching, I came across VBA for Word that would allow me to do this easily.

Since I have never done VBA before, I tried this:

Sub tryIt()

If Selection.Font.Size = 18 Then
MsgBox ("test")

End If
End Sub

But it doesn't work... The msgbox() was just to see if it recognized the text properly.

So how can I separate / differentiate between font size 14 and 18 in a Word document and implement this in a vb script?

Is there any way to extract the 14 and 18 sized text or search for it so that I can do a find/replace?

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

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

发布评论

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

评论(2

沙与沫 2024-10-16 16:24:33

您没有说什么不适用于您的代码。但是,对于初学者来说,请尝试以下操作:

Sub tryIt()
    Dim findRange As Range
    Set findRange = ActiveDocument.Range
    findRange.Find.ClearFormatting
    findRange.Find.Font.Size = 18

    Do While findRange.Find.Execute(findtext:="") = True
        findRange.Select
        'Do something here

        DoEvents
    Loop
End Sub

You didn't say what wasn't working with your code. However, for starters try this:

Sub tryIt()
    Dim findRange As Range
    Set findRange = ActiveDocument.Range
    findRange.Find.ClearFormatting
    findRange.Find.Font.Size = 18

    Do While findRange.Find.Execute(findtext:="") = True
        findRange.Select
        'Do something here

        DoEvents
    Loop
End Sub
明明#如月 2024-10-16 16:24:33

确切地说出您想要的内容有点棘手,但以下宏会将字体大小为 14 的所有连续文本替换为文本“fuzz”。

Sub TryIt()
With Selection.Find
    .ClearFormatting
    .Font.Size = 14
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = "fuzz"
    .Wrap = wdFindContinue
    .Format = True
    .Execute Replace:=wdReplaceAll
End With
End Sub

如果这不是您想要的,您可能需要澄清一下您的意思。

It's a bit tricky to tell what you're after exactly, but the following macro will replace all contiguous text that is in font size 14 with the text "fuzz".

Sub TryIt()
With Selection.Find
    .ClearFormatting
    .Font.Size = 14
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = "fuzz"
    .Wrap = wdFindContinue
    .Format = True
    .Execute Replace:=wdReplaceAll
End With
End Sub

If this isn't what you're after, you may need to clarify a bit what you mean.

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