Solved

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

Posted on 2001-06-08
7
180 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

770 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