Visar inlägg med etikett script. Visa alla inlägg
Visar inlägg med etikett script. Visa alla inlägg

onsdag 10 december 2008

Script: Strip all attachments from incoming mail

This script can be a good friend if you get lots of attachments in your inbox and you wish to have it shipped somewhere else automatically.Here is how you do this:

1. Start Outlook
2. Press ALT+F11 to go into VB editor
3. Rightclick Project1 and choose Insert > Module
4. Click Module1 (can be renamed)
5. Paste in this code below
6. Change the row Const BASE_PATH = "H:\\MailAttachments\\" to where you wanna save the attachments. Make sure you created the folder it points to.
7. Click Save button
8. Make a rule with the script option (depends on which mails you want to strip)
9. Restart Outlook to have the script start working

Code:

Sub StripAttachments(Item As Outlook.MailItem)

On Error GoTo EarlyBath

Const BASE_PATH = "H:\\MailAttachments\\"

If Item.Class = olMail Then
If Item.Attachments.Count > 0 Then
Dim objAtt As Outlook.Attachments
Set objAtt = Item.Attachments
For Each objattach In objAtt
Dim i, lngCounter As Long
Dim strLogger, strFile, strLocalFileLink, strLocalPath, strUser, strFolder As String


lngCounter = Item.Attachments.Count
'Debug.Print lngcounter
strLogger = "-------------------------------------------------------------------------------------------------"

'organise folders by sender
strFolder = BASE_PATH & Item.SenderName & "\\"

If Dir(strFolder, vbDirectory) = "" Then
MkDir (strFolder)
End If

'organise subfolder by received date
strFolder = strFolder & Strings.Format(Item.ReceivedTime, "ddmmyyyy") & "\\"

If Dir(strFolder, vbDirectory) = "" Then
MkDir (strFolder)
End If


'create and display link to dest folder
strLocalPath = "file://" & Replace(strFolder, " ", "%20")
strLocalPath = Replace(strLocalPath, "\\", "\")
strLogger = strLogger & vbCrLf & "Attachment Path: " & strLocalPath & vbCrLf
strLogger = strLogger & vbCrLf & "The following attachments have been stripped from this message:"



'move through the attachments, saving the file, deleting from msg body and inserting links
For i = lngCounter To 1 Step -1

strFile = objAtt.Item(i).FileName
If Len(strFile) > 0 Then
Dim trimLen: trimLen = Len(strFile) - InStrRev(strFile, ".")
Dim time As String
time = Format(Now(), "hhmm")
strFile = strFolder & "\\" & time & "_" & strFile
objAtt.Item(i).SaveAsFile strFile
objAtt.Item(i).Delete
strLocalFileLink = Replace(strFile, "\\", "\")
strLocalFileLink = "file://" & Replace(strLocalFileLink, " ", "%20")

strLogger = strLogger & vbCrLf & vbCrLf & "Attachment " & lngCounter & ": " & strLocalFileLink
Else
End If
'strLogger = ""
SkipAtt:
Next i

strLogger = strLogger & vbCrLf & "-------------------------------------------------------------------------------------------------" & vbCrLf

Item.Body = strLogger & Item.Body
Item.Save
Next
Set objAtt = Nothing

End If
End If
EarlyBath:
Exit Sub
End Sub

Script: Autocategorize mail when read

This script is good when you have a shared mailbox that several people are reading and you wanna know who has read which and what email. This script only works if you have category view on.

Script functionality: When clicking a mail in the mailbox, it will make a category with your name and move that clicked email to the category.


1. Start Outlook

2. Press ALT+F11 to go into VB editor

3. Expand Project1 so Microsoft Outlook Objects is visible

4. Expand Microsoft Outlook Objects so ThisOutlookSession becomes visible

5. Click ThisOutlookSession

6. Paste in this code below in the white field on the right window

7. Edit row Set olkFolder = OpenOutlookFolder("Mailbox - name\Inbox").Items to name on your mailbox. (this example is targeting Inbox folder)

8. Click Save button.9. Restart Outlook to have the script start working


Code:

Public WithEvents olkFolder As Outlook.Items

Private Sub Application_MAPILogonComplete()
'Change the folder path on the following line to that of the fodler you want to monitor
Set olkFolder = OpenOutlookFolder("Mailbox - name\Inbox").Items
End Sub

Private Sub olkFolder_ItemChange(ByVal Item As Object)
'If the item is marked as read
If Item.UnRead = False Then
If TypeName(Application.ActiveInspector) <> "Nothing" Then
If Item.Subject = Application.ActiveInspector.CurrentItem.Subject Then
'If no category is set already
If Item.Categories = "" Then
'Set the category and save the change
Item.Categories = Replace(Session.CurrentUser, ",", "")
Item.Save
End If
End If
End If
End If
End Sub


Function IsNothing(obj)
If TypeName(obj) = "Nothing" Then
IsNothing = True
Else
IsNothing = False
End If
End Function

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
Dim arrFolders As Variant, _
varFolder As Variant, _
olkFolder As Outlook.MAPIFolder
On Error GoTo ehOpenOutlookFolder
If strFolderPath = "" Then
Set OpenOutlookFolder = Nothing
Else
If Left(strFolderPath, 1) = "\" Then
strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
End If
arrFolders = Split(strFolderPath, "\")
For Each varFolder In arrFolders
If IsNothing(olkFolder) Then
Set olkFolder = Session.Folders(varFolder)
Else
Set olkFolder = olkFolder.Folders(varFolder)
End If
Next
Set OpenOutlookFolder = olkFolder
End If
On Error GoTo 0
Exit Function
ehOpenOutlookFolder:
Set OpenOutlookFolder = Nothing
On Error GoTo 0
End Function