Running a macro

S

Sue Mosher [MVP-Outlook]

Put code in the Inspectors.NewInspector event handler. This code needs to go into the built-in ThisOutlookSession module:

Dim WithEvents colInsp As Outlook.Inspectors

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Call yourMacro ' call your macro here
End Sub

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

Thanks I tried it but I need to let the email open completly before it
runs the macro.

I'm trying to open all attachments automatically when the email is
opened. Your solution acts too early.

How can i set it so it only runs the macro automatically after the
email is opened
 
S

Sue Mosher [MVP-Outlook]

If it's a timing issue, you may have to experiments with different approaches, such as a loop that waits for a few seconds or using the Inspector.Activate event or using NewInspector to instantiate a MailItem object WithEvents and putting your code in that object's Open event.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

I tried this, but found that the code needs to complete then the
email opens. This gives me errors as i can't do anything with the
attachments at this stage.

What i Need is something that will run after the form has completed
opening
 
S

Sue Mosher [MVP-Outlook]

Tried what. It's impossible to know what message you're responding to. Please quote earlier messages.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

Tried what. It's impossible to know what message you're responding to. Please quote earlier messages.



If it's a timing issue, you may have to experiments with different
approaches, such as a loop that waits for a few seconds or using the
Inspector.Activate event or using NewInspector to instantiate a
MailItem object WithEvents and putting your code in that object's Open
event.

I tried this, buit these need to finish before the mail opens
completely

I made a call to a macro that has 1 line in it ....msgbox "test"

Untill i click the ok button the mail dosent' open ...

Itried counting the number of attachments but it gives an error,
becaus the mail is not fullu opened.


What i need is something that start when the email is fully opened
 
S

Sue Mosher [MVP-Outlook]

I guess it might help if you described exactly what you have in mind by "the email is fully opened" and why you need to wait until then.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

I guess it might help if you described exactly what you have in mind by "the email is fully opened" and why you need to wait until then.

i'm trying to get macro to open multiple attachements automatically
when an email is opened.


I used your sugestions to run the macro.

in the macro is the lines (just as a test to see if it works at the
right time.

dim I as interger
I=Application.ActiveInspector.CurrentItem.Attachments.Count
MsgBox I

If i try to run it as you suggest when I double click the email .. it
runs the macro and gives and error because the email window is not
opened.

If i let the email open without running your suggestion and then run
the macro from the Tools/macro menu option ... it works fines and
returns the correct count for the number of attachments


I hope this makes it more clear what i'm trying to acheive
Thanks for your efforts ... I'm new to programming and had this one
dropped on me
 
S

Sue Mosher [MVP-Outlook]

What errror? What's the mail environment? Do you see any different behavior with different format messages?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

What errror? What's the mail environment? Do you see any different behavior with different format messages?


runtime error 91

Object variable or with block variable not st


The code is

Dim i As Integer

i = Application.ActiveInspector.CurrentItem.Attachments.Count
MsgBox i


and at this stage the email window is not opened so that you can read
it.

I get the feeling that the macro is trying to runn way too early in
the email open process

If i wait untill the email is readable then run the macro via the
tools/macro menu in the email window, the code works and returns the
number of attachments.

At the moment I'm just trying to find the correct place to trigger the
macro, then I'll worry about the macro code
 
S

Sue Mosher [MVP-Outlook]

That's why I suggested using either the Inspector.Activate event or the MailItem.Open event, where your Inspectors.NewInspector code would instantiate the Inspector or MailItem object variable that you declare WithEvents. I'd go with MailItem.Open myself.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

That's why I suggested using either the Inspector.Activate event or the MailItem.Open event, where your Inspectors.NewInspector code would instantiate the Inspector or MailItem object variable that you declare WithEvents. I'd go with MailItem.Open myself.


If i get it right the mailitem.open opens a mail from the inbox via
code not via user intervention


I want the user to double click which ever email he wants to open ....
the mail opens THEN the macro runs.

The ide is to open all attachments automatically every time the email
opens


I have several users that get mulitple faxes as picture atttachments.
Once they have opened all they can print them using "select all"
 
S

Sue Mosher [MVP-Outlook]

You might be confusing the MailItem.Open method and the MailItem.Open event. If you declare a MailItem object WithEvents and then instantiate it so that object represents a specific message, then the Open even fires when that message opens on the screen, whether the user does it manually or code does it programmatically. At that point, you should have access to the MailItem object's Attachments collection, but the message will not be displayed yet.

You might find the Inspector.Activate event works better for your scenario. It fires when the Inspector becomes the active window, which means it has displayed.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SuperSlueth

You might be confusing the MailItem.Open method and the MailItem.Open event. If you declare a MailItem object WithEvents and then instantiate it so that object represents a specific message, then the Open even fires when that message opens on the screen, whether the user does it manually or code does it programmatically. At that point, you should have access to the MailItem object's Attachments collection, but the message will not be displayed yet.

You might find the Inspector.Activate event works better for your scenario. It fires when the Inspector becomes the active window, which means it has displayed.


At last it working .... almost.

I need to open 1 email first, then there after it works ok.

Can you suggest a method in code of opening and closing an email.

It can be any mail.

Thanks for all your time
 
S

Sue Mosher [MVP-Outlook]

If you look in the object browser, you'll see that the MailItem object has both Display and CLose methods.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top