Issue with Class Module Outlook VBA












0















I am new to VBA and am trying to insert a class module to save an email that arrives to a sub-folder in the inbox called "My Folder" to a location on a sharedrive. I have the below code and have tried sending emails to test but it is not working and cannot figure out why. Any help would be greatly appreciated!



Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox)
Set InboxItems = olFolder.Folders("My Folder")
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "File Path on Share Drive will be entered here"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "||/|<|>|""|:|*|\|?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "" & xFileName & ".msg", olMSG
End If
Exit Sub
End Sub









share|improve this question























  • Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

    – ZorgoZ
    Jan 2 at 19:02













  • Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

    – Hoganator
    Jan 2 at 19:10











  • I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

    – ZorgoZ
    Jan 2 at 19:13











  • Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

    – ZorgoZ
    Jan 2 at 19:17











  • Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

    – ZorgoZ
    Jan 2 at 19:21
















0















I am new to VBA and am trying to insert a class module to save an email that arrives to a sub-folder in the inbox called "My Folder" to a location on a sharedrive. I have the below code and have tried sending emails to test but it is not working and cannot figure out why. Any help would be greatly appreciated!



Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox)
Set InboxItems = olFolder.Folders("My Folder")
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "File Path on Share Drive will be entered here"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "||/|<|>|""|:|*|\|?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "" & xFileName & ".msg", olMSG
End If
Exit Sub
End Sub









share|improve this question























  • Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

    – ZorgoZ
    Jan 2 at 19:02













  • Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

    – Hoganator
    Jan 2 at 19:10











  • I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

    – ZorgoZ
    Jan 2 at 19:13











  • Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

    – ZorgoZ
    Jan 2 at 19:17











  • Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

    – ZorgoZ
    Jan 2 at 19:21














0












0








0








I am new to VBA and am trying to insert a class module to save an email that arrives to a sub-folder in the inbox called "My Folder" to a location on a sharedrive. I have the below code and have tried sending emails to test but it is not working and cannot figure out why. Any help would be greatly appreciated!



Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox)
Set InboxItems = olFolder.Folders("My Folder")
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "File Path on Share Drive will be entered here"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "||/|<|>|""|:|*|\|?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "" & xFileName & ".msg", olMSG
End If
Exit Sub
End Sub









share|improve this question














I am new to VBA and am trying to insert a class module to save an email that arrives to a sub-folder in the inbox called "My Folder" to a location on a sharedrive. I have the below code and have tried sending emails to test but it is not working and cannot figure out why. Any help would be greatly appreciated!



Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox)
Set InboxItems = olFolder.Folders("My Folder")
End Sub

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "File Path on Share Drive will be entered here"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "||/|<|>|""|:|*|\|?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "" & xFileName & ".msg", olMSG
End If
Exit Sub
End Sub






vba outlook






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 18:55









HoganatorHoganator

104




104













  • Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

    – ZorgoZ
    Jan 2 at 19:02













  • Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

    – Hoganator
    Jan 2 at 19:10











  • I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

    – ZorgoZ
    Jan 2 at 19:13











  • Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

    – ZorgoZ
    Jan 2 at 19:17











  • Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

    – ZorgoZ
    Jan 2 at 19:21



















  • Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

    – ZorgoZ
    Jan 2 at 19:02













  • Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

    – Hoganator
    Jan 2 at 19:10











  • I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

    – ZorgoZ
    Jan 2 at 19:13











  • Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

    – ZorgoZ
    Jan 2 at 19:17











  • Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

    – ZorgoZ
    Jan 2 at 19:21

















Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

– ZorgoZ
Jan 2 at 19:02







Never would try such thing in Outlook, even less in VBA. But just one question: how would the application know to call InboxItems_ItemAdd? Where is it registered as event handler? If the method is fired, please be more specific on what exactly does not work and how exactly do you get notified about this. On the other hand: you set InboxItems then you overwrite its value immediately, why?

– ZorgoZ
Jan 2 at 19:02















Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

– Hoganator
Jan 2 at 19:10





Appreciate the response. Like I mentioned, I am new to VBA and am trying to piece together items that I find. It is certainly not my strong suit. I found a forum where people said the framework of the above worked for them in a similar manner, and I tried to tailor it to suit my needs. I just want outlook to save a copy of any email that comes to a specific folder to a location on a sharedrive. Are you saying you would never do such a thing? Thanks for your help

– Hoganator
Jan 2 at 19:10













I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

– ZorgoZ
Jan 2 at 19:13





I would use .net to write a plugin for outlook... but that's my way. It is not that it would not work, I am just not a VBA fan, if there are better methods...

– ZorgoZ
Jan 2 at 19:13













Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

– ZorgoZ
Jan 2 at 19:17





Shouldn't you use Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Folders('My Folder')? Are you sure, that ApplicationStartup is the place of initialization? Here docs.microsoft.com/en-us/office/vba/api/outlook.items.itemadd I see an Initialize_handler override(?).

– ZorgoZ
Jan 2 at 19:17













Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

– ZorgoZ
Jan 2 at 19:21





Just as option: docs.microsoft.com/en-us/visualstudio/vsto/…

– ZorgoZ
Jan 2 at 19:21












1 Answer
1






active

oldest

votes


















0














There were a few issues with the code that I saw. I have this working, make sure you add this to the ThisOutlookSession object in the VBA IDE.



Private WithEvents InboxItems As Outlook.Items

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 FolderPath As String: FolderPath = "YOUR PATH HERE"
Dim FileName As String
Static FSO As Object

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(FolderPath) = False Then FSO.CreateFolder FolderPath

