Share via

How to Change Outlook Save Sent Message Folder Using VBA within Excel

Anonymous
2024-01-24T23:15:07+00:00

I am using VBA through Excel 365 to send the current workbook via Outlook email. It works fine, except that it only saves the message to the default sent items folder. I need the message to be saved in an alternate folder in Outlook. Cobbling together code from various sources, I came up with the following. The code runs without error, but the SaveSentMessage folder never gets updated. I don't know what I'm doing wrong.

Sub SendActiveWorkbookSavingToOtherFolder()

On Error Resume Next

Dim appOutlook As Object

Dim mItem As Object

Dim objNS As Object

Dim objFolder As Object

Set objNS = Application.getnamespace("MAPI")

Set appOutlook = GetObject(, "Outlook.Application") 'Bind to existing instance of Outlook

Set mItem = appOutlook.CreateItem(0) '<<---- number zero, not the letter

Set objFolder = objNS.GetDefaultFolder(5).Parent.Folders("Training") 'used value of 5 to get sent items default folder as shown at https://learn.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders

'objFolder always has a value of "Nothing" even after running the line above

'the folder "Training" is at the same level as Sent Items, Inbox, etc.

With mItem

  .To = "******@domain.com"      

  .Subject = ActiveWorkbook.Name 

 .Attachments.Add ActiveWorkbook.FullName

 .SaveSentMessageFolder = objFolder

  .send

End With

'clean up objects

Set mItem = Nothing

Set appOutlook = Nothing

End Sub

Microsoft 365 and Office | Excel | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Anonymous
    2024-01-25T16:18:38+00:00

    I have no idea on adding outlook reference automatically.

    I'd suggest you create a new thread in Stack Overflow which is special channel to handle VBA programming issue.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-01-25T14:16:17+00:00

    I did not have Outlook reference added. That fixed the user-defined type error. But, now I'm getting a new error. This one is on the "Set myNamespace = Application.GetNamespace("MAPI")" line. It gives an error "Object doesn't support this property or method".

    This file will be used by multiple people from different machines/accounts. Is there a way to check if someone has the Outlook reference added and then add it via code if needed? This needs to be "dummy proof" so all anyone has to do is open the workbook and click the button to run the macro.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-01-25T05:13:25+00:00

    Have you added Outlook reference?

    1. Click on Tools > References from the menu bar in VB editor.
    2. Scroll down the list of available references and check the box next to "Microsoft Outlook 16.0 Object Library"

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-01-25T03:28:51+00:00

    It appears as though that link is for code to be used within Outlook. I am using Excel to create and send the email. As a test, I copied the CreatePersonalContacts sample code and pasted it in a new module in my workbook. When I compile the project, I get an error on the very first line (Dim myNamespace As Outlook.Namespace). The compile error is "User-defined type not defined". I've seen and tested other code that dims variables as Outlook.Namespace or Outlook.Folder. I get the same "User-defined type not defined" error on every example I've copied and pasted. I think it is because I am running the code from Excel, not Outlook.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2024-01-25T03:01:03+00:00

    Check this link for saving sent item to subfolder.

    Folder.Folders property (Outlook) | Microsoft Learn

    Was this answer helpful?

    0 comments No comments