PDA

View Full Version : Solved: MailItem Property



samuelwright
11-02-2005, 05:05 AM
Can you define a variable as a MailItem in MS Outlook 2003 declarations? I have tried but to no avail. I.e.

private objNewMail as Mailitem

objNewMail.Subject = blah.......

It says that it is an unrecognised data type

Any thoughts please?

TonyJollans
11-02-2005, 05:23 AM
It should be alright - it works for me.

Just to confirm - you declare objNewMail at the top of the module (outside all the subs) and then you set it to something before trying to use it inside your sub.

samuelwright
11-02-2005, 07:30 AM
Hmm, that is wierd...here is my code that basically writes the subject line of the email:


Dim protectivemarker As String

Dim internetauthorised As String

Dim caveat As String

Dim descriptor As String

Private objNewmail As MailItem ' This line works in Outlook 2000 for me, but the drop down menu doesnt give that option



Private Sub CheckBox3_Click()



If CheckBox3.Value = True Then ComboBox3.SetFocus



End Sub



Private Sub CommandButton1_Click()



UserForm2.Show ' This is a form that allows you to add items to a combobox1



End Sub



Private Sub CommandButton2_Click()





If CheckBox1.Value = True Then internetauthorised = "Internet-Authorised:"

If CheckBox1.Value = False Then internetauthorised = ""



If CheckBox3.Value = True Then descriptor = ComboBox3.Value & "-"

If CheckBox3.Value = False Then descriptor = ""



objNewmail.Subject = internetauthorised & TextBox1.Value & "-" & protectivemarker & "-" & descriptor & TextBox2.Value & "-" & ComboBox1.Value ' This is the important corresponding line.



Unload Me



End Sub



Private Sub CommandButton3_Click()



Unload Me



End Sub





Private Sub UserForm_Initialize()



TextBox1.Value = Format(Date, "yyyymmdd")



Set objNewmail = Application.ActiveInspector.CurrentItem



Dim i As Long



If GetSetting("TESTAPP", "ComboBox1", "EntryCount", "None") <> "None" Then

For i = 0 To GetSetting("TESTAPP", "ComboBox1", "EntryCount", "None") - 1

ComboBox1.AddItem GetSetting("TESTAPP", "ComboBox1", i, "Unable to read entry")

Next

End If



End Sub



Private Sub UserForm_Terminate()



Dim i As Long



'remove old entries first

If GetSetting("TESTAPP", "ComboBox1", "EntryCount", "None") <> "None" Then

DeleteSetting "TESTAPP", "ComboBox1"

End If



SaveSetting "TESTAPP", "ComboBox1", "EntryCount", ComboBox1.ListCount



For i = 0 To ComboBox1.ListCount - 1

SaveSetting "TESTAPP", "ComboBox1", i, ComboBox1.List(i)

Next



End Sub


The user form the above code refers to is called by setting a toolbar button on a New Message to call a macro which is as follows:

sub Subject_naming

UserForm1.Show

end sub

should I be declaring objNewMail in the module instead of the userform?
should I use Private, Public, Dim? I really cannot understand why I cant do it!!!

chocobochick
11-02-2005, 11:13 AM
A couple suggestions:

1. Go to Tools/References from the VB Editor menu and make sure there's a check in the box marked "Microsoft Outlook 9.0 Object Library" (or something similar). If for some reason it's not checked, make it so.

2. Use the Load statement ("Load NameOfUserForm") to start a UserForm, and the Unload statement ("Unload NameOfUserForm") to close it. If you try to close it by using the Hide method, the form still exists in memory. This means the next time you call the same userform, the Initialize event won't occur and your objNewmail reference won't be properly set, causing errors whenever you attempt to use it.

Edit: My apologies, you are using the Unload statement. But throw a Debug.Print statement in your UserForm_Initialize procedure anyway just to make sure it's triggered.

TonyJollans
11-02-2005, 11:14 AM
If you're failing to find the MailItem object type, it doesn't really matter where it fails!!

I can't think of anything bar a faulty installation that would cause MailItem to be unrecognised. If you go to Tools > References, do you see the Outlook Type Library? (it can't be removed from the list by any normal means). Is it flagged as MISSING?

If you have a proper reference, you must have MailItem. You could have a look in the Object Browser (F2), but if it's not there your Type Library is corrupt.

Howard Kaikow
11-02-2005, 10:21 PM
If you're failing to find the MailItem object type, it doesn't really matter where it fails!!

