Run a script in rules in "Check Messages After Sending"

G

Guest

I do not see an option for "Run a script" in rules when choosing "Check
Messages After Sending". I would like to run a script on items I send , so
that they are automatically "processed" before they are placed in the Sent
folder.

Any suggestions?

Thanks.

- Steve
 
S

Sue Mosher [MVP-Outlook]

You would use the APplication_ItemSend event handler in the THisOutlookSession module of Outlook VBA for that.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

This is what I want to do. I have a script that I put together from your site
and others that strips attachments from e-mails, and puts a small text
attachment in the e-mail that has the name of the file that was attached.
Right now, I must highlight the messages I want to process this way. I would
like to adjust this script so that it automatically does this procedure for
any e-mail message I send with an attachment, and then saves the processed
message in the Sent folder. Here is the entire script:
______________________
Public Sub StripAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
'added
Dim objItem As Object
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolder As String
Dim strS As String

On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection


' Check each selected item for attachments.
' If attachments exist, save them to the Temp
' folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
If objMsg.Class = olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' We need to use a count down loop for
' removing items from a collection. Otherwise,
' the loop counter gets confused and only every
' other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).PathName &
objAttachments.Item(i).FileName

' Write to file
strS = "Removed Attachments: " & vbCrLf & "'" &
objAttachments.Item(i).PathName & objAttachments.Item(i).FileName & "'"
' & vbCrLf & vbCrLf & vbCrLf & objMsg.Body
WriteToFile strS, "J:\My Documents\Attach.txt", True

' add remark to message text
' objMsg.Body = "Removed Attachments: " & vbCrLf & "'"
& objAttachments.Item(i).PathName & objAttachments.Item(i).FileName & "'" &
vbCrLf & vbCrLf & vbCrLf & objMsg.Body

' Delete the attachment.
objAttachments.Item(i).Delete

'AddAttachmentToSelectedMessages
objMsg.Attachments.Add ("J:\My Documents\Attach.txt")
objMsg.Save

Next i
End If

objMsg.Save
End If
Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub



Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.

'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.

Dim lngFN As Long

On Error GoTo Err_WriteToFile
lngFN = FreeFile()
'Change Output in next line to Append to
'append to existing file instead of overwriting
Select Case Overwrite
Case True
Open FileSpec For Output As #lngFN
Case False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Var);
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function
______________________
 
S

Sue Mosher [MVP-Outlook]

In that scenario, you would need to process the messages *after* they land in the Sent Items folder. Put this code in the ThisOutlookSession module:

Dim WithEvents sentItems As Outlook.Items

Private Sub Application_Startup()
Dim ns As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Set ns = Application.GetNamespace("MAPI")
Set fld = ns.GetDefaultFolder(olFolderSentMail)
Set sentItems = fld.Items
Set fld = Nothing
Set ns = Nothing
End Sub


Private Sub sentItems_ItemAdd(ByVal Item As Object)
' put necessary declaration here

' put your code to process Item here
If Item.Class = olMail Then
Set objMsg = Item
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' We need to use a count down loop for
' removing items from a collection. Otherwise,
' the loop counter gets confused and only every
' other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).PathName &
objAttachments.Item(i).FileName

' Write to file
strS = "Removed Attachments: " & vbCrLf & "'" &
objAttachments.Item(i).PathName & objAttachments.Item(i).FileName & "'"
' & vbCrLf & vbCrLf & vbCrLf & objMsg.Body
WriteToFile strS, "J:\My Documents\Attach.txt", True

' add remark to message text
' objMsg.Body = "Removed Attachments: " & vbCrLf & "'"
& objAttachments.Item(i).PathName & objAttachments.Item(i).FileName & "'" &
vbCrLf & vbCrLf & vbCrLf & objMsg.Body

' Delete the attachment.
objAttachments.Item(i).Delete

'AddAttachmentToSelectedMessages
objMsg.Attachments.Add ("J:\My Documents\Attach.txt")
objMsg.Save

Next i
End If
End Sub



--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Sue,

What declarations go in the section you marked:

Private Sub sentItems_ItemAdd(ByVal Item As Object)
' put necessary declaration here

I am still learning vba. How would my code fit into what you have sent? I
see some of my code there.
 
S

Sue Mosher [MVP-Outlook]

You would put there any declaration for variables that the code in the procedure uses. I'm just too lazy to rewrite your entire procedure and figured you can do that part.

Since ItemAdd passes the item added as an argument, all I did was copy and paste the part of your code that acts on a single item.

Note that you'll need to restart Outlook or run the Application_Startup procedure to initialize the event handler.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Thanks for your help.

- Steve

Sue Mosher said:
You would put there any declaration for variables that the code in the procedure uses. I'm just too lazy to rewrite your entire procedure and figured you can do that part.

Since ItemAdd passes the item added as an argument, all I did was copy and paste the part of your code that acts on a single item.

Note that you'll need to restart Outlook or run the Application_Startup procedure to initialize the event handler.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Sue,

I tried your approach and it worked! Outlook will now strip those
attachments automatically as they are sent, and then save a copy of the
message to the sent folder with an attachment that just has the name of the
file in it.

Thanks for your help.

- Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top