Auto saving Outlook attachments VBA





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I've been playing around with the below code in an attempt to save files which we receive daily in Outlook. The code seems to run fine, however, when I go to check the destination folder there are no attachments saved.



How can I modify the code to save the attachments to the specified folder?



 Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item

'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "made-up-email@some_domain.com") And _
(Msg.Subject = "Test") And _
(Msg.Attachments.Count >= 1) Then

'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String

'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "T:London File3 GroupClient ReportingTest"


' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att

' mark as read
Msg.UnRead = False
End If
End If
End Sub









share|improve this question




















  • 1





    attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

    – Tim Williams
    Jan 3 at 16:52













  • I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

    – Kyle Murray
    Jan 3 at 17:19













  • Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

    – Tim Williams
    Jan 3 at 17:23











  • No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

    – Kyle Murray
    Jan 3 at 17:28






  • 1





    "Run without errors" and "run as expected" aren't always the same thing ;-)

    – Tim Williams
    Jan 3 at 17:29


















1















I've been playing around with the below code in an attempt to save files which we receive daily in Outlook. The code seems to run fine, however, when I go to check the destination folder there are no attachments saved.



How can I modify the code to save the attachments to the specified folder?



 Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item

'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "made-up-email@some_domain.com") And _
(Msg.Subject = "Test") And _
(Msg.Attachments.Count >= 1) Then

'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String

'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "T:London File3 GroupClient ReportingTest"


' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att

' mark as read
Msg.UnRead = False
End If
End If
End Sub









share|improve this question




















  • 1





    attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

    – Tim Williams
    Jan 3 at 16:52













  • I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

    – Kyle Murray
    Jan 3 at 17:19













  • Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

    – Tim Williams
    Jan 3 at 17:23











  • No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

    – Kyle Murray
    Jan 3 at 17:28






  • 1





    "Run without errors" and "run as expected" aren't always the same thing ;-)

    – Tim Williams
    Jan 3 at 17:29














1












1








1








I've been playing around with the below code in an attempt to save files which we receive daily in Outlook. The code seems to run fine, however, when I go to check the destination folder there are no attachments saved.



How can I modify the code to save the attachments to the specified folder?



 Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item

'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "made-up-email@some_domain.com") And _
(Msg.Subject = "Test") And _
(Msg.Attachments.Count >= 1) Then

'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String

'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "T:London File3 GroupClient ReportingTest"


' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att

' mark as read
Msg.UnRead = False
End If
End If
End Sub









share|improve this question
















I've been playing around with the below code in an attempt to save files which we receive daily in Outlook. The code seems to run fine, however, when I go to check the destination folder there are no attachments saved.



How can I modify the code to save the attachments to the specified folder?



 Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item

'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderName = "made-up-email@some_domain.com") And _
(Msg.Subject = "Test") And _
(Msg.Attachments.Count >= 1) Then

'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String

'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "T:London File3 GroupClient ReportingTest"


' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att

' mark as read
Msg.UnRead = False
End If
End If
End Sub






vba outlook outlook-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 22:23









0m3r

8,11492555




8,11492555










asked Jan 3 at 16:44









Kyle MurrayKyle Murray

152




152








  • 1





    attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

    – Tim Williams
    Jan 3 at 16:52













  • I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

    – Kyle Murray
    Jan 3 at 17:19













  • Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

    – Tim Williams
    Jan 3 at 17:23











  • No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

    – Kyle Murray
    Jan 3 at 17:28






  • 1





    "Run without errors" and "run as expected" aren't always the same thing ;-)

    – Tim Williams
    Jan 3 at 17:29














  • 1





    attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

    – Tim Williams
    Jan 3 at 16:52













  • I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

    – Kyle Murray
    Jan 3 at 17:19













  • Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

    – Tim Williams
    Jan 3 at 17:23











  • No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

    – Kyle Murray
    Jan 3 at 17:28






  • 1





    "Run without errors" and "run as expected" aren't always the same thing ;-)

    – Tim Williams
    Jan 3 at 17:29








