|
-
Apr 18th, 2008, 07:55 AM
#1
Thread Starter
Addicted Member
Outlook attachments copy to hard-drive
Hey everyone. Wondering if someone could help me get my code fixed up. I'm trying to create code that will enable me to extract ".zip" files from Outlook from a specified folder.
So far I have managed to get my code to retrieve all attachments from my inbox and put them in a folder on my PC.
I'm just wondering if anyone has any ideas of how I could change this code to obtain attachments from a specified mailbox folder...and only get ".zip" attachments.
I simply put the code below into a project and run it, it does the job...Any help appreciated, thanks
Code:
Private Sub Form_Load()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.Namespace
Dim oFldr As Outlook.MAPIFolder
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Outlook.Attachment
Dim iMsgCount As Integer
Dim oMessage As Outlook.MailItem
Dim iCtr As Long, iAttachCnt As Long
Dim sFileNames As String
Dim aFileNames() As String
'get reference to inbox
Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)
For Each oMessage In oFldr.Items
With oMessage
'reference and save all attachments
With oMessage.Attachments
iAttachCnt = .Count
If iAttachCnt > 0 Then
For iCtr = 1 To iAttachCnt
.Item(iCtr).SaveAsFile "C:\INPUT\" & .Item(iCtr).FileName
Next iCtr
End If
End With
End With
DoEvents
Next
-
Apr 18th, 2008, 10:26 AM
#2
Thread Starter
Addicted Member
Re: Outlook attachments copy to hard-drive
I've made an improvement in the respect that the code now searches a folder within INBOX.
However i'm still rather unsure as to how I can check the Mailbox...as we have several Mailboxes. So top level I would need to define the MAILBOX and then the INBOX and then the FOLDER.
I've acomplished looking in the INBOX then FOLDER but can't work out how to define the user mailbox and only copy .zip files. Heres my code:
(I Know theres alot of dim stuff going on but i'm tired and haven't had chance to tidy the code yet, just want to get it working)..
Code:
Private Sub Form_Load()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.Namespace
Dim oFldr As Outlook.MAPIFolder
Dim oAttachments As Outlook.Attachments
Dim oMessage As Outlook.MailItem
Dim iCtr As Long, iAttachCnt As Long
Dim sFileNames As String
Dim aFileNames() As String
Dim SubFolder As MAPIFolder
'get reference to inbox
Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)
Set SubFolder = oFldr.Folders("FOLDER")
For Each oMessage In SubFolder.Items
With oMessage
'reference and save all attachments
With oMessage.Attachments
iAttachCnt = .Count
If iAttachCnt > 0 Then
For iCtr = 1 To iAttachCnt
.Item(iCtr).SaveAsFile "C:\INPUT\" & .Item(iCtr).FileName
Next iCtr
End If
End With
End With
DoEvents
Next
End Sub
-
Apr 18th, 2008, 10:42 AM
#3
Re: Outlook attachments copy to hard-drive
Try this (for downloading only zip files)
after
With oMessage.Attachments
insert a code (i am doing this from memory as I don't have outlook)
If right(attachment name,3)="zip" then
Do the save. you need to replace "attachment name" with the right code...
Hope i understood your query correctly...
For looping thru all the mailboxes, if you do a search in the forums, you will find plenty of examples...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Apr 19th, 2008, 05:32 AM
#4
Thread Starter
Addicted Member
Re: Outlook attachments copy to hard-drive
Thanks mate, that put me on the right track...I used the following code to achieve looking for only certain extensions:
Code:
If Right(Atmt.FileName, 3) = "ZIP" Or Right(Atmt.FileName, 3) = "zip" Then
'..............CONTINUE CODE
I put ZIP and zip as I had an issue with the extensions if they where in zip format, so I just included upper and lower case to be safe...
I still have a slight issue though..My code only seems to obtain the files from the default Mailbox...But I want to be able to define which mailbox to check. Anyone have any ideas what may be going wrong here..I'm using the following code:
Code:
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("ZIP FILES")
The code above will check my default inbox for a ZIP FILES folder and obtains the zip files. However I cannot seem to work out how to obtain files from anything other than the default mailbox. There seems to be a fair few options I could use to achieve this but really not sure..Any help appreciated..
-
Apr 19th, 2008, 05:37 AM
#5
Thread Starter
Addicted Member
Re: Outlook attachments copy to hard-drive
I'm guessing that I may need to do something like this
Code:
set Mailbox = Mb.MailboxAccount("NAME OF MAILBOX")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("ZIP FILES")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|