compile error with sending an email via excel [duplicate] - excel

Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim oMail As String
Dim strbody As String
Dim fdatum As String
Dim VorschauBereich As Range
Dim Tabnr As Integer
Dim Tabtext As String
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(oIMailItem)
I get the error named in the title and cursor jumps to Dim Dim oApp As Outlook.Application

To solve this, you can either use early binding by adding the Mircrosoft Object Library in your references (from the tools menu). Or you can use Late binding by changing the Outlook object declaration to this:
Dim oApp As Object
Dim oMail As Object
and then creating the objects like this:
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(0)
Depending on whether or not your excel WB is used by other people, I prefer late binding to avoid any issues with references on other people's machines. The downside of late binding is that you don't get inteli-text help while coding. Hope this helps!

Tools, References, Available References.
Locate and check Microsoft Outlook xx.x Object Library.

Related

Error declaring a variable as Outlook.MailItem in Excel VBA: user defined type not defined

I get
compile error: user defined type not defined.
The yellow highlight is for this line:
Public Function SendMessage(strSubject, strRecip, strMsg, strAttachment) As Boolean
Blue highlight for this line:
Dim mItem As Outlook.MailItem
Option Explicit
Public Function SendMessage(strSubject, strRecip, strMsg, strAttachment) As Boolean
Dim mOutlookApp As Object
Dim mNameSpace As Object
Set mOutlookApp = GetObject("", "Outlook.application")
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
Dim mFolder As Object
Dim mItem As Outlook.MailItem
Set mItem = mOutlookApp.CreateItem(0)
mItem.To = "Americas"
mItem.CC = strRecip
mItem.SentOnBehalfOfName = "Jordan"
mItem.Subject = strSubject
mItem.Body = strMsg
mItem.Attachments.Add strAttachment
mItem.Display
mItem.Recipients.ResolveAll
End Function
Sub Summarydraft()
Dim result As Boolean
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim LastRow As Long
LastRow = Worksheets("Control").Cells(Rows.Count, "N").End(xlUp).Row
Dim rng As Range, fullrng As Range
Set fullrng = Worksheets("Control").Range("N3:N" & LastRow)
Dim recip As String
For Each rng In fullrng
recip = recip & "; " & rng.Value
Next
strRecip = recip
strSubject = Worksheets("Control").Range("G14")
strMsg = Worksheets("Control").Range("G17")
strAttachment = Worksheets("Control").Range("G20")
result = SendMessage(strSubject, strRecip, strMsg, strAttachment)
End Sub
Either change the line
Dim mItem As Outlook.MailItem
to
Dim mItem As Object
Or add Outlook as a reference in your VBA project.
You need to add an Outlook COM reference to your VBA project if you want to declare Outlook types in the code.
In the code you used the late binding technology. This technology uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook.
To use early binding, you first need to set a reference to the Outlook object library. Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with. You can then use the following syntax to start an Outlook session.
Dim objOL as Outlook.Application
Set objOL = New Outlook.Application
Read more about that in the Automating Outlook from a Visual Basic Application article.

Excel VBA Code Referencing Outlook Results in '287' Run Time Error in Office 2016

I have an Excel VBA script that references Outlook objects. This used to run in prior Office versions.
With Office 2016 and I am getting:
Run-time error '287':
Application-defined or object-defined error
Here is part of my code:
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim Rec As Outlook.Recipient
Dim olGAL As Outlook.AddressList
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olGAL = olNS.GetGlobalAddressList
Set Rec = olNS.CreateRecipient(Cells(1, 1))
Rec.Resolve
Cells(1,1) contains a valid email address.
The '287' error comes in on Rec.Resolve.
I have Microsoft Office 16.0 Object Library checked in Tools/References.
I checked Trust Center Settings in Outlook under Programmatic Access. There are three radio button choices about suspicious activity handling which are all unchecked. My antivirus status is set to Valid.
To make sure you are dealing with a valid string (not empty) I'd suggest declaring a string variable:
Dim recipient as String
Set recipient = Cells(1, 1)
MsgBox recipient
Set Rec = olNS.CreateRecipient(recipient)
Rec.Resolve
Nothing has been changed in the Office or Outlook PIAs. You just needed to replace the Outlook COM referece according to the Outlook version used on a system. The Resolve method remains the same.
You never initialize the olApp variable.
replace the line
Set olApp = Outlook.Application
with
Set olApp = CreateObject("Outlook.Application")

