Solved

Creating a new Outlook e-mail from Excel that contains data from the Excel spreadsheet

Posted on 2001-06-08
7
179 Views
Last Modified: 2010-05-02
What I want to do is use a CommandButton to make it so that when a user clicks on the button (which is in Excel2000), an e-mail message in Outlook 2000 is created that contains data from the Excel spreadsheet and also the Path of the Excel workbook. Below is what I have been using per Microsoft's KnowledgeBase (http://msdn.microsoft.com/library/officedev/off2000/olconWorkingWithFormsInVisualBasicEditor.htm), but it doesn't seem to work correctly, any suggestions?

This code is within Sheet1

Private Sub CommandButton1_Click()
    Dim myMail As Outlook.MailItem
    Set myMail = Application.CreateItem(olMailItem)
    With myMail
        .To = "email@domain.com"
        .Subject = "Subject"
        .Body = "This cell contains: " & Sheet1.Range("G18").Value & "The path to the workbook is: " & ActiveSheet.PageSetup.CenterFooter = ThisWorkbook.FullName
    End With
End Sub

I seem to get an error at the point that says "myMail As Outlook.MailItem" and I'm wondering if this is an incorrect Object name or something. Maybe I need to declare this as the Compile error message that I get is "User-defined type not defined".

I appreciate any insight that anyone may provide.
0
Comment
Question by:Szrachen
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:jrspano
ID: 6169071
try this
you have to have a application obj first and namespace object


   Sub NewMailMessage()
    Dim ol As New Outlook.Application
    Dim ns As Outlook.NameSpace
    Dim newMail As Outlook.MailItem

    'Return a reference to the MAPI layer.
    Set ns = ol.GetNamespace("MAPI")

    'Create a new mail message item.
    Set newMail = ol.CreateItem(olMailItem)
    With newMail
        'Add the subject of the mail message.
        .Subject = "Training Information for October 1997"
        'Create some body text.
        .Body = "Here is the training information you requested:" & vbCrLf

        'Add a recipient and test to make sure that the
        'address is valid using the Resolve method.
        With .Recipients.Add("mindym@imginc.com")
            .Type = olTo
            If Not .Resolve Then
                MsgBox "Unable to resolve address.", vbInformation
                Exit Sub
            End If
        End With

        'Attach a file as a link with an icon.
        With .Attachments.Add _
            ("\\Training\training.xls", olByReference)
            .DisplayName = "Training info"
        End With

        'Send the mail message.
        .Send
    End With

    'Release memory.
    Set ol = Nothing
    Set ns = Nothing
    Set newMail = Nothing
End Sub

0
 

Author Comment

by:Szrachen
ID: 6172289
I tried using your code and changing it to suit my needs. Here is the current code right now and I still get an error on the "ol As New Outlook.Application" part that says, "User-defined type not defined."
Here is the code:

Private Sub CommandButton2_Click()
   Dim ol As New Outlook.Application
   Dim ns As Outlook.NameSpace
   Dim newMail As Outlook.MailItem

   'Return a reference to the MAPI layer.
   Set ns = ol.GetNamespace("MAPI")

   'Create a new mail message item.
   Set newMail = ol.CreateItem(olMailItem)
   With newMail
       'Add the subject of the mail message.
       .Subject = "Worksheet"
       'Create some body text.
       .Body = "Here is the link to the :" & ThisWorkbook.FullName

       'Add a recipient and test to make sure that the
       'address is valid using the Resolve method.
       With .Recipients.Add("e-mail@e-mail.com")
           .Type = olTo
           If Not .Resolve Then
               MsgBox "Unable to resolve address.", vbInformation
               Exit Sub
           End If
       End With

       'Send the mail message.
       .Send
   End With

   'Release memory.
   Set ol = Nothing
   Set ns = Nothing
   Set newMail = Nothing
End Sub

Thanks again for your help.
0
 
LVL 3

Accepted Solution

by:
jrspano earned 50 total points
ID: 6176949
this might be a stupid question but... you do have outlook installed on your development machine don't you?  also have you gone to tools/references and referenced microsoft outlook 9.0 object library?  this would cause that also
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Szrachen
ID: 6178634
Not a stupid question at all. Yeah, I have it installed. The Outlook 9.0 object library was the solution to my problem. It now works correctly. Thank you for your help!
0
 

Author Comment

by:Szrachen
ID: 6178656
Thanks for your help!
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6178685
No problem! glad I could help.
0
 

Author Comment

by:Szrachen
ID: 6179321
Is it also possible to Underline text in the Body section of the e-mail or format the information in the Body section somehow?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now