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...
Get a number from a WORD document
-
- BronzeLounger
- Posts: 1265
- Joined: 03 Feb 2010, 19:59
- Location: Terneuzen, the Netherlands
-
- Administrator
- Posts: 78670
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Get a number from a WORD document
Try this:
You can modify it to suit your needs.
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
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1265
- Joined: 03 Feb 2010, 19:59
- Location: Terneuzen, the Netherlands
Re: Get a number from a WORD document
I'll try... thank you
-
- NewLounger
- Posts: 6
- Joined: 04 Mar 2012, 16:40
Re: Get a number from a WORD document
Excellent !!!