• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Excel macro error - e-mailing from excel

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
bilalaha
Asked:
bilalaha
  • 5
  • 4
1 Solution
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
bilalahaAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
bilalahaAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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
 
bilalahaAuthor Commented:
Does not work.  Can not replace all the variable definisions with one.  I get errors about variables not defined.  
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
???
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
 
bilalahaAuthor Commented:
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
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Phew ... I really couldn't see what I could have done wrong ... glad it helped.

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now