With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = False
.Pattern = "||/|<|>|""|:|*|\|?"

If Item.Class = olMail Then
FileName = .Replace(Item.Subject, vbNullString)
Item.SaveAs FolderPath & FileName & ".msg", olMSG
End If

End With

End Sub





share|improve this answer


























  • Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

    – Hoganator
    Jan 2 at 21:37











  • The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

    – Ryan Wildry
    Jan 2 at 22:09













  • Oh, and triple check you added this to ThisOutlookSession not a module or a class.

    – Ryan Wildry
    Jan 3 at 2:04












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%2f54011701%2fissue-with-class-module-outlook-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









0














There were a few issues with the code that I saw. I have this working, make sure you add this to the ThisOutlookSession object in the VBA IDE.



Private WithEvents InboxItems As Outlook.Items

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 FolderPath As String: FolderPath = "YOUR PATH HERE"
Dim FileName As String
Static FSO As Object

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(FolderPath) = False Then FSO.CreateFolder FolderPath

With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = False
.Pattern = "||/|<|>|""|:|*|\|?"

If Item.Class = olMail Then
FileName = .Replace(Item.Subject, vbNullString)
Item.SaveAs FolderPath & FileName & ".msg", olMSG
End If

End With

End Sub





share|improve this answer


























  • Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

    – Hoganator
    Jan 2 at 21:37











  • The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

    – Ryan Wildry
    Jan 2 at 22:09













  • Oh, and triple check you added this to ThisOutlookSession not a module or a class.

    – Ryan Wildry
    Jan 3 at 2:04
















0














There were a few issues with the code that I saw. I have this working, make sure you add this to the ThisOutlookSession object in the VBA IDE.



Private WithEvents InboxItems As Outlook.Items

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 FolderPath As String: FolderPath = "YOUR PATH HERE"
Dim FileName As String
Static FSO As Object

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(FolderPath) = False Then FSO.CreateFolder FolderPath

With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = False
.Pattern = "||/|<|>|""|:|*|\|?"

If Item.Class = olMail Then
FileName = .Replace(Item.Subject, vbNullString)
Item.SaveAs FolderPath & FileName & ".msg", olMSG
End If

End With

End Sub





share|improve this answer


























  • Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

    – Hoganator
    Jan 2 at 21:37











  • The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

    – Ryan Wildry
    Jan 2 at 22:09













  • Oh, and triple check you added this to ThisOutlookSession not a module or a class.

    – Ryan Wildry
    Jan 3 at 2:04














0












0








0







There were a few issues with the code that I saw. I have this working, make sure you add this to the ThisOutlookSession object in the VBA IDE.



Private WithEvents InboxItems As Outlook.Items

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 FolderPath As String: FolderPath = "YOUR PATH HERE"
Dim FileName As String
Static FSO As Object

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(FolderPath) = False Then FSO.CreateFolder FolderPath

With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = False
.Pattern = "||/|<|>|""|:|*|\|?"

If Item.Class = olMail Then
FileName = .Replace(Item.Subject, vbNullString)
Item.SaveAs FolderPath & FileName & ".msg", olMSG
End If

End With

End Sub





share|improve this answer















There were a few issues with the code that I saw. I have this working, make sure you add this to the ThisOutlookSession object in the VBA IDE.



Private WithEvents InboxItems As Outlook.Items

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 FolderPath As String: FolderPath = "YOUR PATH HERE"
Dim FileName As String
Static FSO As Object

If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(FolderPath) = False Then FSO.CreateFolder FolderPath

With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = False
.Pattern = "||/|<|>|""|:|*|\|?"

If Item.Class = olMail Then
FileName = .Replace(Item.Subject, vbNullString)
Item.SaveAs FolderPath & FileName & ".msg", olMSG
End If

End With

End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 22:10

























answered Jan 2 at 21:14









Ryan WildryRyan Wildry

3,5611926




3,5611926













  • Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

    – Hoganator
    Jan 2 at 21:37











  • The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

    – Ryan Wildry
    Jan 2 at 22:09













  • Oh, and triple check you added this to ThisOutlookSession not a module or a class.

    – Ryan Wildry
    Jan 3 at 2:04



















  • Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

    – Hoganator
    Jan 2 at 21:37











  • The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

    – Ryan Wildry
    Jan 2 at 22:09













  • Oh, and triple check you added this to ThisOutlookSession not a module or a class.

    – Ryan Wildry
    Jan 3 at 2:04

















Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

– Hoganator
Jan 2 at 21:37





Hi Ryan, thanks so much! Unfortunately, it is still not working on my end. I have sent multiple test emails. I am in a corporate environment and have tried using Public instead of Private. Do you have any suggestions to get this working? Really appreciate your help

– Hoganator
Jan 2 at 21:37













The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

– Ryan Wildry
Jan 2 at 22:09







The code should work, however, few things to check.Make sure you've updated the FolderPath variable. Make sure macros are enabled all the time. See: support.office.com/en-us/article/…. If that doesn't work, SaveAs apparently can be finicky. You might need to use Redemption. See: stackoverflow.com/questions/29148947/…

– Ryan Wildry
Jan 2 at 22:09















Oh, and triple check you added this to ThisOutlookSession not a module or a class.

– Ryan Wildry
Jan 3 at 2:04





Oh, and triple check you added this to ThisOutlookSession not a module or a class.

– Ryan Wildry
Jan 3 at 2:04




















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%2f54011701%2fissue-with-class-module-outlook-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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter