PDA

View Full Version : [SOLVED:] Simple code for checking that a given text matches a pattern



adrianrff
11-23-2017, 08:15 AM
Hi, folks.

I have Word file that executes some code upon saving it which is working quite well so far. The code saves the file with a given name (taken from a textbox inside the file) and some other stuff. I want to check that the pattern of the name matches a given pattern.

The textbox contains a text like "Memorándum interno SG-025-17". The code extracts the "SG-025-17" part and uses that to save the file. What I want to do is, before saving the file, check that the sequence following "Memorándum Interno" matches the pattern SG-[number][number][number]-17 (3 numbers exactly). I'm thinking of using regex but I feel like it could complicate things unnecessarily and I'm also a little pressed on time to try to figure it out by myself.

Any suggestion will be much appreciated.

Thanks in advance

macropod
11-23-2017, 02:56 PM
This is pretty simple, really. Suppose your 'SG-025-17' is in a string variable named StrFlNm. You can test StrFlNm's contents with:

If StrFlNm Like "SG-###-17" Then
'validated
MsgBox StrFlNm & " is valid", vbInformation
Else
'not validated
MsgBox StrFlNm & " is invalid", vbCritical
End If

adrianrff
11-23-2017, 03:34 PM
That really is extremely simple. I had forgotten about the 'like' thing.

Thank you very much, macropod