Macros and Outlook?

  • Thread starter Thread starter BruceJ
  • Start date Start date
B

BruceJ

Is there a way to "Record a macro" in OL 2002? I want to create rules via VB
(and it looks like you can not do this reliabley in OL 2002), so my next
option is to do a MACRO. Can I create a macro (that launches in excel
ideally!) to modify an existing rule?

Thanks
Bruce
 
You can't record a macro in Outlook, but you can write them. While it is possible (not at all easy) to make some modifications of very simple rules, my guess is that your project (as yet undescribed) will need more than just simple rules. See http://www.slipstick.com/dev/vb.htm for VB+Outlook resources, including samples.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Sue,

Do you know of any samples that will look at email as it comes in, and then
match it to a list of given addresses, and then move it to a folder?

Is there a way to look at the code for any of the rules that I have created?
I am used to being able to "record" a macro and then modify it in Excel, and
I am just looking for the basic info I need.

Thanks
Bruce



You can't record a macro in Outlook, but you can write them. While it is
possible (not at all easy) to make some modifications of very simple rules,
my guess is that your project (as yet undescribed) will need more than just
simple rules. See http://www.slipstick.com/dev/vb.htm for VB+Outlook
resources, including samples.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Sue, after spending more time on your site, I came across something that
sounded _VERY_ much workable to me! That is a RULE with "Sender is in
SPECIFIED Address Book".

This LOOKS like it is just what I would need....

If I can get THIS to work, then all I need to do is to update the contact
with then new info when I am looking for emails. Hopefully I can figure out
how to do this with VBA!!!


Thanks
Bruce



See http://www.slipstick.com/dev/code/quarexe.htm for a simple example of
watching for new Inbox items and moving some to a new folder. Your variation
would involve looking at addresses. If you mean the From address, see
http://www.slipstick.com/dev/code/getsenderaddy.htm.

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Sue,

Thanks for the link!
I tried the code, but, it did not work....
I even put a msgbox inthe code so I could see if it was getting to the code.
It is not. I did put it in the correct folder, as per the instructions.

Right now, my outlook is REALLLLLLLLYYYYYYY SLOWWWWW so I can not get there
to pull out the code I put in. (I am cleaning out my pst file right now, and
then going to run scanpst. I do have lots of JUNK and VIRUS emails. I need
to purge those out and get my PST file smaller. It is currentlly over 1 g,
and I need to watch out. Does 2002 still have the issue with 1.2 (I think
that was the right size) files cuasing data to get lost and not being able
to access your data? I think I am up to the current patches. This maybe
should be a seperate email....


Back to the origianal issue...

Now, is there a way, I can do something similar from EXCEL? I use the VBA to
send out all of the email I am actually looking for. All of the email I send
out of OL, I don't need to put into the fold, as I do not need to follow
their email perse. Or does it need to be called from Outlook?






Great, Bruce. You might find this code useful, too:
http://www.slipstick.com/contacts/addauto.htm
 
Outlook 2003 adds a new format for PST files that allows them to be nearly unlimited in size.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Did you check your macro security settings -- Tools | Macro | Security? Does other VBA code run?

If you're using Excel to send out the mail, then you should be able to adapt the code I provided to add the same functionality (checking addresses and creating contacts) to your Excel routines.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
I D/L redemption, and installed it. I then tried to register it in VB (I
have 2 entries for SafeOutlookLibrary and both point to redemption.dll) When
I restarted OL 2002, I got an error that it could not find extend.dat (or
extra.dat if I look in event log. I THOUGHT it said extend.dat ) (I cleared
the message, as I thought I would be able to get it again by restarting O/L-
I have not been able to get the error again….)

I have pressed ALT-11, starting the VBA editor, and then browsed to
ThisOutlookSession and pasted the whole code section there. I have since
moved all but the first section into a diff module.

I then tried to run it by creating a new email to a address that was not in
the contacts folder. I did not get any response. I then put a msgbox in the
code, expecting it to pop the message box, it did not. I am NOT familiar
with O/L for VBA, but I have done some in EXCEL, so it SHOULD be similair…



What am I missing?



Also, how whould I change the contact address book to work with? Would I
change the line

Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items

To something like Set colContacts = “NewAddressBook”





Thanks for taking the time to read and to respond to this.



I have not tried any other VBA code. I have not had any need for it. what
would be a good one to just test something?

I have signed the code with selfcert and I have also put security to LOW.



Thanks

Bruce





Did you check your macro security settings -- Tools | Macro | Security? Does
other VBA code run?

If you're using Excel to send out the mail, then you should be able to adapt
the code I provided to add the same functionality (checking addresses and
creating contacts) to your Excel routines.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Did you restart Outlook after changing the macro security setting to Low? If so, does any other VBA code run? Write a "hello world" MsgBox routine if you need something to test. Did you try putting a breakpoint in the Application_ ItemSend() event handler?

You can make a basic check of whether there's a problem with Redemption by looking at Tools | Options | Other | Advanced Options | Addin Manager. It should appear there.
Also, how whould I change the contact address book to work with? Would I
change the line

Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items

To something like Set colContacts = “NewAddressBook”

Contacts folders and address books are *not* the same thing, so I'm not sure what you're getting at.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Did you restart Outlook after changing the macro security setting to Low?
I thought I did... I did restart again and my Hello World worked.
Now I am getting
run-time error '424':
Object required
and debug puts me at
For Each objSRecip In objSMail.Recipients
objSMail.Recipients = nothing when I place my mouse over it.

Thanks
Bruce


Did you restart Outlook after changing the macro security setting to Low? If
so, does any other VBA code run? Write a "hello world" MsgBox routine if you
need something to test. Did you try putting a breakpoint in the Application_
ItemSend() event handler?

You can make a basic check of whether there's a problem with Redemption by
looking at Tools | Options | Other | Advanced Options | Addin Manager. It
should appear there.
Also, how whould I change the contact address book to work with? Would I
change the line

Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items

To something like Set colContacts = “NewAddressBook”

Contacts folders and address books are *not* the same thing, so I'm not sure
what you're getting at.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Sorry to be a pest <grin>

If I wanted it to add the contact info into a diff folder, would I change
the
Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
to
Set colContacts = "MyContactFolderNameHere"
If not, how would I cahnge the folder?
thanks

Bruce

Did you restart Outlook after changing the macro security setting to Low? If
so, does any other VBA code run? Write a "hello world" MsgBox routine if you
need something to test. Did you try putting a breakpoint in the Application_
ItemSend() event handler?

You can make a basic check of whether there's a problem with Redemption by
looking at Tools | Options | Other | Advanced Options | Addin Manager. It
should appear there.
Also, how whould I change the contact address book to work with? Would I
change the line

Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items

To something like Set colContacts = “NewAddressBook”

Contacts folders and address books are *not* the same thing, so I'm not sure
what you're getting at.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
What statement instantiates objSMail?

BruceJ said:
Did you restart Outlook after changing the macro security setting to Low?
Now I am getting
run-time error '424':
Object required
and debug puts me at
For Each objSRecip In objSMail.Recipients
objSMail.Recipients = nothing when I place my mouse over it.

Thanks
Bruce


Did you restart Outlook after changing the macro security setting to Low? If
so, does any other VBA code run? Write a "hello world" MsgBox routine if you
need something to test. Did you try putting a breakpoint in the Application_
ItemSend() event handler?

You can make a basic check of whether there's a problem with Redemption by
looking at Tools | Options | Other | Advanced Options | Addin Manager. It
should appear there.


Contacts folders and address books are *not* the same thing, so I'm not sure
what you're getting at.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Here is the code I have (as you an see, just cut and paste now (I took out
my msgbox)
Redemtion.dll is present, Outlook DOES have it in the add-in manager.

Thanks
Bruce

' sample application by Sue Mosher
' send questions/comments to (e-mail address removed)

' The Application_ItemSend procedure must go in the
' built-in ThisOutlookSession session module in Outlook VBA
Private Sub Application_ItemSend(ByVal Item As Object, _
Cancel As Boolean)
Item.Categories = ""
If Item.Class = olMail Then
Call AddRecipToContacts(Item)
End If
End Sub

' This procedure can go in any module
Sub AddRecipToContacts(objMail As MailItem)
Dim strFind As String
Dim strAddress As String
Dim objSMail As Redemption.SafeMailItem
Dim objSRecip As Redemption.SafeRecipient
Dim objNS As NameSpace
Dim colContacts As Items
Dim objContact As ContactItem
Dim i As Integer

' process message recipients
Set objSMail = CreateObject("Redemption.SafeMailItem")
objMail.Save
objSMail.Item = objMail
Set objNS = Application.GetNamespace("MAPI")
Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
For Each objSRecip In objSMail.Recipients
' check to see if the recip is already in Contacts
strAddress = objSRecip.Address
For i = 1 To 3
strFind = "[Email" & i & "Address] = " & _
AddQuote(strAddress)
Set objContact = colContacts.Find(strFind)
If Not objContact Is Nothing Then
Exit For
End If
Next

' if not, add it
If objContact Is Nothing Then
Set objContact = Application.CreateItem(olContactItem)
With objContact
.FullName = objSRecip.Name
.Email1Address = strAddress
.Save
End With
End If
Set objContact = Nothing
Next

Set objSMail = Nothing
Set objSRecip = Nothing
Set objNS = Nothing
Set colContacts = Nothing
End Sub

' helper function - put in any module
Function AddQuote(MyText) As String
AddQuote = Chr(34) & MyText & Chr(34)
End Function


What statement instantiates objSMail?
 
Sue,
Am I doing this correctly? How would I use the getfolder() within the
function AddRecipToContacts to set the desired contact folder (it is on the
root of my Personal Folders) so I thought it should be "personal
folders/BPConstacts"

' Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
Set colContacts = GetFolder("Personal Folders\BPContacts").Items
I get an error13 Type mismatch

If I do
Set colContacts = GetFolder("BPContacts").Items
I get a run time error 91 object varible or With block varible not set

I am doing this wrong? I must be...

Thanks
Bruce




To get a non-default folder, you need to walk the folder hierarchy using the
Folders collections or use a function that does that for you. See
http://www.slipstick.com/dev/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
It's almost always better in Outlook to get objects one at a time before using their properties, e.g.

Set objFolder = GetFolder("Personal Folders\BPContacts")
If Not objFolder Is Nothing Then
Set colContacts = objFolder.Items
Else
MsgBox "Could not get a MAPIFolder object for Personal Folders\BPContacts"
End If

How did you declare colContacts?

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Getting closer!!! Now, it seems to be finding the record (if present) but it
is adding it to the default olContactItem.
How can I tell what GetFolder is finding? Is there a way to display the
value? And what SHOULD it look like Iif I am calling the directory the right
name?

Also, in the portion that adds the contact:
If objContact Is Nothing Then
MsgBox "Adding ..."
Set objContact = Application.CreateItem(olContactItem)
With objContact
.FullName = objSRecip.Name
.Email1Address = strAddress
.Save
End With
End If

how do I force a location for the item?
below is the module as it stands.
Thanks
Bruce
------------------------

Sub AddRecipToContacts(objMail As MailItem)
Dim strFind As String
Dim strAddress As String
Dim objSMail As Redemption.SafeMailItem
Dim objSRecip As Redemption.SafeRecipient
Dim objNS As NameSpace
Dim colContacts As Items
Dim objContact As ContactItem
Dim i As Integer

' process message recipients
Set objSMail = CreateObject("Redemption.SafeMailItem")
objMail.Save
objSMail.Item = objMail
Set objNS = Application.GetNamespace("MAPI")
'Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
Set objFolder = GetFolder("Personal Folders\BPContacts")
If Not objFolder Is Nothing Then
Set colContacts = objFolder.Items
MsgBox "Got info for Personal Folders\BPContacts"
If Not colContacts Is Nothing Then
MsgBox "colcontacts has value"
Else
MsgBox "colcontacts is empty"
Stop
End If


Else
MsgBox "Could not get a MAPIFolder object for Personal
Folders\BPContacts"
End If


For Each objSRecip In objSMail.Recipients
' check to see if the recip is already in Contacts
strAddress = objSRecip.Address
For i = 1 To 3
strFind = "[Email" & i & "Address] = " & _
AddQuote(strAddress)
Set objContact = colContacts.Find(strFind)
'Stop
If Not objContact Is Nothing Then
Stop
Exit For
End If
Next
Stop
' if not, add it
If objContact Is Nothing Then
MsgBox "Adding ..."
Set objContact = Application.CreateItem(olContactItem)
With objContact
.FullName = objSRecip.Name
.Email1Address = strAddress
.Save
End With
End If
'Stop
Set objContact = Nothing
Next

Set objSMail = Nothing
Set objSRecip = Nothing
Set objNS = Nothing
Set colContacts = Nothing
End Sub
It's almost always better in Outlook to get objects one at a time before
using their properties, e.g.

Set objFolder = GetFolder("Personal Folders\BPContacts")
If Not objFolder Is Nothing Then
Set colContacts = objFolder.Items
Else
MsgBox "Could not get a MAPIFolder object for Personal
Folders\BPContacts"
End If

How did you declare colContacts?

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx
 
I figured it out! I am using
Set objContact = objFolder.Items.Add(olContactItem)
Now... I just need to figure out the name for the fields and stuff those
with the info I need!

Thanks!

Bruce

BruceJ said:
Getting closer!!! Now, it seems to be finding the record (if present) but it
is adding it to the default olContactItem.
How can I tell what GetFolder is finding? Is there a way to display the
value? And what SHOULD it look like Iif I am calling the directory the right
name?

Also, in the portion that adds the contact:
If objContact Is Nothing Then
MsgBox "Adding ..."
Set objContact = Application.CreateItem(olContactItem)
With objContact
.FullName = objSRecip.Name
.Email1Address = strAddress
.Save
End With
End If

how do I force a location for the item?
below is the module as it stands.
Thanks
Bruce
------------------------

Sub AddRecipToContacts(objMail As MailItem)
Dim strFind As String
Dim strAddress As String
Dim objSMail As Redemption.SafeMailItem
Dim objSRecip As Redemption.SafeRecipient
Dim objNS As NameSpace
Dim colContacts As Items
Dim objContact As ContactItem
Dim i As Integer

' process message recipients
Set objSMail = CreateObject("Redemption.SafeMailItem")
objMail.Save
objSMail.Item = objMail
Set objNS = Application.GetNamespace("MAPI")
'Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
Set objFolder = GetFolder("Personal Folders\BPContacts")
If Not objFolder Is Nothing Then
Set colContacts = objFolder.Items
MsgBox "Got info for Personal Folders\BPContacts"
If Not colContacts Is Nothing Then
MsgBox "colcontacts has value"
Else
MsgBox "colcontacts is empty"
Stop
End If


Else
MsgBox "Could not get a MAPIFolder object for Personal
Folders\BPContacts"
End If


For Each objSRecip In objSMail.Recipients
' check to see if the recip is already in Contacts
strAddress = objSRecip.Address
For i = 1 To 3
strFind = "[Email" & i & "Address] = " & _
AddQuote(strAddress)
Set objContact = colContacts.Find(strFind)
'Stop
If Not objContact Is Nothing Then
Stop
Exit For
End If
Next
Stop
' if not, add it
If objContact Is Nothing Then
MsgBox "Adding ..."
Set objContact = Application.CreateItem(olContactItem)
With objContact
.FullName = objSRecip.Name
.Email1Address = strAddress
.Save
End With
End If
'Stop
Set objContact = Nothing
Next

Set objSMail = Nothing
Set objSRecip = Nothing
Set objNS = Nothing
Set colContacts = Nothing
End Sub
It's almost always better in Outlook to get objects one at a time before
using their properties, e.g.

Set objFolder = GetFolder("Personal Folders\BPContacts")
If Not objFolder Is Nothing Then
Set colContacts = objFolder.Items
Else
MsgBox "Could not get a MAPIFolder object for Personal
Folders\BPContacts"
End If

How did you declare colContacts?

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx

BruceJ said:
Sue,
Am I doing this correctly? How would I use the getfolder() within the
function AddRecipToContacts to set the desired contact folder (it is on the
root of my Personal Folders) so I thought it should be "personal
folders/BPConstacts"

' Set colContacts = objNS.GetDefaultFolder(olFolderContacts).Items
Set colContacts = GetFolder("Personal Folders\BPContacts").Items
I get an error13 Type mismatch

If I do
Set colContacts = GetFolder("BPContacts").Items
I get a run time error 91 object varible or With block varible not set
 
Back
Top