开发者

Extracting specific text from a Word file

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 VB开发者_运维知识库A 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