1




1





attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

– Tim Williams
Jan 3 at 16:52







attPath & Att should be attPath & "" & Att If you comment out your error handling the problem should be clearer

– Tim Williams
Jan 3 at 16:52















I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

– Kyle Murray
Jan 3 at 17:19







I followed these instructions but the files are still not saving! What am I missing? There's no error as the code runs without any errors flagged without error handling.

– Kyle Murray
Jan 3 at 17:19















Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

– Tim Williams
Jan 3 at 17:23





Have you placed breakpoints and tried to debug your code to see which parts are actually executing?

– Tim Williams
Jan 3 at 17:23













No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

– Kyle Murray
Jan 3 at 17:28





No - I will try and do this now. I'm just confused that if there is no error then this implies the code has run fine?

– Kyle Murray
Jan 3 at 17:28




1




1





"Run without errors" and "run as expected" aren't always the same thing ;-)

– Tim Williams
Jan 3 at 17:29





"Run without errors" and "run as expected" aren't always the same thing ;-)

– Tim Williams
Jan 3 at 17:29












1 Answer
1






active

oldest

votes


















1














This code should work, something you may not have done is added this to the ThisOutlookSession object. Don't add to a standard module.



Private WithEvents InboxItems As Outlook.Items
Const attPath As String = "T:London File3 GroupClient ReportingTest"

Private Sub Application_Startup()
Dim outlookApp As Outlook.Application: Set outlookApp = Outlook.Application
Dim objectNS As Outlook.NameSpace: Set objectNS = outlookApp.GetNamespace("MAPI")
Set InboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal Item As Object)
Dim Msg As Outlook.MailItem: Set Msg = Item
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Filename As String

If Not TypeName(Msg) = "MailItem" Then Exit Sub
If (Msg.SenderName = "made-up-email@some_domain.com") And (Msg.Subject = "Test") And (Msg.Attachments.Count >= 1) Then
Set myAttachments = Item.Attachments
Filename = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Filename
Msg.UnRead = False
End If
End Sub





share|improve this answer
























  • Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

    – Kyle Murray
    Jan 3 at 20:44













  • Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

    – Kyle Murray
    Jan 4 at 17:41











  • Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

    – Ryan Wildry
    Jan 4 at 18:15












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026433%2fauto-saving-outlook-attachments-vba%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This code should work, something you may not have done is added this to the ThisOutlookSession object. Don't add to a standard module.



Private WithEvents InboxItems As Outlook.Items
Const attPath As String = "T:London File3 GroupClient ReportingTest"

Private Sub Application_Startup()
Dim outlookApp As Outlook.Application: Set outlookApp = Outlook.Application
Dim objectNS As Outlook.NameSpace: Set objectNS = outlookApp.GetNamespace("MAPI")
Set InboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal Item As Object)
Dim Msg As Outlook.MailItem: Set Msg = Item
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Filename As String

If Not TypeName(Msg) = "MailItem" Then Exit Sub
If (Msg.SenderName = "made-up-email@some_domain.com") And (Msg.Subject = "Test") And (Msg.Attachments.Count >= 1) Then
Set myAttachments = Item.Attachments
Filename = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Filename
Msg.UnRead = False
End If
End Sub





share|improve this answer
























  • Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

    – Kyle Murray
    Jan 3 at 20:44













  • Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

    – Kyle Murray
    Jan 4 at 17:41











  • Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

    – Ryan Wildry
    Jan 4 at 18:15
















1














This code should work, something you may not have done is added this to the ThisOutlookSession object. Don't add to a standard module.



Private WithEvents InboxItems As Outlook.Items
Const attPath As String = "T:London File3 GroupClient ReportingTest"

Private Sub Application_Startup()
Dim outlookApp As Outlook.Application: Set outlookApp = Outlook.Application
Dim objectNS As Outlook.NameSpace: Set objectNS = outlookApp.GetNamespace("MAPI")
Set InboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal Item As Object)
Dim Msg As Outlook.MailItem: Set Msg = Item
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Filename As String

