A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Extracting attachments with Macro



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old July 22nd 08, 06:27 PM posted to microsoft.public.outlook.program_vba
Ron P
external usenet poster
 
Posts: 2
Default Extracting attachments with Macro

Hi everyone.

I have a macro program that was programmed by someone years ago that I am
trying to figure out how it works. The macro would search the inbox for a
specific subject line of "Workorder", Save the excel attachment to a
directory and then delete the email from the inbox. It has worked for years
as there was only one excel attachment to the email, but now corporate IT
guys have changed email policies and as a result the macro no longer works.
This is due to the email now having 2 attachments, there is the excel
attachment and now an additional txt document attachment. How can I get this
to look at just the Excel attachment? I'm kind of at a loss in trying to
figure out how this person coded. It looks straight forward but I'm
apparently missing something.

Sub ExcelExtract()

Dim Item, Attachments, FolderName As Object
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim Folder As Outlook.MAPIFolder
Dim MailItem As Outlook.MailItem


Set myOlExp = myOlApp.ActiveExplorer
Set nsp = myOlApp.GetNamespace("MAPI")
Set Folder = nsp.GetDefaultFolder(olFolderInbox)
x = 0

itemcount = Folder.Items.Count
For Each myitem In Folder.Items
Set Attachments = myitem.Attachments
If InStr(myitem.Subject, "Workorder") 0 And
myitem.Attachments.Count 0 Then mycount = mycount + 1
Next

Workordercount = itemcount - mycount
Do Until Folder.Items.Count = Workordercount
For Each Item In Folder.Items
If InStr(Item.Subject, "Workorder") 0 Then
Set Attachments = Item.Attachments
If Attachments.Count 0 Then x = x + 1
For i = 1 To Attachments.Count
Attachments(i).SaveAsFile "C:\Worktemp\Workorder" & x &
".xls"
Item.Delete
Next i
End If
Next

Loop
End Sub


Thanks

--
Ron P
Ads
  #2  
Old July 22nd 08, 07:07 PM posted to microsoft.public.outlook.program_vba
krazymike
external usenet poster
 
Posts: 9
Default Extracting attachments with Macro

* * * * * * * * * * Attachments(i).SaveAsFile "C:\Worktemp\Workorder" & x & ".xls"

On my machine, I had to use Attachments.Item(i).SaveAsFile

Try checking for the extension first:

If mid$(Attachments.Item(i).FileName ,
InStrRev(Attachments.Item(i).FileName, ".") +1) = "xls" Then
Attachments(i).SaveAsFile "C:\Worktemp\Workorder" & x & ".xls"
  #3  
Old July 22nd 08, 07:14 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Extracting attachments with Macro

Here's one way. This will loop through the emails in your default
Inbox and check each email for the word "Workorder" in the subject. If
found, and there are attachments, it loops through the attachments
until it finds the spreadsheet and saves it to your folder. This is
air code so please test it first.

HTH,
JP


Sub ExcelExtract2()
Dim i As Long
Dim Msg As Outlook.MailItem
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim MyItems As Outlook.Items
Dim myAttach As Outlook.Attachments
Dim Att As Outlook.Attachment
Dim OrdNum As Long

Set olApp = Application
Set objNS = olApp.GetNamespace("MAPI")
Set MyItems = objNS.GetDefaultFolder(olFolderInbox).Items

For i = MyItems.Count To 1 Step -1
If MyItems.Item(i).Class = olMail Then
Set Msg = MyItems.Item(i)
If (InStr(Msg.Subject, "Workorder") 0) And
(Msg.Attachments.Count 0) Then
Set myAttach = Msg.Attachments
For Each Att In myAttach
If UCase$(Right$(Att.FileName, 3)) = "XLS" Then
OrdNum = OrdNum + 1
Att.SaveAsFile "C:\Worktemp\Workorder" & OrdNum &
".xls"
Msg.Delete
End If
Next Att
End If
End If
Next i

End Sub




On Jul 22, 1:27*pm, Ron P wrote:
Hi everyone.

I have a macro program that was programmed by someone years ago that I am
trying to figure out how it works. *The macro would search the inbox for a
specific subject line of "Workorder", Save the excel attachment to a
directory and then delete the email from the inbox. *It has worked for years
as there was only one excel attachment to the email, but now corporate IT
guys have changed email policies and as a result the macro no longer works. *
This is due to the email now having 2 attachments, *there is the excel
attachment and now an additional txt document attachment. *How can I get this
to look at just the Excel attachment? *I'm kind of at a loss in trying to
figure out how this person coded. *It looks straight forward but I'm
apparently missing something.



  #4  
Old July 22nd 08, 10:46 PM posted to microsoft.public.outlook.program_vba
Ron P
external usenet poster
 
Posts: 2
Default Extracting attachments with Macro

THANKS ever so much. The information contained helped out ALOT!!!
--
Ron P


"JP" wrote:

Here's one way. This will loop through the emails in your default
Inbox and check each email for the word "Workorder" in the subject. If
found, and there are attachments, it loops through the attachments
until it finds the spreadsheet and saves it to your folder. This is
air code so please test it first.

HTH,
JP


Sub ExcelExtract2()
Dim i As Long
Dim Msg As Outlook.MailItem
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim MyItems As Outlook.Items
Dim myAttach As Outlook.Attachments
Dim Att As Outlook.Attachment
Dim OrdNum As Long

Set olApp = Application
Set objNS = olApp.GetNamespace("MAPI")
Set MyItems = objNS.GetDefaultFolder(olFolderInbox).Items

For i = MyItems.Count To 1 Step -1
If MyItems.Item(i).Class = olMail Then
Set Msg = MyItems.Item(i)
If (InStr(Msg.Subject, "Workorder") 0) And
(Msg.Attachments.Count 0) Then
Set myAttach = Msg.Attachments
For Each Att In myAttach
If UCase$(Right$(Att.FileName, 3)) = "XLS" Then
OrdNum = OrdNum + 1
Att.SaveAsFile "C:\Worktemp\Workorder" & OrdNum &
".xls"
Msg.Delete
End If
Next Att
End If
End If
Next i

End Sub




On Jul 22, 1:27 pm, Ron P wrote:
Hi everyone.

I have a macro program that was programmed by someone years ago that I am
trying to figure out how it works. The macro would search the inbox for a
specific subject line of "Workorder", Save the excel attachment to a
directory and then delete the email from the inbox. It has worked for years
as there was only one excel attachment to the email, but now corporate IT
guys have changed email policies and as a result the macro no longer works.
This is due to the email now having 2 attachments, there is the excel
attachment and now an additional txt document attachment. How can I get this
to look at just the Excel attachment? I'm kind of at a loss in trying to
figure out how this person coded. It looks straight forward but I'm
apparently missing something.




  #5  
Old July 23rd 08, 03:20 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Extracting attachments with Macro

Glad to hear it!

--JP

On Jul 22, 5:46*pm, Ron P wrote:
THANKS ever so much. *The information contained helped out ALOT!!!
--
Ron P



"JP" wrote:
Here's one way. This will loop through the emails in your default
Inbox and check each email for the word "Workorder" in the subject. If
found, and there are attachments, it loops through the attachments
until it finds the spreadsheet and saves it to your folder. This is
air code so please test it first.

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
macro can not get all attachments in one time [email protected] Outlook and VBA 2 April 25th 08 02:16 PM
ANy clues on extracting attachments??? [email protected] Outlook and VBA 0 October 17th 07 03:21 PM
Extracting Data from Attachments mrbalaje Outlook and VBA 1 September 10th 07 06:45 AM
Extracting attachments from .pst files schizoid_man Outlook - General Queries 2 December 15th 06 09:38 AM
vba macro to print email and attachments outlook 2000 Dan over in IT Outlook and VBA 2 June 7th 06 08:08 PM


All times are GMT +1. The time now is 07:46 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.