Save only attachments with "*.doc"

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
213
Hi,

this is a crossed post from:

https://www.excelforum.com/outlook-...s/1186116-save-only-attachments-with-doc.html

There is no answer so I am trying here.

My macro is:

Hi Guys,


my macro is :


Code:
Public WithEvents FolderItems As Outlook.Items


Private Sub Application_Startup()
   Set FolderItems = Session.GetDefaultFolder(olFolderInbox).Folders("FolderTest").Items
End Sub


Private Sub FolderItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String


Dim myDestFolder As Outlook.Folder
Set myDestFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("FolderTest").Folders("processed")


sSaveFolder = "C:\temp"
    
[B]For Each oAttachment In Item.Attachments[/B]
[B]    If oAttachment.DisplayName Like "*.doc" Then[/B]
[B]        oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName[/B]
[B]    End If[/B]
[B]Next[/B]


Item.Move myDestFolder


End Sub

Where is a bold text there --> if is checking if oAttachment has word document ("*.doc").


and if it is in e-mail only the doc attachment should be saved into folder. But problem is that all attachments are saved,


how can i change that ?


Please help,
Jacek
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Jacek,

I'm not able to replicate the problem that I think you are describing, of all attachments being saved instead of just the ones that end in ".doc".

In order to get the code to work, I needed to add a path separator "/" between the sSaveFolder and the file name. If you didn't have that, then I would expect that any files that were saved were placed under the root C:\ folder.

You might try using the .FileName property of the attachment instead of .DisplayName. On the few files I tested, those properties were the same - but you might have some setting that make them different. I thought that might be due to the windows explorer setting of "Hide extensions for known file types", but when I temporarily checked that, the DisplayName still showed the file extensions.

To test the display and file names of the attachments being processed, add Debug.Print statements as shown below. These will print the names to the Immediate Window of the VB Editor.

Code:
sSaveFolder = "C:\temp"
    
For Each oAttachment In Item.Attachments
    Debug.Print oAttachment.DisplayName
    Debug.Print oAttachment.FileName
    If LCase$(oAttachment.FileName) Like "*.doc" Then
        oAttachment.SaveAsFile sSaveFolder & "\" & oAttachment.FileName
    End If
Next

You should also use LCase$() as shown above to have a non-case-sensitive comparison.
 
Last edited:
Upvote 0
Thank you very much Jerry,

wow your answer is amazing!

Good explanation !

Your code is working like a charm, thank you !

One more question.
LCase is making string to lower case.

What $ does mean?

Jacek
 
Upvote 0
One more question.
LCase is making string to lower case.

What $ does mean?

Jacek, I'm glad that helped.

The LCase$() function is more efficient than LCase() when the input argument is known to be a String data type. For your procedure, the benefit of using LCase$() is unnoticeable, but it's just a coding habit to use the most efficient method if it doesn't sacrifice simplicity or clarity.

I appreciate that you noted the Cross-Post in your OP. It would be good to also note the Cross-Post on ExcelForum.com so no one spends anytime responding to your question without being aware of this thread.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,793
Messages
6,174,635
Members
452,575
Latest member
Fstick546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top