I can't think of anything bar a faulty installation that would cause MailItem to be unrecognised. If you go to Tools > References, do you see the Outlook Type Library? (it can't be removed from the list by any normal means). Is it flagged as MISSING?

If you have a proper reference, you must have MailItem. You could have a look in the Object Browser (F2), but if it's not there your Type Library is corrupt.

It is not possible, unless the OP has discovered a bug, to uncheck the reference to the Outlook library when creating a project within Outlook itself.

It is possible that other references are being used that cause an ambiguity.
Make sure that the Outlook references occur before any non-required references.

Also, to eliminate the latter problem, and it's a good habit to do this anyway, use

Private Benjamin as Outlook.MailItem

instead of

Private Benjamin as MailItem

TonyJollans
11-03-2005, 02:03 AM
It is possible that other references are being used that cause an ambiguity.

How??

Not only is it not possible to uncheck the Outlook library, it is also not possible to move another library above it in the search chain - and even if it were possible it would not cause the MailItem type to be unrecognised.

Why do you say it is good habit (rather than a matter of choice) to fully qualify types with the default library? What do you consider the pros and cons? It can never be ambiguous to the machine (although it can sometimes be to people).

Howard Kaikow
11-03-2005, 03:25 AM
How??

Not only is it not possible to uncheck the Outlook library, it is also not possible to move another library above it in the search chain - and even if it were possible it would not cause the MailItem type to be unrecognised.

Why do you say it is good habit (rather than a matter of choice) to fully qualify types with the default library? What do you consider the pros and cons? It can never be ambiguous to the machine (although it can sometimes be to people).

The position of a library in the search chain depends on the environment in which the code is being used.

In this case, outlook is being used, so no library should be causing a conflict, but if automating outlook from another app, there could be such a conflict.

if 2 libraries define an object with the same name, you must qualify the name to assure the proper object is referenced.

And ALWAYS qualifying the name assures that you do not get in trouble if another library is added or the code is used via automation ftom another app.

It is always better to code to avoid such possibilities.

samuelwright
11-03-2005, 04:07 AM
Thanks for your replies, it seems to be an interesting debate...I have been doing some trial and error experimenting, and have found the following:

If I open the VBA Editor in the Outlook 2003 "frontpage" (where the inbox, calendar, contacts are etc- sorry if I have just made up a name for it!), and then type the following:

public objNewMail as MailItem

Then that works...however, if I do the same after I have created a new message (in effect, creating a seperate macro/piece of VBA code for the new message-seperate from the Outlook "frontpage"), then MailItem is not recognised.

Would it have something to do with using Word 2003 as the default message editor?

Sam

Killian
11-03-2005, 04:29 AM
It certainly doesn't do any harm to make explicit declarations and does make the code portable (as far as automation using early-binding is concerned)

However:
Regarding using Word as the mail editor - well, yes, when you open the VBE with the MailItem open, you may have noticed that you're creating a Word project rather than an Outlook one and therein lies your problem.

If you are adding a toolbar to new mail items, why don't you use the Outlook app's NewMail event to do it?

samuelwright
11-03-2005, 04:40 AM
Howard Kaikows' advice seems to have worked: I have made all references explicit to Outlook now by appending Outlook prior to MailItem. I was creating a word project I will post my code in due course when I have finished it so you can cast your expert eyes over it.. Thank you!

ps I thought about the NewMail event, but on balance, since I eventually hope to distribute this to loads of people, I would still rather they opened a new message as they have always done, and then just have a button to name their subjects...I guess it is just a matter of personal preference

Howard Kaikow
11-03-2005, 05:31 AM
It certainly doesn't do any harm to make explicit declarations and does make the code portable (as far as automation using early-binding is concerned)

However:
Regarding using Word as the mail editor - well, yes, when you open the VBE with the MailItem open, you may have noticed that you're creating a Word project rather than an Outlook one and therein lies your problem.

If you are adding a toolbar to new mail items, why don't you use the Outlook app's NewMail event to do it?

Ayup, a reference to Outlook would need to be added in the Word project.

TonyJollans
11-03-2005, 06:58 AM
Yes, using Word as Outlook editor creates some interesting situations. You do get an automatic reference to the Outlook library but you do have to be explicit when referencing things in it.

If you want your code to run in a Word project you'll have to change some other things as well - for example, you can't use the Outlook ActiveInspector quite so easily.