Solved

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

Posted on 2001-06-08
7
178 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Szrachen
Comment Utility
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
Comment Utility
Thanks for your help!
0
 
LVL 3

Expert Comment

by:jrspano
Comment Utility
No problem! glad I could help.
0
 

Author Comment

by:Szrachen
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

772 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

15 Experts available now in Live!

Get 1:1 Help Now