Solved

Bobbit31 here are your points.

Posted on 2003-10-29
13
211 Views
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
        objOutlookRecip.Resolve
      Next
     
      .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

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

Error_fhpSendEmail:
  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  ---------------------------------------------
0
Comment
Question by:gideonn
  • 7
  • 6
13 Comments
 
LVL 1

Author Comment

by:gideonn
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.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9643492
where do you get the error? (what line?)
0
 
LVL 1

Author Comment

by:gideonn
ID: 9643511
Sorry it was an after thought.
0
 
LVL 18

Expert Comment

by:bobbit31
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)
0
 
LVL 18

Expert Comment

by:bobbit31
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.

However,
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.

basicially:

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")
                       
0
 
LVL 1

Author Comment

by:gideonn
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Expert Comment

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

mailto:blah@aol.com

that?

hold on let me look ;)
0
 
LVL 1

Author Comment

by:gideonn
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!
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9644082
yeah... hold on
0
 
LVL 18

Expert Comment

by:bobbit31
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 = "blah@aol.com"
strSub = "Subject (test)"
strMsg = "Message (test)"

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

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

End Sub
0
 
LVL 1

Author Comment

by:gideonn
ID: 9644301
Method  or data member not found on

Me.hwnd
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 500 total points
ID: 9644329
ok, do this:

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

Author Comment

by:gideonn
ID: 9644354
Thank you so much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

706 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

19 Experts available now in Live!

Get 1:1 Help Now