Consulting

Results 1 to 13 of 13

Thread: Solved: MailItem Property

  1. #1

    Solved: MailItem Property

    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?

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Hmm, that is wierd...here is my code that basically writes the subject line of the email:

    [VBA]
    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

    [/VBA]
    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!!!

  4. #4
    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.

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by TonyJollans
    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

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by Howard Kaikow
    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).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by TonyJollans
    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.

  9. #9
    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:

    [VBA]public objNewMail as MailItem [/VBA]

    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

  10. #10
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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?
    K :-)

  11. #11
    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

  12. #12
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Killian
    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.

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •