Bobbit31 here are your points.

Posted on 2003-10-29
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
  • 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.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

837 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