Vba script including macro appears in rules but wont run

Status
Not open for further replies.

Paul Fone

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hi there

Here is a script for Outlook 2010 to click on the first hyperlink in an email. It shows up as a script in the rules in outlook - the macro appears in the developer tab in Macros and runs fine but it will not trigger live. Any help would be much appreciated

'Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub LaunchURL(Itm As Outlook.MailItem)
Set objOL = Application
Set objDoc = objOL.ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objDoc.Hyperlinks(1).Follow
Set Itm = Nothing
Set objOL = Nothing
Set objDoc = Nothing
Set objSel = Nothing
End Sub
Sub LoopThruEmails()
Dim i As Long
Dim InboxItems As Outlook.Items
Dim thisEmail As Outlook.MailItem
Set InboxItems = GetItems(GetNS(GetOutlookApp), olFolderInbox)
' assume there are inbox items
For i = InboxItems.Count To 1 Step -1
If TypeName(InboxItems.Item(i)) = "MailItem" Then ' it's an email
Set thisEmail = InboxItems.Item(i)
thisEmail.Display
' Sleep (3000) ' wait 3 seconds
Dim currItem As Outlook.MailItem
Dim Insp As Outlook.Inspector
Set currItem = Application.ActiveInspector.CurrentItem
LaunchURL currItem
thisEmail.Close olDiscard
Exit Sub

End If
Next i
End Sub
Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function
Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function
Function GetItems(olNS As Outlook.NameSpace, folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function




Regards Paul Fone
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This line: Sub LaunchURL(Itm As Outlook.MailItem) tells it to only work when called by a rule or another script.
You need something like this to run it manually
Sub LaunchURL()
dim Itm As Outlook.MailItem
set itm = objOL.ActiveExplorer.Selection.Item(1)
 

Paul Fone

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hi Diane

I actually want it to run automatically using a rule but it doesn't work

Regards
Paul Fone
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
If run automatically by a rule, the rules passes a ref on the newly added item. Instead of using that one, you try to follow a link in the body of the current open email, see this line:
Code:
Set objDoc = objOL.ActiveInspector.WordEditor
 

Paul Fone

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hello Michael

I am a real novice at this so could you please explain where I place this line or what is my next step?
 
Status
Not open for further replies.
Top