?
Solved

Possible reasons why this short code isn't working in Outlook 2007

Posted on 2009-02-22
7
Medium Priority
?
172 Views
Last Modified: 2012-05-06
Any ideas about why this simple code would't run with Outlook 2007? I have gmail IMAP inmail, so I am seeking advice about other reasons for code not running.

There are no error messages. Nothing happens when I run it from the macro dialog box.

Thank you.

The code is taken from  the Absolute Beginner's Guide to VBA which has proven helpful to me. It is supposed to add an address, subject and message body to an email when I press the New Mail button.
Sub SendAMessage()
        Dim mi As MailItem
        Set mi = Application.CreateItem(olMailItem)
        
        Dim ns As NameSpace
        Dim msg As MailItem
        
        'Set up the namespace
        
        Set ns = ThisOutlookSession.Session
        
        'Create the new MailItem
        
        Set msg = Application.CreateItem(olMailItem)
        
        'Specify the recipient, subject, and body
        'and then send the message
        
        With msg
        
        'Adjust the following address!
        
        .Recipients.Add "elkodog@hotmail.com"
        .Subject = "Just Testing"
        .Body = "This is only a test of Section 11.4"
        .Send
    
    End With
 
End Sub

Open in new window

0
Comment
Question by:9XqUwH3S
  • 4
  • 3
7 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 23705200
Nothing obviously wrong that I can see.  Are you calling it from outlook or somewhere else?

Chris
0
 

Author Comment

by:9XqUwH3S
ID: 23705252
Hi Chris,
Yes, calling from Outlook Macro dialog box.
I checked trusted settings stuff. Do you think I need a certificate to run this? I presume, not.
If any thoughts come to mind, please post. I'll keep this question open for a day or two.
The thing is that this all seems so straight-forward.
Okay, well, I may be able to think of additional information as I go about my business. Thank you for responding.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 23705358
Are macros enabled, I presume so otherwise you should have got an error message.  Open the VBE, alt + f11.

Select the module containing your code and on teh first line:

"Set mi = Application.CreateItem(olMailItem)" press F9 to set a break point.  The line should highlight, now in outlook run the macro and execution should stop at the selected line .. if not it isn't running otherwise we can step through with F8 to find when it fails.

Chris
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:9XqUwH3S
ID: 23705502
Is this what was anticipated?
I'm not sure where you're going with this.


Set mi = Application.CreateItem(olMailItem)
Sub SendAMessage()
        Dim mi As MailItem
        Set mi = Application.CreateItem(olMailItem)
        
        Dim ns As NameSpace
        Dim msg As MailItem
        
        'Set up the namespace
        
        Set ns = ThisOutlookSession.Session
        
        'Create the new MailItem
        
        Set msg = Application.CreateItem(olMailItem)
        
        'Specify the recipient, subject, and body
        'and then send the message
        
        With msg
        
        'Adjust the following address!
        
        .Recipients.Add "elkodog@hotmail.com"
        .Subject = "Just Testing"
        .Body = "This is only a test of Section 11.4"
        .Send
    
    End With
 
End Sub

Open in new window

Capture.JPG
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 23705728
Sort of.

1. That first line set mi = ... is outside a sub and invalid so should be deleted ... or at least commented out.

2. The line I referred would be line 4 in the snippet as posted by you.

3. Where i'm going is to see if the code is executing since the code itself is fine so just need the specifics of the operational environment.

Therefore if the code breaks, it is being executed otherwise it isn't and one way or the other we can resolve the problem.

Chris
0
 

Author Closing Comment

by:9XqUwH3S
ID: 31549781
Oops. The solution WAS complete, accurate and easy to understand. I corrected the feedback with the accept answer dialog interactive.
One last query, here: I'm determined to get the 'flow' of this VBA stuff but am quite new. Can I ask 'Why?' this omission solved the problem?
This is really cool, Chris. Thanks for my mini-education at Experts-Exchange!

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 23708548
I am happy to be corrected but as I understand VBA when executed 'compiles' the code.  In this way any invalid lines prevent the code from running ... as might have been the case here.

In a VBS, then you start with main code outside a sub and subs for anyything else defined as blocks but in VBA it all resides in subs/functions, (well public declarations aside anyway).

Chris
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Take a look at these 6 Outlook Email management tools which can augment the working and performance of Microsoft Outlook to give you a more rewarding emailing experience.
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question