PDA

View Full Version : Outlook Macro



daniels012
11-16-2009, 01:58 PM
Outlook macro
Can there be code written to open the 'To" button when the new button is hit?

So if I hit the "New" button it would execute the To button right away after the new email opens?
Or add a button to the menu bar which would open the "new" message and open the "to" button all at once.

I am using Office 2007
Thank You,
Michael

JP2112
11-16-2009, 03:37 PM
Check out the SelectNamesDialog Object.

http://msdn.microsoft.com/en-us/library/bb176400.aspx

daniels012
11-17-2009, 09:07 AM
WOW!
That is nice for opening the names. Can I add something to this to open the New email first?

MIchael

JP2112
11-17-2009, 06:00 PM
Sort of. Use Application.CreateItem to create a new email, but use the SelectNamesDialog object to get the recipients. Then use MailItem.Recipients.Add to add them to the message you created with CreateItem.

daniels012
11-19-2009, 12:19 PM
I am so bad/new at writing code. I am not sure exactly what you are talking about and/or where to add the lines to the code you first provided or what?

Michael

JP2112
11-19-2009, 06:19 PM
Don't say you're bad, just new ;)

Since we're doing this programmatically, you won't actually be creating a new email the same way you would manually.

The code from that link is:

Sub ShowContactsInDialog()
Dim oDialog As SelectNamesDialog
Dim oAL As AddressList
Dim oContacts As Folder

Set oDialog = Application.Session.GetSelectNamesDialog
Set oContacts = _
Application.Session.GetDefaultFolder(olFolderContacts)

'Look for the address list that corresponds with the Contacts folder
For Each oAL In Application.Session.AddressLists
If oAL.GetContactsFolder = oContacts Then
Exit For
End If
Next
With oDialog
'Initialize the dialog box with the address list representing the Contacts folder
.InitialAddressList = oAL
.ShowOnlyInitialAddressList = True
If .Display Then
'Recipients Resolved
'Access Recipients using oDialog.Recipients
End If
End With
End Sub
One of the comments tells you to access the recipients using oDialog.Recipients. So you'd need a loop to add each of the recipients selected in the dialog to the mail item you're creating.

To create the email, I'd use a function like:

Function CreateNewItem(ByRef olApp As Outlook.Application, _
whatItem As OlItemType) As Object
Set CreateNewItem = olApp.CreateItem(whatItem)
End Function
So the code becomes:

Sub ShowContactsInDialog()
Dim oDialog As SelectNamesDialog
Dim oAL As AddressList
Dim oContacts As Folder
Dim Msg As Outlook.MailItem
Dim recip As Outlook.Recipients

Set oDialog = Application.Session.GetSelectNamesDialog
Set oContacts = _
Application.Session.GetDefaultFolder(olFolderContacts)

'Look for the address list that corresponds with the Contacts folder
For Each oAL In Application.Session.AddressLists
If oAL.GetContactsFolder = oContacts Then
Exit For
End If
Next
With oDialog
'Initialize the dialog box with the address list representing the Contacts folder
.InitialAddressList = oAL
.ShowOnlyInitialAddressList = True
If .Display Then
'Recipients Resolved
'Access Recipients using oDialog.Recipients
Set Msg = CreateNewItem(Outlook.Application, olMailItem)

' loop through oDialog.Recipients and add to msg
For Each recip In oDialog.Recipients
Msg.Recipients.Add recip
Next recip

' show msg
Msg.Display

End If
End With
End Sub

Function CreateNewItem(ByRef olApp As Outlook.Application, _
whatItem As OlItemType) As Object
Set CreateNewItem = olApp.CreateItem(whatItem)
End Function

daniels012
11-20-2009, 07:59 AM
I attached the code to a button I added to the menu bar. Nothing happens when I hit the button. Could it be a Macro issue? Although it works with the Microsoft code i used before??

Michael

JP2112
11-23-2009, 05:08 AM
Try setting a breakpoint and stepping through the code to see where it fails. To set a breakpoint, click on the first line of code and press F9. Then press F8 to step through the code one line at a time.

If it runs and then fails, post which line it failed on and the exact text of any error msg you received.

If the code doesn't even run, make sure you assigned the macro to the toolbar button correctly.

daniels012
11-23-2009, 10:13 AM
Had to enable Macros! Once I did I get an error... on this line of code:
For Each recip In oDialog.Recipients

Michael

JP2112
11-23-2009, 11:03 AM
Did you select any names in the names dialog? How many?

daniels012
11-24-2009, 07:54 AM
I selected 1 name, then I hit OK and it gives a runtime error '13

Michael

JP2112
11-24-2009, 04:39 PM
I think we're checking the wrong collection. Change

Dim recip As Outlook.Recipients

to

Dim recip As SelectNamesDialog.Recipient


Also, per http://msdn.microsoft.com/en-us/library/bb176181.aspx, you'll need to initialize this property before using it:

If you do not set this property before displaying the Select Names dialog box, then the Recipients object represented by SelectNamesDialog.Recipients will have a Recipients.Count equal to zero.

nichobe
09-23-2010, 08:16 AM
as you are looking at a Recipient not a group in the For loop you need to define recip as singular not plural
Change:-
Dim recip As Outlook.recipients to
Dim recip As Outlook.recipient
great post guys! thanks