vba timing

B

bobh

Hi,

On a daily basis I recieve emails which have spreadsheet attachments,
these emails I want to move to another Outlook folder and save the
attachment to a folder on my 'c:' disk drive.

I have vba code to save the attachment and I created a rule for when
the 'subject' line contains specific text to move it to the other
Outlook folder and run a vba sub which calls another sub which does
the actual work, see below(ps: I got this code from someplace on the
web).
I believe this code/process will work but I'm having one issue which
is

the rule says
Apply this rule after the message arrives
with testtry in the subject
and on this machine only
move it to the MyTestFolder folder
and run Project1.TestSave

So, when an email is recieved in my inbox which has 'testtry' in the
subject line it moves it to the 'MyTestFolder' but before it completes
that move it start to run the vba sub 'TestSave' which passes the
values to the sub SaveEmailAttachmentsToFolder and starts to run it
before the rule has finished moving the email to the folder so the sub
SaveEmailAttachmentsToFolder does not find any emails in MyTestFolder
so I end up getting the "MsgBox "There are no messages in this folder
" message from the sub.

How do I make the rule finish moving the email before it starts
running the script???

Public Sub TestSave(msg As MailItem)
'Arg 1 = Folder name in your Inbox
'Arg 2 = File extension, "" is every file
'Arg 3 = Save folder, "C:\Outlook\MyFiles" or ""
'If you use "" it will create a date/time stamped folder for you in
the "My Documents" folder.
'Note: If you use this "C:\Outlook\MyFiles" the folder must exist

SaveEmailAttachmentsToFolder "MyTestFolder", "xls", "C:\BH-Outlook
\TestSave"

End Sub


Public Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As
String, ExtString As String, DestFolder As String)
On Error GoTo ThisMacro_err
'Do not change code in the macro below

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim MyDocPath As String
Dim I As Integer
Dim wsh As Object
Dim fs As Object

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders(OutlookFolderInInbox)

I = 0
' Check subfolder for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in this folder : " &
OutlookFolderInInbox, vbInformation, "Nothing Found"
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Exit Sub
End If

'Create DestFolder if DestFolder = ""
If DestFolder = "" Then
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
MyDocPath = wsh.SpecialFolders.Item("mydocuments")
DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-
ss")
If Not fs.FolderExists(DestFolder) Then
fs.CreateFolder DestFolder
End If
End If

If Right(DestFolder, 1) <> "\" Then
DestFolder = DestFolder & "\"
End If

' Check each message for attachments and extensions
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
If LCase(Right(Atmt.FileName, Len(ExtString))) =
LCase(ExtString) Then
FileName = DestFolder & Item.SenderName & " " &
Atmt.FileName
Atmt.SaveAsFile FileName
I = I + 1
End If
Next Atmt
Next Item

' Show this message when Finished
If I > 0 Then
MsgBox "You can find the files here : " _
& DestFolder, vbInformation, "Finished!"
Else
MsgBox "No attached files in your mail.", vbInformation,
"Finished!"
End If

' Clear memory
ThisMacro_exit:
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Set fs = Nothing
Set wsh = Nothing
Exit Sub

' Error information
ThisMacro_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following
information." _
& vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume ThisMacro_exit

End Sub
 
M

Michael Bauer [MVP - Outlook]

In TestSave you already have a reference to the email (see the msg
variable). Pass that to the SaveEmailAttachmentsToFolder procedure, and
handle only that one item. Then you don't need to loop through the entire
inbox.

--
Best regards
Michael Bauer - MVP Outlook
Category Manager - Manage and share your categories:
SAM - The Sending Account Manager:
<http://www.vboffice.net/product.html?lang=en>


Am Mon, 17 May 2010 11:41:41 -0700 (PDT) schrieb bobh:
 
B

bobh

In TestSave you already have a reference to the email (see the msg
variable). Pass that to the SaveEmailAttachmentsToFolder procedure, and
handle only that one item. Then you don't need to loop through the entire
inbox.

--
Best regards
Michael Bauer - MVP Outlook
  Category Manager - Manage and share your categories:
  SAM - The Sending Account Manager:
  <http://www.vboffice.net/product.html?lang=en>

Am Mon, 17 May 2010 11:41:41 -0700 (PDT) schrieb bobh:























- Show quoted text -

I assume you mean here and unfortunetly I don't know how to do that. I
don't really know Outlook vba. What would the vba code be to select
and check the "msg as Mailitem" before moving it the the subfloder?

' Check each message for attachments and extensions
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
If LCase(Right(Atmt.FileName, Len(ExtString))) =
LCase(ExtString) Then
FileName = DestFolder & Item.SenderName & " " &
Atmt.FileName
Atmt.SaveAsFile FileName
I = I + 1
End If
Next Atmt
Next Item
 
M

Michael Bauer [MVP - Outlook]

In this function replace

Public Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As
String, ExtString As String, DestFolder As String)

by

Public Sub SaveEmailAttachmentsToFolder(Item as MailItem, ExtString As
String, DestFolder As String)

and remove the outer loop, the one through the SubFolder.Items collection.

--
Best regards
Michael Bauer - MVP Outlook
Category Manager - Manage and share your categories:
SAM - The Sending Account Manager:
<http://www.vboffice.net/product.html?lang=en>


Am Wed, 19 May 2010 09:31:37 -0700 (PDT) schrieb bobh:
 

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