Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA RegExp causes Compile error while vbscript.regexp works

Tags:

regex

vba

outlook

I was writing a script for VBA, for Outlook 2013, that uses regular expressions and every example I find seems to use Set regex = New RegExp to create a RegExp object. When I tried this I got Compile error: User-defined type not defined.

I managed to get regular expressions to work using Set regex = CreateObject("vbscript.regexp").

Any reason why the first options is not working?

like image 887
Batandwa Avatar asked Jan 15 '14 14:01

Batandwa


People also ask

What is the purpose of RegExp object in VBScript?

The VBScript RegExp object matches strings against special text patterns, called regular expressions. The InStr function can be used to search a string to see if it contains another string. The RegExp object provides a far more sophisticated string searching facility by using regular expressions.

What is VBScript RegExp?

Regular Expressions is a sequence of characters that forms a pattern, which is mainly used for search and replace. The purpose of creating a pattern is to match specific strings, so that the developer can extract characters based on conditions and replace certain characters.

Does VBA support RegEx?

RegEx stands for “Regular Expression” in VBA Excel and is a sequence of characters that defines the search pattern for finding a specific pattern of characters in a string of values. In a simple word, “we can create a regular expression pattern and use it to search for the string of that pattern.”

How do I write RegEx in VBA?

How to start using RegEx. To start using RegEx in VBA, you need to select and activate the RegEx object reference library from the references list. Start by going to the VBA IDE, select Tools –> References, then select Microsoft VBScript Regular Expressions 5.5, then click OK.


2 Answers

Probably only missing some dependencies, meaning some references should be added.

Go to Tools -> References -> Find & check the "Microsoft VBScript Regular Expressions" (1.0 or 5.5 both work for me).

like image 75
Bernard Saucier Avatar answered Sep 21 '22 14:09

Bernard Saucier


I know this question was already successfully answered, I just wanted to add a solution that worked for me.

Tools -> References - > "Microsoft VBScript Regular Expressions 5.5" was already activated.

Set regex = New RegExp caused a compile error.

Set regex = New VBScript_RegExp_55.RegExp works.

like image 45
Axel Avatar answered Sep 21 '22 14:09

Axel