If Not TypeName(Msg) = "MailItem" Then Exit Sub
If (Msg.SenderName = "made-up-email@some_domain.com") And (Msg.Subject = "Test") And (Msg.Attachments.Count >= 1) Then
Set myAttachments = Item.Attachments
Filename = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Filename
Msg.UnRead = False
End If
End Sub





share|improve this answer
























  • Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

    – Kyle Murray
    Jan 3 at 20:44













  • Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

    – Kyle Murray
    Jan 4 at 17:41











  • Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

    – Ryan Wildry
    Jan 4 at 18:15














1












1








1







This code should work, something you may not have done is added this to the ThisOutlookSession object. Don't add to a standard module.



Private WithEvents InboxItems As Outlook.Items
Const attPath As String = "T:London File3 GroupClient ReportingTest"

Private Sub Application_Startup()
Dim outlookApp As Outlook.Application: Set outlookApp = Outlook.Application
Dim objectNS As Outlook.NameSpace: Set objectNS = outlookApp.GetNamespace("MAPI")
Set InboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal Item As Object)
Dim Msg As Outlook.MailItem: Set Msg = Item
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Filename As String

If Not TypeName(Msg) = "MailItem" Then Exit Sub
If (Msg.SenderName = "made-up-email@some_domain.com") And (Msg.Subject = "Test") And (Msg.Attachments.Count >= 1) Then
Set myAttachments = Item.Attachments
Filename = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Filename
Msg.UnRead = False
End If
End Sub





share|improve this answer













This code should work, something you may not have done is added this to the ThisOutlookSession object. Don't add to a standard module.



Private WithEvents InboxItems As Outlook.Items
Const attPath As String = "T:London File3 GroupClient ReportingTest"

Private Sub Application_Startup()
Dim outlookApp As Outlook.Application: Set outlookApp = Outlook.Application
Dim objectNS As Outlook.NameSpace: Set objectNS = outlookApp.GetNamespace("MAPI")
Set InboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub InboxItems_ItemAdd(ByVal Item As Object)
Dim Msg As Outlook.MailItem: Set Msg = Item
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Filename As String

If Not TypeName(Msg) = "MailItem" Then Exit Sub
If (Msg.SenderName = "made-up-email@some_domain.com") And (Msg.Subject = "Test") And (Msg.Attachments.Count >= 1) Then
Set myAttachments = Item.Attachments
Filename = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Filename
Msg.UnRead = False
End If
End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 18:08









Ryan WildryRyan Wildry

3,5911927




3,5911927













  • Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

    – Kyle Murray
    Jan 3 at 20:44













  • Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

    – Kyle Murray
    Jan 4 at 17:41











  • Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

    – Ryan Wildry
    Jan 4 at 18:15



















  • Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

    – Kyle Murray
    Jan 3 at 20:44













  • Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

    – Kyle Murray
    Jan 4 at 17:41











  • Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

    – Ryan Wildry
    Jan 4 at 18:15

















Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

– Kyle Murray
Jan 3 at 20:44







Hi, thanks for getting back to me... I used this code which again seems to run fine. However this time it saved a "thumbs.db" file in the specified folder. So it seems the script is running but not picking up the attachments. Any idea why?

– Kyle Murray
Jan 3 at 20:44















Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

– Kyle Murray
Jan 4 at 17:41





Hi @Ryan Wildry. Managed to get this working, thanks V much. Do you know how I would reference a subfolder within a shared mailbox instead of my own inbox?

– Kyle Murray
Jan 4 at 17:41













Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

– Ryan Wildry
Jan 4 at 18:15





Hey Kyle, glad this helped. If it did, feel free to mark as the accepted answer. Feel free to raise a new question for help on getting the right folder. On StackOverflow, we like to limit the post to a single topic.

– Ryan Wildry
Jan 4 at 18:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026433%2fauto-saving-outlook-attachments-vba%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window