VBScript Calls a Macro in Outlook

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

VBScript Calls a Macro in Outlook

Post by armsys » Mon Apr 28, 2008 10:49 am

Given an Outlook mail item is opened (active), I would like to use MS script to call a macro named Normal.AS_Marcos.Email_Style. It fails.
Please show me a proper way to call an Outlook macro from MS script.
Thanks.

VBSTART
Sub Email_Style()
set app=createobject("Outlook.Application")
app.run MacroName:=Normal.AS_Macros.Email_Style
End Sub
VBEND

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Apr 28, 2008 10:54 am

Looks like it should be:

Code: Select all

VBSTART
Sub Email_Style()
  set app=createobject("Outlook.Application")
  app.run "Normal.AS_Macros.Email_Style"
End Sub
VBEND
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Apr 28, 2008 11:03 am

Macrus,
Thanks for your rapid rescue. However, it still generates error:
Microsoft VBScript runtime error: 438
Object doesn't support this property or method: 'app.run'

Please help. Thanks.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Apr 28, 2008 11:11 am

Marcus,
The following is the Outlook/Word macro in question:

Sub Email_Style()
ActiveWindow.View.Type = wdWebView
WordBasic.MailAsHTML
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "C:\ArmSys\Word\Template\Email.dot"
.XMLSchemaReferences.AutomaticValidation = True
.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
End With
Selection.Style = ActiveDocument.Styles("Email Normal")
End Sub

The VBA macro runs successfully to apply changes to an active Outlook mail item.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Apr 28, 2008 12:04 pm

To be honest I have no experience of writing VBA in Outlook, so I'm not familiar with what properties and methods are available. It would seem that Outlook does not offer a Run command for running macros in the same way that Word does.

Also - I guess you need to have Outlook open and an item active for your macro to work? But your code creates a new Outlook instance and then tries to run the macro. Presumably the item won't be active if Outlook has just been started.

So you'd probably need to split your code up, have a function that does the CreateObject to open Outlook and then another to select and activate the item. Then your code above. Either by finding a way to run Outlook macros (I'll search the docs and see what I can find), or converting that code to VBScript.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Apr 28, 2008 12:13 pm

All I could find for running VBA macros from outside of Outlook was this:

http://groups.google.com/group/microsof ... 2adbfbb537

But I couldn't get it to work. Sorry.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Apr 28, 2008 12:25 pm

Marcus,
This shows the strength and beauty of Macro Scheduler--short learning curve and fast result.
My workaround solution is pure MS code:
SRT>VBA_Email_Style
Press ALT
Press F8
Release ALT
WaitWindowOpen>Macros
Wait>.1
Send>Email_Style
Wait>.1
Press ALT
Send>r
Release ALT
End>VBA_Email_Style
Most importantly, it works fine.
Thanks for your help.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts