MartyConnelly
martyconnelly at shaw.ca
Sat Mar 12 18:23:30 CST 2005
Whilest footling around with regular expressions I came across this Generic method to call Regular expressions from within Access Queries by John Nurick [Microsoft Access MVP] One interesting point is destroying the object after running the query since it otherwise stays in memory. 'START OF CODE Function rgxReplace(Optional Target As Variant, _ Optional Pattern As String = "", _ Optional Replacement As String = "", _ Optional CaseSensitive As Boolean = False, _ Optional ReplaceAll As Boolean = True, _ Optional Multiline As Boolean = False, _ Optional FailOnError As Boolean = True, _ Optional Persist As Boolean = True) _ As Variant 'Regular expression replacement function suitable for use 'in VBA generally and in Access queries. '-- from John Nurick [Microsoft Access MVP] 'Takes a search string (Target), a regular expression '(Pattern), and a replacement string (Replacement). 'If a substring of Target matches Pattern, that substring 'is replaced by Replacement. (The substring can be the whole 'of Target.) 'CaseSensitive matches regardless of case 'but does not affect the case of Replacement. 'ReplaceAll controls whether all substrings 'that match Pattern are replaced or only the first. 'Multiline controls whether a Target containing 'newlines is parsed as one multiline string or a 'succession of one-line strings. 'Persist controls whether the compiled RegExp object 'remains in memory ready for the next call to the 'function or whether it is disposed of immediately. This 'means the function can be used in queries without having 'to create, compile, use and destroy 'a new RegExp object for each row being processed. 'But it also means that the object remains in memory 'after the query has run. To destroy the object and release 'the memory, call this function one last time with no arguments. ' 'Calling the function with different arguments (e.g. a new 'Pattern and/or Replacement) recompiles the RegExp object, so 'the function can be used in different queries. However there 'may be problems if two threads are calling the function at 'the same time. Const rgxPROC_NAME = "rgxReplace" Static oRE As Object 'Static declaration means we don't have to create 'and compile the RegExp object every time the function 'is called. On Error GoTo ErrHandler rgxReplace = Null 'Default return value 'NB: if FailOnError is false, returns Null on error If IsMissing(Target) Then 'This is the signal to dispose of oRE Set oRE = Nothing Exit Function 'with default value End If 'Create the RegExp object if necessary If oRE Is Nothing Then Set oRE = CreateObject("VBScript.Regexp") End If With oRE 'Check whether the current arguments (other than Target) 'are different from those stored in oRE, and update them '(thereby recompiling the regex) only if necessary. If CaseSensitive = .ignoreCase Then .ignoreCase = Not .ignoreCase End If If ReplaceAll <> .Global Then .Global = ReplaceAll End If If Multiline <> .Multiline Then .Multiline = Multiline End If If Pattern <> .Pattern Then .Pattern = Pattern End If 'Finally, do the replacement If IsNull(Target) Then rgxReplace = Null Else rgxReplace = .Replace(CStr(Target), Replacement) End If End With 'Tidy up and normal exit If Not Persist Then Set oRE = Nothing Exit Function ErrHandler: If FailOnError Then With Err Select Case .Number 'Replace the default "object-defined error" message Case 13: .Description = "Type mismatch, probably because " _ & "the ""Target"" argument could not be converted to a string" Case 5017: .Description = "Syntax error in regular expression" Case 5018: .Description = "Unexpected quantifier in regular expression" Case 5019: .Description = "Expected ']' in regular expression" Case 5020: .Description = "Expected ')' in regular expression" Case Else If oRE Is Nothing Then 'Failed to create Regexp object .Description = "Could not create VBScript.RegExp object. " & _ Err.Description Else 'Unexpected error .Description = rgxPROC_NAME & ": " & .Description End If End Select Set oRE = Nothing .Raise Err.Number, rgxPROC_NAME, _ rgxPROC_NAME & "(): " & .Description End With Else 'Fail silently Err.Clear Set oRE = Nothing End If End Function 'END OF CODE Susan Harkins wrote: >I'm looking for a macro that counts words on a single page and prints the >results. Anyone have something I could adapt? > >Susan H. > > -- Marty Connelly Victoria, B.C. Canada