Andy
2010-10-19 12:40:41 UTC
Hey there!
I'm currently working on a project that involves merging data from
many different csv files into one Excel workbook for analysis.
The csv files are sent to a public folder (outlook:\\Public Folders
\All Public Folders\etc...)
I thought the easiest way to do this would be to import the data
straight from Outlook to the Excel file but after much painful testing
I couldn't get it to work so I will settle for a way to copy the files
from the public folder to my H drive.
I have code to do this but it only seems to work on my main mailbox
folders, not public and am unsure why... I have almost no experience
with Outlook coding so any help is appreciated!
This is the current code:
If there is a way to input which public folder is copied from instead
of using a selection, that's fine too!
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
strFolderpath = "H:\Merge Tool\Import from Outlook\"
On Error Resume Next
If MsgBox("Are you sure?", vbYesNo, "Copying files") = vbNo Then
Exit Sub
Else
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' Check each selected item for attachments. If attachments exist,
' save them to the Temp folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
'Use the MsgBox command to troubleshoot. Remove it from the final
code.
' MsgBox objAttachments.Count
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment. Commented out for testing purposes
' objAttachments.Item(i).Delete
' objMsg.Delete
Next i
End If
Next
End If
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
I'm currently working on a project that involves merging data from
many different csv files into one Excel workbook for analysis.
The csv files are sent to a public folder (outlook:\\Public Folders
\All Public Folders\etc...)
I thought the easiest way to do this would be to import the data
straight from Outlook to the Excel file but after much painful testing
I couldn't get it to work so I will settle for a way to copy the files
from the public folder to my H drive.
I have code to do this but it only seems to work on my main mailbox
folders, not public and am unsure why... I have almost no experience
with Outlook coding so any help is appreciated!
This is the current code:
If there is a way to input which public folder is copied from instead
of using a selection, that's fine too!
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
strFolderpath = "H:\Merge Tool\Import from Outlook\"
On Error Resume Next
If MsgBox("Are you sure?", vbYesNo, "Copying files") = vbNo Then
Exit Sub
Else
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' Check each selected item for attachments. If attachments exist,
' save them to the Temp folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
'Use the MsgBox command to troubleshoot. Remove it from the final
code.
' MsgBox objAttachments.Count
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment. Commented out for testing purposes
' objAttachments.Item(i).Delete
' objMsg.Delete
Next i
End If
Next
End If
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub