Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Bobbit31 here are your points.

Posted on 2003-10-29
Medium Priority
Last Modified: 2010-04-17
Sorry about that.  Maybe you can help me with this, I have added some addtional points so you aren't getting the shaft.  I have the following script to send emails however everytime I try to use it I get an error that says "User-Defined type not defined" can you help me?

------------------------------  Script  -------------------------------
Public Function fhpSendEmail(strRecip As String, strSubject As String, strMsg As String, Optional strCC As String) As Boolean
' ----------------------------------------------------- '
' Purpose      : Send an e-mail to an Outlook recipient '
' ----------------------------------------------------- '
On Error GoTo Error_fhpSendEmail
  Dim strInfoMsg As String                                      'Info message
  Dim objOutlook As Outlook.Application                         'Outlook variable
  Dim objOutlookMsg As Outlook.MailItem                         'Outlook variable
  Dim objOutlookRecip As Outlook.Recipient                      'Outlook variable
  If (strRecip <> "") And (strSubject <> "") And (strMsg <> "") Then
    Set objOutlook = CreateObject("Outlook.Application")        'Create the Outlook session
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)       'Create the message

    With objOutlookMsg                                          'The message
      Set objOutlookRecip = .Recipients.Add(strRecip)           'Recipient of the message
      objOutlookRecip.Type = olTo

      If strCC <> "" Then                                       'If CC recipients
        Set objOutlookRecip = .Recipients.Add(strCC)            'CC recipients
        objOutlookRecip.Type = olCC
      End If
      .Subject = strSubject                                     'Message subject
      .Body = strMsg & vbCrLf & vbCrLf                          'Message content
      .Importance = olImportanceNormal                          'Message importance

      For Each objOutlookRecip In .Recipients                   'Resolve each Recipient's name
      .Save                                                     'Save message
      .Send                                                     'Send message
    End With
    fhpSendEmail = True                                         'Message sent
  Else                                                          'If not enough information
    strInfoMsg = "Unable to send message because you have supplied to few data!" & vbCrLf & _
                 "Info needed are :" & vbCrLf & _
                 "Recipient" & vbTab & vbTab & "The person to recieve the mail" & vbCrLf & _
                 "Subject" & vbTab & vbTab & "What the mail is about" & vbCrLf & _
                 "Message " & vbTab & vbTab & "The text you want to send"
    MsgBox strInfoMsg, vbCritical + vbOKOnly, "Error sendig e-mail"
    fhpSendEmail = False                                        'Message NOT sent
  End If

  Set objOutlook = Nothing                                      'Release object
  Set objOutlookMsg = Nothing                                   'Release object
  Set objOutlookRecip = Nothing                                 'Release object
  Exit Function

  fhpSendEmail = False                                          'Message NOT sent
  Select Case Err
    Case Else
      MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error in function 'fhpSendEmail'"
      Resume Exit_fhpSendEmail
  End Select

End Function
----------------------------------------  BREAK  ---------------------------------------------
Question by:gideonn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6

Author Comment

ID: 9643484
Oh and it stops here

Dim objOutlook As Outlook.Application

it says that "ObjOutlook as Outlook.Applcation" is not defined however I have already referenced the Outlook 9 OCX.
LVL 18

Expert Comment

ID: 9643492
where do you get the error? (what line?)

Author Comment

ID: 9643511
Sorry it was an after thought.
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

LVL 18

Expert Comment

ID: 9643546
are you sure you referenced Microsoft Outlook Object under project references?

the above code works and compiles fine for me... and the reason it's giving you that error is b/c it doesn't know what Outlook.Application is.

Do the auto-sensing (auto-complete) work if you type Outlook. (dropdown should appear)
LVL 18

Expert Comment

ID: 9643610
also, since you are using latebinding anyway... you could do this instead:

Dim objOutlook As Object                         'Outlook variable
Dim objOutlookMsg As Object                         'Outlook variable
Dim objOutlookRecip As Object                      'Outlook variable

your CreateObject calls will set the object to it's proper type (you won't get the auto-sensing though)
There are advantages and disadvantages b/w using early and late binding... do a search on it in the vb forum and you'll find out more.

one of the important ones is that when you use CreateObject("Outlook.Application") it will find ANY version of Outlook if it's installed.

when you reference Outlook Object 9.0 it's assuming that the client has Outlook 9.0 installed.

b/c the compiler doesn't know what objOutlook etc... is at compile time and how much memory it will need, it adds extra overhead to your app during runtime.


if you reference MS Outlook 9.0 object library and wish to use early binding:

Dim objOutlook As New Outlook.Application

late binding:

Dim objOutlook as Object
Set objOutlook = CreateObject("Outlook.Application")

Author Comment

ID: 9643692
How about this, is there a "Mailto" equivlant in VB.  In other words is there a way to have a command to open a email and fill out the "Subject", "To", and "Main" boxes and allow the user to send it.  That way the user can make changes to the email as needed?  Thanks.
LVL 18

Expert Comment

ID: 9643871
you mean just to launch the default mail client like in html?


hold on let me look ;)

Author Comment

ID: 9643940
Basically but for instance the previous script that I put up is a function and the tool tip for filling it out shows that you can input information in the various fields on the email itself.  (I.E. Subject, To, etc...)  I need something that will allow me to do that but will not automatically send the email and will not require a lot of over head.  I want it to just pull up the blank email fill in what it knows and then the user can do whatever they want.  Does that make sense?  Thanks!
LVL 18

Expert Comment

ID: 9644082
yeah... hold on
LVL 18

Expert Comment

ID: 9644234
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1

Private Sub Command1_Click()
Dim strTo As String
Dim strSub As String
Dim strMsg As String
Dim sFinal As String

strTo = ""
strSub = "Subject (test)"
strMsg = "Message (test)"

sFinal = strTo & "?subject=" & strSub & "&body=" & strMsg

ShellExecute Me.hwnd, "open", "mailto:" & sFinal, vbNullString, vbNullString, SW_SHOWNORMAL

End Sub

Author Comment

ID: 9644301
Method  or data member not found on

LVL 18

Accepted Solution

bobbit31 earned 2000 total points
ID: 9644329
ok, do this:

ShellExecute 0, "open", "mailto:" & sFinal, vbNullString, vbNullString, SW_SHOWNORMAL

Author Comment

ID: 9644354
Thank you so much!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

670 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