Extracting specific text from a Word file

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:39:53

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4568551/extracting-specific-text-from-a-word-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!