After Insert automatic email

K

Kevin

I am trying to generate an automatic email after a users enter a record via a
form. I have the following code in the After Insert Event:


Private Sub Form_AfterInsert()

Set O = CreateObject("Outlook.Application")

Set m = O.CreateItem(0)
m.To = "Slezewski, Kevin"
m.Subject = "Finish Sample Request Entered"
m.Body = Chr(13) & "JobNumber"
m.display

End Sub

I currently get a Compile Error: Variable not defined and it highlights the
first "O =" in the code. I am not sure what variable I should be defining.
I don't even truly know if the rest of the code will achieve what I intend it
to, so any other suggestions are appreciated.

Also of note...I have it set to "display" the email only for the testing of
the code, but plan to change that to "send" when I implement. I believe I
should be using DoCmd.SendObject for that. As mentioned, any help is greatly
appreciated!
 
S

Stefan Hoffmann

hi Kevin,
I currently get a Compile Error: Variable not defined and it highlights the
first "O =" in the code.
Take a look at the first lines of your module. When you will find there an

Option Explicit

you have to declare any variable your are using in your code. So you
have to do for your variable O:

Private Sub Form_AfterInsert()

' Declare variable before usage:
Dim O As Object

Set O = CreateObject("Outlook.Application")
...

End Sub


mfG
--> stefan <--
 
K

Kevin

Thanks for the response! I did as you listed below and it got that part of
the code working properly, however I now get the same compile error and it
highlights "m =". I tried adding: Dim m As Item, but it resulted in another
error. It seems as though I have to declare the variable there also, but I
have no idea how to do that.

Any continued help is appreciated!
Slez
 
S

Stefan Hoffmann

hi Kevin,
Thanks for the response! I did as you listed below and it got that part of
the code working properly, however I now get the same compile error and it
highlights "m =". I tried adding: Dim m As Item, but it resulted in another
error. It seems as though I have to declare the variable there also, but I
have no idea how to do that.
Now you are mixing to techniques for using external libraries:

Late binding

Dim O As Object
Set O = CreateObject("Outlook.Application")

The type of the object O will be defined at runtime through the
CreataObject() method.

Using late binding, you don't need to set references to the used library.
The code will compile even when the library is not present or of an
incompatible version. Invalid parameter list, wrong spelled method names
will not be detected.

Early binding

Dim m As Item

Here the compiler must already know the type Item.

Using early binding you have to set a reference to the used library in
the VBA editor (in the english VBA IDE, this should be Extras / References).
Invalid parameter list, wrong spelled method names will be detected.
Pressing F2 in the VBA IDE will reveal all methods and properties of a
library, when you select it.

So use

Dim m As Object ' for late binding.

or

Dim O As Outlook.Application
Dim m As Outlook.Item

and set a reference to Microsoft Outlook.

mfG
--> stefan <--
 
Top