Get a number from a WORD document

User avatar
ErikJan
BronzeLounger
Posts: 1265
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Get a number from a WORD document

Post by ErikJan »

I'm "creating" a WORD document... Somewhere near the to there is this string "Document: 1 of 321".

I'd like to get the "321" in VBA so I can report this total in VBA... I know how to do these things in Excel, but WORD...

User avatar
HansV
Administrator
Posts: 78670
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get a number from a WORD document

Post by HansV »

Try this:

Code: Select all

Sub GetNumber()
    Dim rng As Range
    Dim strText As String
    Dim lngPos As Long
    Dim lngNumber As Long
    Set rng = ActiveDocument.Content
    With rng.Find
        .ClearFormatting
        .Text = "Document: [0-9]@ of [0-9]@[!0-9]"
        .MatchWildcards = True
        If .Execute Then
            strText = rng.Text
            strText = Left(strText, Len(strText) - 1)
            lngPos = InStrRev(strText, " ")
            lngNumber = Val(Mid(strText, lngPos + 1))
            ' Do something with lngNumber here, e.g.
            MsgBox "The number of documents is " & lngNumber, vbInformation
        Else
            MsgBox "Number of documents not found!", vbInformation
        End If
    End With
End Sub
You can modify it to suit your needs.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1265
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Get a number from a WORD document

Post by ErikJan »

I'll try... thank you

Murat OSMA
NewLounger
Posts: 6
Joined: 04 Mar 2012, 16:40

Re: Get a number from a WORD document

Post by Murat OSMA »

Excellent !!!
Www.ExcelVBA.Net - Msn Messenger: muratosma@excelvba.net