?
Solved

Excel macro error - e-mailing from excel

Posted on 2011-02-21
9
Medium Priority
?
382 Views
Last Modified: 2012-05-11
Hello Experts,

I am not an expert and know very little about Visual basic, so I need step by step help.  here is the problem:

I have an excel spreadsheet with a macro to e-mail a file and use a template stored in the draft in outlook 2003.  I have used this before abd all was working.  Now when I usde it, I get a Run-Time eror'13': Type mismatch.  When I select debug, Microsoft Visual Basic Editor opens up and Next is Highlighted.  I press continue, everything works.  It has something to do with excel trying to access outlook and send mail.  Code is below with the hi
Sub EmailData(EmailAddr As String, FilePath As String, Filename As String, _
    Subject As String, Body As String)

' Set reference to:
'Microsoft Outlook 9.0 Object Library
'C:\Program Files\Microsoft Office\Office\msoutl9.olb

Dim olApp As Outlook.Application, myItem As Outlook.MailItem
Dim SafeItem As Object, mapiUtils As Object
Dim olMailItem As Integer

Dim appOL As Outlook.Application
Dim nmsNameSpace As Outlook.NameSpace
Dim fldFolder As Outlook.MAPIFolder
Dim itm As Outlook.MailItem


Set appOL = CreateObject("Outlook.application")
Set nmsNameSpace = appOL.GetNamespace("MAPI")

Set fldFolder = nmsNameSpace.GetDefaultFolder(olFolderDrafts)
Set olApp = CreateObject("outlook.application")
   

olMailItem = 0

'Looking in the Draft folder set the item to be sent equal to the message
'with the subject = Template
'This allows you to create a boilerplate HTML message (with a signature)

For Each itm In fldFolder.Items

    If itm.Subject = "Template" Then
        Set myItem = itm.Copy
        myItem.HTMLBody = itm.HTMLBody
        'myItem.Save
       
        Exit For
       
    End If
 
Next'
' This procedure is the guts of the email routine.
' The email properties are populated, and depending on
' whether Redemption is loaded, specific code sends the
' to Outlook.
'
    olMailItem = 0
    'Set olApp = CreateObject("outlook.application")
    'Set myItem = olApp.CreateItem(olMailItem)
    myItem.Subject = Subject
    myItem.To = EmailAddr
    'myItem.Body = Body

    If Filename <> "" Then myItem.Attachments.Add FilePath & Filename
    myItem.NoAging = True
'    If Workbooks("ShellEmail.xls").Worksheets("Variables"). _
        Range("RedLoaded") Then
        ' Redemption is installed- use Redemption code to send emails.
      '  myItem.Save
     '   Set SafeItem = CreateObject("Redemption.SafeMailItem")
    '    SafeItem.Item = myItem
   '     SafeItem.send
  '      Set mapiUtils = CreateObject("Redemption.MAPIUtils")
 '       mapiUtils.DeliverNow
'    Else
        ' Redemption is not installed.
        'myItem.send
'    End If
    'myItem.send
    myItem.display
   
    Set myItem = Nothing
    Set SafeItem = Nothing
    Set olApp = Nothing
End Sub


0
Comment
Question by:bilalaha
  • 5
  • 4
9 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34950458
Try a check to see if it is a mail item replace your loop as below to do this it adds a check to the if statement.

Chris
For Each itm In fldFolder.Items

    If itm.Subject = "Template" and itm.class = olmail Then
        Set myItem = itm.Copy
        myItem.HTMLBody = itm.HTMLBody
        'myItem.Save
        
        Exit For
        
    End If
 
Next'

Open in new window

0
 
LVL 2

Author Comment

by:bilalaha
ID: 34951109
I still get the same error and when I continue, I debug and in the editor, I continue at the break and it send s the e-mail with the attachment using the template in the draft.  

Any other thoughts on this?  Any other program that is is out ther that will do what i need to do?

I need to e-mail a message with attachment.  The attachment is different for each person.  The message is the same and there is no mail merger in the message.  

Thanks in advance for all the help i can get.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34951220
OK I am seeing a different understanding.  Add to the code I gave you ...

Dim itm As Outlook.MailItem
with
Dim itm As object

Any improvement now?

Chris
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:bilalaha
ID: 34951264
Chris,

As i stated at the beginnig, I know very little about VB, so I do not know where to put the code you gave me.  If you update the code I am using and insert you updates in and past it for me, then I will replace the code I have.

Thanks,

Bill
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 34951442
Just looking at the section where you finish with next i.e.

starts with:
    Dim olApp
Ends with:
Next'

Then replace that block with the above

Chris
Dim olApp As Outlook.Application, myItem As Outlook.MailItem
Dim SafeItem As Object, mapiUtils As Object
Dim olMailItem As Integer

Dim appOL As Outlook.Application
Dim nmsNameSpace As Outlook.NameSpace
Dim fldFolder As Outlook.MAPIFolder
Dim itm As object


Set appOL = CreateObject("Outlook.application")
Set nmsNameSpace = appOL.GetNamespace("MAPI")

Set fldFolder = nmsNameSpace.GetDefaultFolder(olFolderDrafts)
Set olApp = CreateObject("outlook.application")
    

olMailItem = 0

'Looking in the Draft folder set the item to be sent equal to the message
'with the subject = Template
'This allows you to create a boilerplate HTML message (with a signature)


For Each itm In fldFolder.Items

    If itm.Subject = "Template" and itm.class = olmail Then
        Set myItem = itm.Copy
        myItem.HTMLBody = itm.HTMLBody
        'myItem.Save
        
        Exit For
        
    End If
 
Next'

Open in new window

0
 
LVL 2

Author Comment

by:bilalaha
ID: 34954173
Does not work.  Can not replace all the variable definisions with one.  I get errors about variables not defined.  
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34954567
???
The block I posted is a duplicate of your own with the change around the definition of itm so that is still defined and the added check in the if construct.  Eveything is still defined similar to  before so can you provide more precise information on the issue?

Chris
0
 
LVL 2

Author Comment

by:bilalaha
ID: 34961043
Thanks Chris.  I wnt back and copied the code you sent and replaced the code I have and It worked.  I must have done something to get an error.

Thanks again.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34961266
Phew ... I really couldn't see what I could have done wrong ... glad it helped.

Chris
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses

850 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