Log in

View Full Version : email function word 2000/2003



davidboutche
07-30-2009, 06:43 AM
I've been using the function from thread http://www.vbaexpress.com/forum/showthread.php?t=27538

The function takes information from bookmarks and inserts them into an email ready to be sent.

It works fine on my machine as I have office 2003 and I have set in the reference library Microsoft Outlook 11 i think.

This also works when using Office 2000 by selecting outlook 9.

The problem is the word application is used on both computers with 2000 and others with 2003.

Is it possible to overcome this problem without usin two different versions of the template?

Option Explicit
Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "A Caution Certificate has been issued"
.Body = "A " & ActiveDocument.Bookmarks("appbook").Range.Text & " has been issued to:" & vbCrLf & "Surname: " & ActiveDocument.Bookmarks("surnamebook").Range.Text & vbCrLf & "First Names: " & ActiveDocument.Bookmarks("forenamebook").Range.Text & vbCrLf & "Address: " & ActiveDocument.Bookmarks("addressbook").Range.Text & vbCrLf & "Post Code: " & ActiveDocument.Bookmarks("postcodebook").Range.Text & vbCrLf & "DOB: " & ActiveDocument.Bookmarks("dobbook").Range.Text & vbCrLf & "Sex: " & ActiveDocument.Bookmarks("sexbook").Range.Text & vbCrLf & "Age: " & ActiveDocument.Bookmarks("agebook").Range.Text _
& vbCrLf & "Ethnicity: " & ActiveDocument.Bookmarks("ethnicitybook").Range.Text & vbCrLf & "Place of Birth: " & ActiveDocument.Bookmarks("pobbook").Range.Text & vbCrLf & "Occupation: " & ActiveDocument.Bookmarks("occupationbook").Range.Text & vbCrLf & "Custody Number: " & ActiveDocument.Bookmarks("custodybook").Range.Text _
& vbCrLf & vbCrLf & "Offence Details" & vbCrLf & vbCrLf & "Offence: " & ActiveDocument.Bookmarks("offencebook").Range.Text & vbCrLf & "Time,Date and Location of Offence: " & ActiveDocument.Bookmarks("datebook").Range.Text & vbCrLf & "Crime Number: " & ActiveDocument.Bookmarks("crimebook").Range.Text _
& vbCrLf & "Additional Offence: " & ActiveDocument.Bookmarks("secondoffencebook").Range.Text & vbCrLf & "Time,Date and Location of second offence: " & ActiveDocument.Bookmarks("seconddatebook").Range.Text & vbCrLf & "Second Crime Number: " & ActiveDocument.Bookmarks("secondcrimebook").Range.Text _
& vbCrLf & vbCrLf & "Rank: " & ActiveDocument.Bookmarks("adminrankbook").Range.Text & vbCrLf & "Collar Number: " & ActiveDocument.Bookmarks("admincollarbook").Range.Text & vbCrLf & "Station Code: " & ActiveDocument.Bookmarks("adminstationbook").Range.Text & vbCrLf & "Date Administered: " & ActiveDocument.Bookmarks("admindatebook").Range.Text
.To = ".Non Court Disposals"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub
:help

lucas
07-30-2009, 08:11 AM
David, you should investigate late binding. I just posted some informative links on the subject at this thread. I hope it helps or at least gives you enough info to ask futher questions.

http://www.vbaexpress.com/forum/showthread.php?t=27841

davidboutche
07-30-2009, 10:24 AM
well having read those documents, particularly dicksclicks, I think I understand the difference between early and late binding.

What I am having trouble with, is the code that I've used for my email procedure looks like it uses late binding... does it not?

I understand that early binding uses the ref library in the design stages and late codes it in. My code seems to use 'CreateObject and CreateItem' as in the example?

lucas
07-30-2009, 01:03 PM
One way to be sure is to copy the code to a new document and try to run it. You will find that it returns an error stating that some variables are not defined. If you then add the reference it will run......so the answer is that the code is using early binding.

davidboutche
07-31-2009, 01:42 AM
OK, I think I finally got there..

I think I was using late binding after all.

I excluded the fist line of
option explicit

and removed the early binding references. I'm not sure which of these did the trick, my guess is the early reference to outlook 11.

Seems to work now on both office 2000 and 2003

And I think I understand early and late bindings!:friends:

lucas
07-31-2009, 06:52 AM
Or you could have and should have left Option Explicit in the code and defined your variables.

If it won't run with Option Expicit at the top of the code then something is wrong, that is what the statement does, make sure you dim all variables and shows errors.

fumei
07-31-2009, 12:18 PM
Agreed. Do NOT remove Option Explicit!

The problem is this. Because you are using late-binding VBA does not know what olMailItem, or olImportanceNormal, mean. When you use late-binding you must use the actual values, after all olMailItem refers to.......nothing, because there IS no reference.

Look up what the numeric value of olMailItem (or any other referenced/named item) in ObjectBrowser.

It is 0 BTW. olImportanceNormal = 1

Keep Option Explicit! In the long run it will be your friend.

Also, please...use the underscore character to break up extended lines of code.

davidboutche
07-31-2009, 01:27 PM
I think I understand where you're coming from.

I don't have 'word' with me at the moment so I can't test it out till Sunday but I think what you're saying is :

Set EmailItem = OL.CreateItem(olMailItem)
'should read
Set EmailItem = OL.CreateItem(0)

'and

.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow

'should read

.Importance = 1

as these are the actual values in the outlook model? Am I getting this?

geekgirlau
08-06-2009, 10:50 PM
These are constants used by Outlook. Because you are using late binding, vba has no way of knowing what those constants actually mean, because there is no reference to Outlook.

With some constants you can find the numeric value in Help, otherwise you need to ask Mr Google.