Consulting

Results 1 to 3 of 3

Thread: Simple code for checking that a given text matches a pattern

  1. #1

    Simple code for checking that a given text matches a pattern

    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


  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    That really is extremely simple. I had forgotten about the 'like' thing.

    Thank you very much, macropod

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •