如何通过VB宏遍历Word文档中的文本 [英] How to iterate through text in the Word document by VB macro

查看:501
本文介绍了如何通过VB宏遍历Word文档中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Macro来计数Word文档中的字符,我不知道如何在Visual Basic宏中引用两个文本并对其进行遍历.

I wanted to count chars in the Word document by Macro I have no idea how to get reference two the text in visual basic macro and go through it.

我想计算一下文档中每个字符的数量. 例如在文档中:

I would like to count how many of every char was in the document. For example in document:

ABZBB


A x 1
B x 3
Z x 1

   Sub Macro1()
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=200, Height:=400)
Box.TextFrame.TextRange.Text = "My text comes this way" + Chr(10)
Dim s As String
Application.ScreenUpdating = False
docLength = ActiveDocument.Range.Characters.Count

Box.TextFrame.TextRange.Text = Box.TextFrame.TextRange.Text + "Text length is: " + Str(docLength) + Chr(10)

Dim arr(128) As Integer
Dim character As Integer
For i = 1 To docLength - 1

        character = Asc(ActiveDocument.Range.Characters(i))
If iAsc >= 0 And iAsc <= 127 Then
         arr(character) = arr(character) + 1
 End If
Next i


End Sub

推荐答案

以下是对文档中的各个字母(和其他一些字符)进行计数的简单示例,也许很慢.

Below is a simplistic, and perhaps slow, example of counting individual letters (and some other characters) in a document.

Sub CountChars()
    Dim iCount(57) As Integer
    Dim x As Integer
    Dim iTotal As Integer
    Dim iAsc As Integer

    Application.ScreenUpdating = False
    iTotal = ActiveDocument.Range.Characters.Count

    For x = 1 To iTotal
        iAsc = Asc(ActiveDocument.Range.Characters(x))
        If iAsc >= 65 And iAsc <= 122 Then
        iCount(iAsc - 65) = iCount(iAsc - 65) + 1
        End If
    Next x
    For x = 0 To 57
        Debug.Print x, iCount(x)
    Next x
    Application.ScreenUpdating = True
End Sub

更改为

Debug.Print Chr(x + 65), iCount(x)

显示字符本身.

可以使用Find(以某种方式)计算字符的出现;否则将需要正则表达式.

It may be possible to use Find (somehow) to count occurrences of characters; otherwise it would require Regex.

使用替换的替代方法:

'Tools, References: Microsoft Scripting Runtime
Sub CountCharsWithReplace()
    Dim doc As Document
    Dim rDupe As Range
    Dim dicChars As Scripting.Dictionary
    Dim s As String
    Dim iTotalChars As Integer
    Dim iTempChars As Integer
    Dim iDiff As Integer
    Dim n As Integer
    Dim blnExec As Boolean
    Dim lett As Variant
    Application.ScreenUpdating = False
    Set doc = ActiveDocument
    iTotalChars = doc.Range.Characters.Count
    Set rDupe = doc.Range
    Set dicChars = New Scripting.Dictionary
    Do While rDupe.Characters.Count > 1
        s = rDupe.Characters(1).Text
        blnExec = rDupe.Find.Execute(s, , , , , , , , , "", wdReplaceAll)
        iTempChars = doc.Range.Characters.Count
        iDiff = iTotalChars - iTempChars
        iTotalChars = iTempChars
        If Asc(s) >= 65 And Asc(s) <= 122 Then
            dicChars.Add s, iDiff
        End If
        n = n + 1
    Loop
    ActiveDocument.Undo Times:=n
    Application.ScreenUpdating = True
    For Each lett In dicChars.Keys
        Debug.Print lett, dicChars(lett)
    Next lett
End Sub

这篇关于如何通过VB宏遍历Word文档中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