Log in

View Full Version : VB in Outlook for e-mail attachments



Slash
12-21-2011, 03:43 PM
Hi,

I am new to this forum…

I need someone’s help here. I have a below code which execute in the VB editor in (ThisOutlookSeession) under Outlook 2010.This code move an attachment from e-mail folder (“Temp”) into a “C:/Attachment” folder. The code works perfectly but I have two issues here:


'Private Sub Application_Startup()

'End Sub

Sub GetAttachments()

Dim ns As NameSpace
Dim Temp As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim myNewFolder As MAPIFolder
Set ns = GetNamespace("MAPI")
Set Temp = ns.GetDefaultFolder(olFolderInbox)
Set myNewFolder = Temp.Folders("Temp")

i = 0
' Check Inbox for messages and exit of none found
If myNewFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Temp.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In myNewFolder.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
FileName = "C:\Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1


Next Atmt
Next Item


' Clear memory
Application_Startup_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub

End Sub


1) When run the Outlook using “Scheduled Tasks” it doesn’t execute this code by it self. I have to execute it manually in return to move the attachment. How can I run this code automatically without running it manually? That is, I want to automate this process. I even added 'Application_Startup()' to the code but still didn’t run by it self

2) What code should I used to move the e-mail from my “Temp” folder to the delete folder? That is, after the attachment is moved I what the e-mail to be deleted in this particular folder

Thank for your help in advance

JP2112
12-27-2011, 09:31 AM
When run the Outlook using “Scheduled Tasks”

What does this mean? Windows Task Scheduler? You're better off writing this as a VBScript file and telling WTS to run that instead.

http://social.technet.microsoft.com/Forums/en-US/windowsserver2008r2general/thread/464a3bb3-3bcd-47a0-ab03-f0b8910f2ed1

http://blogs.technet.com/b/heyscriptingguy/archive/2004/09/22/how-can-i-manage-scheduled-tasks-using-scripts.aspx