I know this is an Excel forum but ...

adambc

Board Regular
Joined
Jan 13, 2020
Messages
135
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having failed in my attempt to move a VBA macro that uses Dir from a local/network drive environment to OneDrive, I’ve now built a solution that sends an Outlook email/attachment to the intended recipient, but they still have to save the attachment to the designated folder ...

So I’ve been looking at automatically saving the attachment when the new mail is received in Outlook ...

There are several posts in several forums that tell me how to do this, but I cannot get anything to work ...

I want to save the attachment that I KNOW will be named NEWINCIDENT??????.xlsm (where ?????? is a unique string) to a named folder ...

I'm running Office 365 Home/Outlook Version 2002 (Build 12527.20278 Click-to-Run) ...

This is "typical" of the VBA code I've put into ThisOutlookSession ...
Code:
Public WithEvents olItems As Outlook.Items

Private Sub Application_Startup()
Set olItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)
Dim NewMail As Outlook.MailItem
Dim Atts As Attachments
Dim Att As Attachment
Dim strPath As String
Dim strName As String

If Item.Class = olMail Then
Set NewMail = Item
End If

Set Atts = Item.Attachments

If Atts.Count > 0 Then
For Each Att In Atts
'neither of the following 2 lines work
If Att.FileName = "NEWINCIDENT*.*" Then
'If InStr(LCase(Att.FileName), "NEWINCIDENT") > 0 Then
strPath = "C:\Users\User\Documents\$$$ADAM\New_Incidents_Submitted"
strName = NewMail.Subject & " " & Chr(45) & " " & Att.FileName
Att.SaveAsFile strPath & strName
End If
Next
End If
End Sub
... and Saved/restarted Outlook ...

... but it doesn't seem to do anything (at least not what I want it to!) ...

Can anyone help please - or is there a forum as responsive as this one that has a similar focus on Outlook?

Many thanks ...
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
51,001
Office Version
  1. 365
Platform
  1. Windows
You could also use
VBA Code:
If InStr(1, Att.Filename, "NEWINCIDENT", vbTextCompare) > 0 Then
saves converting the string
 

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
1,969
I was wishing to point out that @adambc had already the solution, but he failed when decided to compare a Lowercase string with an Uppercase one...
Bye
 

adambc

Board Regular
Joined
Jan 13, 2020
Messages
135
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Many thanks to both of you ...

Have made all the necessary changes (feeling a bit daft!) but it still doesn’t work!

I really don’t to (can’t) use Run a Script (involves a RegEdit since MS switched it off!) - but if WithEvents won’t work, I might not have any option?!!!
 

adambc

Board Regular
Joined
Jan 13, 2020
Messages
135
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Sorry, that should have read ...

“I really don’t want to (can’t) ...”
 

adambc

Board Regular
Joined
Jan 13, 2020
Messages
135
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

ERRATUM ... all working perfectly now!

Turns out I had a “rogue” Outlook Data File that was set as the default, but the emails were going into the “proper” Inbox and hence the macro wasn’t seeing them - with a bit of fiddling about, the rogue has gone!

Thanks again (for pointing out my shortcomings - I will get better over time; and with help from you/others for which I’m extremely grateful) ...
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
51,001
Office Version
  1. 365
Platform
  1. Windows
I was wishing to point out that @adambc had already the solution, but he failed when decided to compare a Lowercase string with an Uppercase one...
And I was just pointing out to the OP, that you don't need to do the conversion, which eliminates the possibility of that type of error :)

@adambc
Glad you sorted it & thanks for the feedback
 

adambc

Board Regular
Joined
Jan 13, 2020
Messages
135
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
@Fluff @Anthony47

As above, I fixed the problem I had by getting rid of the rogue Outlook Data File that was lurking around - and was set as the default ...

But ... I now need to move the code to the real user's Outlook - and point it at a specific account eg MyName@OrgName.com (which is not their default account) ...

My code (as above) starts ...

VBA Code:
Public WithEvents olItems As Outlook.Items

Private Sub Application_Startup()

Set olItems = Session.GetDefaultFolder(olFolderInbox).Items

End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)

Etc etc etc

... but can you help me Set olItems = {the Inbox of MyName@OrgName.com}.Items

Many thanks ...
 

Watch MrExcel Video

Forum statistics

Threads
1,119,005
Messages
5,575,520
Members
412,672
Latest member
Tupelo1984
Top