How to make this regex work in VBA? (1 Viewer)

prabha_friend

Prabhakaran Karuppaih
Local time
Tomorrow, 04:04
Joined
Mar 22, 2009
Messages
784
Code:
Dim MyRegExp As RegExp
Set MyRegExp = New RegExp
With MyRegExp
    .Pattern = "(?<=Your message wasn't delivered to )(.*)(?= because)"
    Cells.SpecialCells(xlCellTypeLastCell).Offset(1, -1).Value = MyRegExp.Execute(Returned_Mail.Body)
End With
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:34
Joined
May 21, 2018
Messages
8,528
The execute method returns a matchcollection which is a collection of the matched strings. Could be more than one. At a minimum you probably need and index, but I would verify that a match was made. Maybe something like

Dim MyRegExp As RegExp
Dim MyMatches as MatchCollection
Set MyRegExp = New RegExp

MyRegExp.Pattern = "(?<=Your message wasn't delivered to )(.*)(?= because)"
Set MyMatches = MyRegExp.Execute(Returned_Mail.Body)

If myMatches.count > 0 then
Cells.SpecialCells(xlCellTypeLastCell).Offset(1, -1).Value = myMatches(1)
end if
 

prabha_friend

Prabhakaran Karuppaih
Local time
Tomorrow, 04:04
Joined
Mar 22, 2009
Messages
784
Hi Maj,
Thanks for your reply. But I am getting the following Error:

Run-time error '5017':
Method 'Execute' of object 'IRegExp2' Failed.

What to do? Please Help.

With Hope,
Prabhakaran
 

prabha_friend

Prabhakaran Karuppaih
Local time
Tomorrow, 04:04
Joined
Mar 22, 2009
Messages
784
Especially on the Below line of Code:
Code:
Set MyMatches = MyRegExp.Execute(Returned_Mail.Body)
 

prabha_friend

Prabhakaran Karuppaih
Local time
Tomorrow, 04:04
Joined
Mar 22, 2009
Messages
784
So, Can we conclude that using RegularExpressions we can't do a "between String" Match?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:34
Joined
May 21, 2018
Messages
8,528
PM @arnelgp he seems to be the guru here on regexp. I kind of stumble my way through.
 

Users who are viewing this thread

Top Bottom