Late binding compile error: User-defined type not defined referencing Outlook mailitem in Excel VBA

I am using late binding from Excel.
I get
Compiler Error: User defined type not defined
for:
If TypeOf olMail Is MailItem Then
I declare the following variables:
Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olItms As Object
Dim olMail As Object
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = Fldr.Items
The code was working but I had to change to late binding because a user is not using the same Outlook version.
I'm guessing the reason you get the error is because you no longer have a reference to the outlook object library, and thus MailItem is an unknown type.
Instead of using TypeOf
If TypeOf olMail Is MailItem Then
use the TypeName function to compare the object type name to a string...
If TypeName(olMail) = "MailItem" Then
(note I'm assuming the type name will be MailItem, but you can always throw in MsgBox TypeName(olMail) just to be sure!)
Type Name function is somewhat expensive. Class property (implemented by all OOM objects) would be a better aalternative. Check that oMail.Class = 43 (43 is OlObjectClass.olMail constant)

User defined type not defined when creating Outlook Object

Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim oMail As String
Dim strbody As String
Dim fdatum As String
Dim VorschauBereich As Range
Dim Tabnr As Integer
Dim Tabtext As String
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(oIMailItem)
I get the error named in the title and cursor jumps to Dim Dim oApp As Outlook.Application
To solve this, you can either use early binding by adding the Mircrosoft Object Library in your references (from the tools menu). Or you can use Late binding by changing the Outlook object declaration to this:
Dim oApp As Object
Dim oMail As Object
and then creating the objects like this:
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(0)
Depending on whether or not your excel WB is used by other people, I prefer late binding to avoid any issues with references on other people's machines. The downside of late binding is that you don't get inteli-text help while coding. Hope this helps!
Tools, References, Available References.
Locate and check Microsoft Outlook xx.x Object Library.

Object Required - error 424 MS ACCESS 2010

I'm really new at VBA but I have this same code in another database, and now I've just copied the code and paste in another database but I get this Object Required error in the last line.
The code is bigger but I've just stopped on the line where I get the error.
Dim frm As Form, ctl As Control
Dim varItm As Variant
Dim stgMO, stgPID, stMail, stgMailCC As String
Dim Question As Long
Dim OutApp, OutMail As Object
Set frm = Forms!Overview
Set ctl = frm!cl_onboarding
stgMO = ctl.Column(7)
stgPID = ctl.Column(2)
stgMail = ctl.Column(8)
stgMailCC = ctl.Column(9)
Question = MsgBox("Do you want to send an e-mail containing the codes for this Agent?", vbYesNo, "Send e-mail")
If Question = vbYes Then
Set OutApp = Outlook.Application
you have dim stMail as a variable but then you use stgMail. just check your spelling.
You also use dim question as long, this confuses me a bit because I thought long meant an integer (there are min and max values but I cant remember).
You need to set a Reference in VBA to Outlook.
VBA editor -> menu Tools -> References
Select and check Microsoft Outlook 14.0 Object Library
Edit: while the above is true, you would probably get a different error if the reference was missing.
The problem may be in this line:
Dim OutApp, OutMail As Object
which actually gets evaluated as
Dim OutApp As Variant, OutMail As Object
and should read
Dim OutApp As Object, OutMail As Object
But a Variant can hold an object too, so this may also not be the cause of the error.

Resources