Solved

Excel macro error - e-mailing from excel

Posted on 2011-02-21
9
365 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

13 Experts available now in Live!

Get 1:1 Help Now