PDA

View Full Version : Macro OK. Script NOT OK.



Crow
07-21-2014, 01:39 PM
I have a working macro (below): a certain type of email arrives and a rule directs it to a folder. I hand run a macro which extracts info from the body, creates a url, a launches in my default browser (Chrome). Does exactly as required.

I've converted to a script (below). The rule directs to the folder, and then runs the script. The extract and create step works, but not the launch.

I'm new to VBA programming so guess I've made a mistake/assumption somewhere?

Macro
====
Sub PostLog()
Dim MyMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim strAddress As String
Dim objShell

Set MyMail = Application.ActiveExplorer().Selection(1)

Set Reg1 = New RegExp

With Reg1
.Pattern = "my pattern"
.Global = False
End With
If Reg1.Test(MyMail.Body) Then
Set M1 = Reg1.Execute(MyMail.Body)
For Each M In M1
strAddress = "my url" + Mid(M, 19, 26)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run (strAddress)
Next
End If

End Sub

Script
=====
Sub PostLog(MyMail As Outlook.MailItem)
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim strAddress As String
Dim objShell

Set Reg1 = New RegExp

With Reg1
.Pattern = "my pattern"
.Global = False
End With
If Reg1.Test(MyMail.Body) Then
Set M1 = Reg1.Execute(MyMail.Body)
For Each M In M1
strAddress = "my url" + Mid(M, 19, 26)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run (strAddress)
Next
End If

End Sub

westconn1
07-21-2014, 02:04 PM
how is the script called?


The extract and create step workshow can you tell?

Crow
07-21-2014, 02:15 PM
The script is called as "run a script" as the last task in a rule.

I've MsgBox traced to display the values.

I wondered if CreateObject is OK for a macro but not a script?