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.
Printable View
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.
VB Code:
'Set A Reference to the Microsoft Outlook Object Library Private olApp As Outlook.Application Private olNS As Outlook.NameSpace Private olAL As Outlook.AddressList Private olAE As Outlook.AddressEntry Private olMail As Outlook.MailItem Private Sub Form_Load() Dim Counter As Long Set olApp = New Outlook.Application Set olNS = olApp.GetNamespace("MAPI") For Each olAL In olNS.AddressLists For Each olAE In olAL.AddressEntries DoEvents Counter = Counter + 1 Label1.Caption = Counter ListView1.ListItems.Add , , olAE.Name ListView1.ListItems(ListView1.ListItems.Count).SubItems(1) = olAE.Address Next Next End Sub Private Sub ListView1_Click() Set olMail = olApp.CreateItem(olMailItem) olMail.Recipients.Add ListView1.SelectedItem.Text olMail.Subject = "Test" olMail.Body = "Test" olMail.SendSet olMail = olApp.CreateItem(olMailItem) olMail.Recipients.Add ListView1.SelectedItem.Text olMail.Subject = "Test" olMail.Body = "Test" olMail.Send End Sub
I need to be able to add some addresses to the address book.
Could it be possible if so how ?
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:
Sub CommandButton1_Click() myName = Item.To Set myNameSpace = Application.GetNameSpace("MAPI") Set myGAddressList = myNameSpace.AddressLists("Global Address List") Set myGEntries = myGAddressList.AddressEntries Set myGEntry = myGEntries(myName) myManager = myGEntry.Manager Set myGEntry2 = myGEntries(myManager) Set myPAddressList = myNameSpace.AddressLists("Personal Address Book") Set myPEntries = myPAddressList.AddressEntries 'Add a new AddressEntry object to the personal 'address collection with the name, address, and 'manager of the name in your To field. Set myPEntry = myPEntries.Add("Microsoft Mail Address", myName) myPEntry.Address = myGEntry.Address myPEntry.Manager = myGentry.Manager 'Update to persist the collection. myPEntry.Update 'Now add the manager's info. to 'the Personal address collection. Set myPEntry2 = myPEntries.Add("Microsoft Mail Address", myManager) myPEntry2.Address = myGEntry2.Address myPEntry2.Manager = myGentry2.Manager myPEntry2.Update End Sub
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.
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.
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.
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) ?
Through the normal Outlook UI - yes its easy.
Through code, I am not sure.
There is a way of adding new Personal Folder stores:
but I haven't found an equivalent "AddAddressList".VB Code:
Dim myOlApp As New Outlook.Application Dim myNS As Outlook.NameSpace Set myNS = myOlApp.GetNamespace("MAPI") myNS.AddStore "c:\my documents\" & myNS.CurrentUser & ".pst"
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.
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.
Silly me
message.send True
You should be able to do:Quote:
Is it possible to fill in all contents and show the email to be send without sending it through code ??
MessageObject.Display
once you have put everything you want into the message.
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.
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!
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.
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.
VB Code:
DoCmd.Hourglass True ' Message to send with the mail msg = "Dit bericht is verstuurd vanuit code." & vbCrLf & "Er kunnen geen items toegevoegd worden aan het adresboek," & _ vbCrLf & "als er geen 'Personal Address Book' aanwezig is." & vbCrLf ' login and password can be given with it ' mapSessie.Password = "" ' mapSessie.UserName = "" ' if there isn't an nt login known or not automaticly logged in 'with it a username and password will be asked. mapSessie.SignOn mapBericht.SessionID = mapSessie.SessionID 'Compose new message mapBericht.Compose ' mapBericht.Show ' show address book ' MsgBox mapBericht.AddressModifiable ' ' mapBericht.AddressModifiable = True ' mapBericht.RecipDisplayName = "Girly" mapBericht.RecipAddress = "girly" ' Test if address is known mapBericht.AddressResolveUI = True mapBericht.ResolveName 'make rest of message mapBericht.MsgSubject = "Status Report : from " & Time mapBericht.MsgNoteText = msg & "Test op email versturen via code." & vbLf & "Build successful!" & vbLf & Time 'Send the message pad = Mid$(CurrentDb.Name, 1, Len(CurrentDb.Name) - InStr(1, StrReverse(CurrentDb.Name), "\") + 1) If frmSoort.Value <> 1 Then pad = pad & "Verkoop.rtf" DoCmd.OutputTo acOutputReport, "vfaktuur_doc", acFormatRTF, pad Else pad = pad & "Verkoop.snp" DoCmd.OutputTo acOutputReport, "vfaktuur_doc", acFormatSNP, pad End If mapBericht.AttachmentPathName = pad ' add attachment mapBericht.Send True Kill (pad) mail_Exit: On Error Resume Next mapSessie.SignOff DoCmd.Hourglass False Exit Sub mail_Err: Select Case Err.Number Case 32003: MsgBox "The message could not be sent", vbExclamation, "Outlook Express" Case Else: MsgBox Err.Number & Err.Description End Select Resume mail_Exit
don't mind the stringvalues it is filled with a default to test my code.
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.
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.VB Code:
Private Sub Form_Load() ' MAPISession1.DownloadMail = False ' dont get new mail MAPISession1.NewSession = True ' force a new session MAPISession1.SignOn MAPISession1.SignOff End ' End Sub
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:
Private Sub Form_Load() ' MAPISession1.DownloadMail = False ' don't get new mail MAPISession1.NewSession = True ' force a new session MAPISession1.UserName = "MCA" ' username / profilename for mail MAPISession1.Password = "PASSWORD" ' users password MAPISession1.SignOn MAPISession1.SignOff End ' End Sub
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.
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.
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.
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.