Results 1 to 22 of 22

Thread: Mail through code "Strange"

  1. #1

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Mail through code "Strange"

    Is it possible to retrieve the adress book through code ??

    I am using the mapiMessages and MapiSession object to send email through code.

    I need to append addresses to the Personal Adress Book also.
    Last edited by swatty; Oct 23rd, 2002 at 08:20 AM.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    I use a ListView. You can substitute to fit your needs

    VB Code:
    1. 'Set A Reference to the Microsoft Outlook Object Library
    2.  
    3. Private olApp As Outlook.Application
    4. Private olNS As Outlook.NameSpace
    5. Private olAL As Outlook.AddressList
    6. Private olAE As Outlook.AddressEntry
    7. Private olMail As Outlook.MailItem
    8.  
    9. Private Sub Form_Load()
    10. Dim Counter As Long
    11. Set olApp = New Outlook.Application
    12.   Set olNS = olApp.GetNamespace("MAPI")
    13.   For Each olAL In olNS.AddressLists
    14.     For Each olAE In olAL.AddressEntries
    15.       DoEvents
    16.       Counter = Counter + 1
    17.       Label1.Caption = Counter
    18.       ListView1.ListItems.Add , , olAE.Name
    19.       ListView1.ListItems(ListView1.ListItems.Count).SubItems(1) = olAE.Address
    20.     Next
    21.   Next
    22. End Sub
    23.  
    24. Private Sub ListView1_Click()
    25. Set olMail = olApp.CreateItem(olMailItem)
    26.   olMail.Recipients.Add  ListView1.SelectedItem.Text
    27.   olMail.Subject = "Test"
    28.   olMail.Body = "Test"
    29.   olMail.SendSet olMail = olApp.CreateItem(olMailItem)
    30.   olMail.Recipients.Add ListView1.SelectedItem.Text
    31.   olMail.Subject = "Test"
    32.   olMail.Body = "Test"
    33.   olMail.Send
    34. End Sub

  3. #3

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    I need to be able to add some addresses to the address book.

    Could it be possible if so how ?
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Taken from the help system in Outlook:


    This VBScript example uses the Click event of a CommandButton. The name in the "To" field of the form is located in the Global Address List as well as the manager for that person. Both of these entries are added to the sender's Personal Address Book using the Add method.
    VB Code:
    1. Sub CommandButton1_Click()
    2.     myName = Item.To
    3.     Set myNameSpace = Application.GetNameSpace("MAPI")
    4.     Set myGAddressList = myNameSpace.AddressLists("Global Address List")
    5.     Set myGEntries = myGAddressList.AddressEntries
    6.     Set myGEntry = myGEntries(myName)
    7.     myManager = myGEntry.Manager    
    8.     Set myGEntry2 = myGEntries(myManager)
    9.     Set myPAddressList = myNameSpace.AddressLists("Personal Address Book")
    10.     Set myPEntries = myPAddressList.AddressEntries
    11.     'Add a new AddressEntry object to the personal
    12.     'address collection with the name, address, and
    13.     'manager of the name in your To field.
    14.     Set myPEntry = myPEntries.Add("Microsoft Mail Address", myName)
    15.     myPEntry.Address = myGEntry.Address
    16.     myPEntry.Manager = myGentry.Manager
    17.     'Update to persist the collection.
    18.     myPEntry.Update
    19.     'Now add the manager's info. to
    20.     'the Personal address collection.
    21.     Set myPEntry2 = myPEntries.Add("Microsoft Mail Address", myManager)
    22.     myPEntry2.Address = myGEntry2.Address
    23.     myPEntry2.Manager = myGentry2.Manager
    24.     myPEntry2.Update
    25. End Sub

  5. #5

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Still trying.

    Chris with the code you provided i get an error
    "The requested operation cannot be completed because the operation is not supported on this object."

    seems like it won't allow me to add recipients to the Global Adress List.

    tried it on the Public Folders same error.


    There is no Personal Address Book available.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  6. #6
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    I wouldn't expect you to be able to add things to the Global Address List - you certainly can't as a normal user in Outlook / Exchange.

    If you want to add an address, you will need to add it to Contacts or a Personal Address Book.

    If you want to modify an address of an existing entry, you will need read / write / edit permissions on the object in the GAL to be able to do it.

  7. #7

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Just found it out i have no rights to change or add items in any adress book.

    Will talk to my boss to get more rights to check this out .

    Thanks anyway.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  8. #8

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Got it.

    If no 'Personal Address Book' is made on that machine you cannot add entries to it (obvious).

    Should it be possible to add this address book (if it is not known on the machine) ?
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Through the normal Outlook UI - yes its easy.

    Through code, I am not sure.
    There is a way of adding new Personal Folder stores:
    VB Code:
    1. Dim myOlApp As New Outlook.Application
    2. Dim myNS As Outlook.NameSpace
    3. Set myNS = myOlApp.GetNamespace("MAPI")
    4. myNS.AddStore "c:\my documents\" & myNS.CurrentUser & ".pst"
    but I haven't found an equivalent "AddAddressList".

    Can you have an empty PAB file, and if the user doesn't have one copy it to the user and utilise it? Or will it still not recognise it until its specified in the profile?
    Maybe just recognise the user doesn't want to have personal addresses, and tell them to create a PAB if they want your application.

  10. #10

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    That is what I am doing now.

    They can add known addresses from the client table to the PAB.

    If i don't find a PAB file i show a messagebox that no 'Personal Address Book' is known for this user.

    So they can specify it first.

    Is it possible to fill in all contents and show the email to be send without sending it through code ??

    For now I am sending it through code without user action but I prefer the user click the send button , so he still knows what he is sending. Can this be easaly done ?

    Thanks anyhow for the great help.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  11. #11

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Silly me
    message.send True
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  12. #12
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Is it possible to fill in all contents and show the email to be send without sending it through code ??
    You should be able to do:

    MessageObject.Display

    once you have put everything you want into the message.

  13. #13

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    With the line messagge.Send True it is resolved (showing the mail to the user)


    Now i got a strange thing happening.

    Playing around with it I can
    make a total mail with everything entered even a file attached to it.

    But , now when the Session.SignOn is fired a messagebox pops up to choose the profile ??

    Where did this come from , didn't had this earlier.

    Please if you know can you point a direction for me.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  14. #14
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    I have only found that the profile prompt can be turned on or off via the standard Outlook User Interface.

    Take a look at (for Outlook 98 / 2000) Tools, Options, Mail Services tab. Near the top is a "prompt for profile" option.

    HOWEVER, if you are now getting one of these when you didn't get one before, maybe there is a way of doing it via code!

  15. #15

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    The prompt for profile isn't set.

    Yesterday I could send mails through code without this message.

    To let me add addresses in the adress book I had to have a Personal Address Book, to get this all working i messed a bit with my outlook.

    Now if i want to send a mail wiith attachment and all it works wihtout this messagebox if outlook is running.
    If outlook is not running this profile thing comes up first.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  16. #16
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Maybe there isn't a default profile set?

    When you right click on the Outlook icon and choose properties. Can you do a Show Profiles and make sure that there is a default set at the bottom of the screen. (Of course - just opening this window might set one up - so make sure you OK out rather than cancel).

    Also: Try showing us the sign-on / login / first connection code you have for Outlook. There may be a switch / default setting that needs to be applyed.

  17. #17

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    VB Code:
    1. DoCmd.Hourglass True
    2.    
    3.    ' Message to send with the mail
    4.    msg = "Dit bericht is verstuurd vanuit code." & vbCrLf & "Er kunnen geen items toegevoegd worden aan het adresboek," & _
    5.       vbCrLf & "als er geen 'Personal Address Book' aanwezig is." & vbCrLf
    6.      
    7.    ' login and password can be given with it
    8. '   mapSessie.Password = ""
    9. '   mapSessie.UserName = ""
    10.    
    11.    ' if there isn't an nt login known or not automaticly logged in
    12. 'with it a username and password will be asked.
    13.    mapSessie.SignOn
    14.    
    15.    mapBericht.SessionID = mapSessie.SessionID
    16.    'Compose new message
    17.    mapBericht.Compose
    18.  
    19. '   mapBericht.Show ' show address book
    20. '   MsgBox mapBericht.AddressModifiable '
    21. '   mapBericht.AddressModifiable = True '    
    22.  
    23.    mapBericht.RecipDisplayName = "Girly"
    24.    mapBericht.RecipAddress = "girly"
    25.    
    26.    
    27.    ' Test if address is known
    28.    mapBericht.AddressResolveUI = True
    29.    mapBericht.ResolveName
    30.    
    31.    'make rest of message
    32.    mapBericht.MsgSubject = "Status Report : from " & Time
    33.    mapBericht.MsgNoteText = msg & "Test op email versturen via code." & vbLf & "Build successful!" & vbLf & Time
    34.    'Send the message
    35.    
    36.    pad = Mid$(CurrentDb.Name, 1, Len(CurrentDb.Name) - InStr(1, StrReverse(CurrentDb.Name), "\") + 1)
    37.    If frmSoort.Value <> 1 Then
    38.       pad = pad & "Verkoop.rtf"
    39.       DoCmd.OutputTo acOutputReport, "vfaktuur_doc", acFormatRTF, pad
    40.    Else
    41.       pad = pad & "Verkoop.snp"
    42.       DoCmd.OutputTo acOutputReport, "vfaktuur_doc", acFormatSNP, pad
    43.    End If
    44.    
    45.    mapBericht.AttachmentPathName = pad
    46.    ' add attachment
    47.    
    48.    mapBericht.Send True
    49.    Kill (pad)
    50.    
    51. mail_Exit:
    52.    On Error Resume Next
    53.    
    54.    mapSessie.SignOff
    55.    DoCmd.Hourglass False
    56.    Exit Sub
    57.    
    58. mail_Err:
    59.    Select Case Err.Number
    60.       Case 32003:   MsgBox "The message could not be sent", vbExclamation, "Outlook Express"
    61.       Case Else:    MsgBox Err.Number & Err.Description
    62.    End Select
    63.    Resume mail_Exit

    don't mind the stringvalues it is filled with a default to test my code.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  18. #18
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Looks OK to me....

    Here is some documentation that might give you a clue....

    Proerties of the MAPIsession:
    The LogonUI property can be used to turn off the default logon screen provided by the mail server. You can set this property to False to suppress the mail server from presenting the logon screen when you invoke the SignOn method. If you set this property to False, you must supply valid data in the UserName and Password properties, or an error will be reported.
    You can use the NewSession property to force the creation of a new MAPI session, even if a MAPI session already exists for this workstation. By default, the SignOn method will attempt to locate any current MAPI interface sessions before attempting to log on to the mail server. When you set the NewSession parameter to True, the SignOn method will create a second MAPI session link with the mail server.

    VB Code:
    1. Private Sub Form_Load()
    2. '
    3. MAPISession1.DownloadMail = False ' dont get new mail
    4. MAPISession1.NewSession = True ' force a new session
    5. MAPISession1.SignOn
    6. MAPISession1.SignOff
    7. End
    8. '
    9. End Sub
    The Password and UserName properties should be set to valid values if you want to bypass the default logon screen. If you supply a UserName but leave the Password property blank, the SignOn method will force the logon dialog box to appear and prompt for the missing information. If, however, the LogonUI property is set to False, no dialog box will appear, and an error will be returned.
    If you are using the Microsoft Exchange Mail Client, you only need to provide a valid Profile Name - the username will be obtained from the Profile. Microsoft Exchange Mail will ignore any value in the Password property.
    VB Code:
    1. Private Sub Form_Load()
    2. '
    3. MAPISession1.DownloadMail = False ' don't get new mail
    4. MAPISession1.NewSession = True ' force a new session
    5. MAPISession1.UserName = "MCA" ' username / profilename for mail
    6. MAPISession1.Password = "PASSWORD" ' users password
    7. MAPISession1.SignOn
    8. MAPISession1.SignOff
    9. End
    10. '
    11. End Sub

  19. #19

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Sounds right.

    How can u know which profile is used on the machine.

    Did the trick with a new session and supplied the profile name , it worked good.

    But if i look at other machines there can be a different profile name.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  20. #20

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    When this routine is run once with setting the username to the profile , and placing this filling of username in comment it will run without asking the profile .

    So if I provide the profile in the username box of this control it doesn't show a box asking for the profile.

    Will this work on all machines if profile isn't known ??
    I don't think so , there must be a way to retrieve the profiles known on the machine running the app.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  21. #21
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    a) You are using MAPIsession and MAPImessage cntrols. These make use of something called Simple MAPI - which will not allow you to list profiles.
    b) You don't want to use full MAPI!
    c) Even if you could list all the profiles present, you wouldn't know which one to choose. For example, my profiles are Mail, PST only, Last year, and CJordan. The last sounds the most "official", but happens to be the one I rarely use! Some of these Profiles would allow me to send an e-mail, thers wouldn't. Some would have access to a PAB, others wouldn't.

    Normally, you just try to take the default - as that is the one the user uses most often.

  22. #22

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Thanks.

    Will work with what i have till now.

    If the profile isn't the right one then they have to give another one.

    It isn't so i need to have the PAB for this.
    The PAB i used for adding addresses to it.This will rarely be run.


    Thanks anyway.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

Posting Permissions

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



Click Here to Expand Forum to Full Width