Solved

VBA Word macro - how to get characters after the searched for string

Posted on 2016-09-16
5
55 Views
Last Modified: 2016-09-17
I am trying to create a macro which will find a reference number in the document and save as a variable but I cannot seem to find out how to do this.
The reference number will always be 5 characters after the word "Reference: " so I am hopefully looking for some simple code allong the lines of:
Content.Find.Text  = "Reference: " + 5

This is my code so far:
Dim c As Range
Dim startword As String
startword = "Reference: "
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting

With c.Find
.Text = startword
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWildcards = True
End With

c.Find.Execute

Open in new window


Any thoughts?
0
Comment
Question by:fruitloopy
  • 3
  • 2
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 41801241
This assumes that the reference number is numeric
Sub FindRefNumber()
    Dim c As Range
    Dim startword As String
    Dim RefNumber As String
    startword = "Reference: "
    Set c = ActiveDocument.Content
    
    With c.Find
        .Text = startword & "[0-9]{5}"
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
    
        If .Execute() Then
            RefNumber = Right(c.Text, 5)
        End If
    End With
End Sub

Open in new window

0
 
LVL 2

Author Closing Comment

by:fruitloopy
ID: 41801404
AWESOME!

I knew it would be something as simple as that. Thank you.

Final code (Modified as the reference number begins with a capital letter):
[code]Dim c As Range
Dim startword As String
Dim refnumber As String

startword = "Reference: "
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting

With c.Find
.Text = startword & "[A-Z0-9]{5}"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True

If .Execute() Then
refnumber = Right(c.Text, 5)
End If

End With[/code]
Thanks again
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 41801505
Actually Graham if you're still there I have a very similar issue.
I need to run a second search for the words "Closing Date: " and to match the next set of numbers and words of a date in the format - 12th November 2016, etc. Obviously the date (1-31) and the number of characters in the month can change.
I've been searching around but can't figure out how to search for a variable length of characters up to a maximum of 20.
Any idea?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41802126
This works with the dates I have tested so far:
Sub FindDate()
    Dim c As Range
    Dim startword As String
    Dim CloseDate As String
    startword = "Closing Date: "
    Set c = ActiveDocument.Content
    
    With c.Find
        .Text = startword & "[0-9]{1,2}[d-t]{2} [JFMASOND]{1}[a-z]{2,8} [0-9]{4}"
        .MatchWildcards = True
    
        If .Execute() Then
            CloseDate = Split(c.Text, ": ")(1)
        End If
    End With
End Sub

Open in new window

1
 
LVL 2

Author Comment

by:fruitloopy
ID: 41803048
That's fantastic Graham. Works like a dream!
Thank you
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now