troubleshooting Question

Updating Word macro and adding a pop-up error message when applicable

Avatar of Andreamary
Andreamary asked on
* AAAMicrosoft WordVBA
2 Comments1 Solution186 ViewsLast Modified:
My current macro code (see code below) finds all instances of the following three string formats (which are work order #'s) in a Word 2010 file (the file can be anywhere from 10 to 100 pages) and pastes them into a new Word file, one string per line:
  AAA-#### (where AAA could be any letter from A – Z, followed by a dash, then 4 numbers)
  ####-####### (4 numbers, followed by a dash, then 7 numbers)
  AAA-##-#### (where AAA could be any letter from A – Z, followed by a dash, 2 numbers, a dash, then 4 numbers)

Two changes I would like to make to the above current macro:
  • I would like to update the macro so it only searches for one of the three text strings above (as the other two text strings have since become obsolete) to then paste into a new Word file, one string per line:
####-####### (4 numbers, followed by a dash, then 7 numbers)

  • I would like the macro to generate an error message as a dialog pop-up box at the first instance it comes across a text string that does not follow the approved format as outlined above (####-####### [4 numbers, followed by a dash, then 7 numbers], with the error message that includes the problem text string (work order #) to assist the user in locating the erroneously-entered work order number in the document and fixing it. Once the user corrected the work order number, the user would then re-run the macro.

The two instances of wrongly formatted strings for the macro to detect and generate an error message are as follows:
####-###### (4 numbers, followed by a dash, then 6 numbers)
####-######## (4 numbers, followed by a dash, then 8 numbers)

The error message will ensure going forward that we are not missing any work order numbers being transferred by the macro to the new Word file due to the user mis-keying in the work orders by accidentally adding or subtracting one digit.

I've attached a sample Word file showing the two examples of wrongly-formatted text strings that can occur and for which I would like the macro to generate an error message.

The current code is shown below:
Sub Extract_WOs_ADCs()
'
' Extract_WOs_ADCs Macro
'
' Keyboard Shortcut: Ctrl+y
'
    Set Activedoc = ActiveDocument
    strRes = ""
    Dim reg As Object 'VBScript_RegExp_55.regexp
    Dim Match As Object ' VBScript_RegExp_55.Match
    Dim Matches As Object 'VBScript_RegExp_55.MatchCollection

    ' instanciation
    Set reg = CreateObject("VBScript.RegExp")
    With reg
        .Global = True
        .MultiLine = True
        .Ignorecase = True
        .Pattern = "\b([A-Z]{3}(-\d{2})?-\d{4}|\d{4}-\d{7})\b"
    Set Matches = .Execute(Activedoc.Range.Text)
    End With
    For Each Match In Matches
        strRes = strRes & Match.Value & vbCrLf
    Next Match
    
    If strRes <> "" Then
        Set newDoc = Documents.Add
        newDoc.Range.Text = strRes
    Else
        MsgBox "No Matches"
    End If
End Sub

I hope I've provided sufficient details. Please let me know if you have any questions...

Thanks!
Andrea
Sample_EE.docx
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros