Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve Albert - 23 Dec 2005 12:55 GMT
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
Sue Mosher [MVP-Outlook] - 23 Dec 2005 13:48 GMT
You would use the APplication_ItemSend event handler in the THisOutlookSession module of Outlook VBA for that.

Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

>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
[quoted text clipped - 6 lines]
>
> - Steve
Steve Albert - 23 Dec 2005 15:31 GMT
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
______________________

> You would use the APplication_ItemSend event handler in the THisOutlookSession module of Outlook VBA for that.
>
[quoted text clipped - 8 lines]
> >
> > - Steve
Sue Mosher [MVP-Outlook] - 23 Dec 2005 16:04 GMT
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

Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> 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
[quoted text clipped - 129 lines]
>> >
>> > - Steve
Steve Albert - 23 Dec 2005 18:46 GMT
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.

> 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:
>
[quoted text clipped - 185 lines]
> >> >
> >> > - Steve
Sue Mosher [MVP-Outlook] - 23 Dec 2005 18:51 GMT
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.

Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> Sue,
>
[quoted text clipped - 195 lines]
>> >> >
>> >> > - Steve
Steve Albert - 23 Dec 2005 19:01 GMT
Thanks for your help.

- Steve

> 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.
>
[quoted text clipped - 201 lines]
> >> >> >
> >> >> > - Steve
Steve Albert - 27 Dec 2005 13:05 GMT
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

> 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.
>
[quoted text clipped - 201 lines]
> >> >> >
> >> >> > - Steve
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.