Fred Blogs
12-30-2018, 07:50 AM
Hi there,
I am not an experienced coder in any way, but have pulled together from various code snippets around the internet a VBA macro which automatically saves incoming emails to the hard drive. This has been working well for over a year but I now need it to work on a non-default mailbox and can't work out how to change the code. Here is the key extract from the macro with the two lines I need to change highlighted with red text:-
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
Dim objSent As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing
End Sub
The red lines needs to refer to a specific mailbox (say "abc at xyz dot com" *) and the "Inbox" and "Sent Items" folders contained therein. I have spent hours, days even, scouring the web and found loads of "non default folder" VBA information in forums etc, however, if I read them correctly (and I repeat I am not an experienced coder) they seem to suggest I need 30-40 lines of code to achieve what I seek and this seems unlikely.
Is there a way to replace the "GetDefaultFolder" bits in the two lines above with the specific mailbox name (eg "abc at xyz dot com" *) and the "olFolderxxxxxxx" bits with their specific folder names (ie "Inbox" and "Sent Items")?
Thanks for reading and for any advice you can provide. Apologies is this is covered by an existing post - I did look first though.
* NB Forum won't let me post this as correctly formatted email address, but I'm sure you understand what I mean.
Regards
Fred
PS I have another (very minor) tweak I am looking for assistance with. Is there a (simple) way of limiting the length of the string to the first 20 characters returned by this line of code?
sName = oMail.Subject
Some emails contain VERY long subject lines and this can cause a filename length error when saving.
I am not an experienced coder in any way, but have pulled together from various code snippets around the internet a VBA macro which automatically saves incoming emails to the hard drive. This has been working well for over a year but I now need it to work on a non-default mailbox and can't work out how to change the code. Here is the key extract from the macro with the two lines I need to change highlighted with red text:-
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
Dim objSent As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing
End Sub
The red lines needs to refer to a specific mailbox (say "abc at xyz dot com" *) and the "Inbox" and "Sent Items" folders contained therein. I have spent hours, days even, scouring the web and found loads of "non default folder" VBA information in forums etc, however, if I read them correctly (and I repeat I am not an experienced coder) they seem to suggest I need 30-40 lines of code to achieve what I seek and this seems unlikely.
Is there a way to replace the "GetDefaultFolder" bits in the two lines above with the specific mailbox name (eg "abc at xyz dot com" *) and the "olFolderxxxxxxx" bits with their specific folder names (ie "Inbox" and "Sent Items")?
Thanks for reading and for any advice you can provide. Apologies is this is covered by an existing post - I did look first though.
* NB Forum won't let me post this as correctly formatted email address, but I'm sure you understand what I mean.
Regards
Fred
PS I have another (very minor) tweak I am looking for assistance with. Is there a (simple) way of limiting the length of the string to the first 20 characters returned by this line of code?
sName = oMail.Subject
Some emails contain VERY long subject lines and this can cause a filename length error when saving.