Results 1 to 24 of 24

Thread: vba code for outlook ----email saving

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    vba code for outlook ----email saving

    while saving an email message in outlook i would like to build a macro in outlook so that i can do the following:

    1)extract the date from the email in reverse ie yyyy-mm--dd

    2) user input for the filename

    3) extract the subject heading of the email into the saving field

    so combining 1,2,3 the email wud be saved with the resulting name.

    can somebody help me with it.

    i am very new to macro coding for vba.

    thanking you

    cheers!!!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    Its a bit of code but mostly for invoking the procedure. It may be simplier to select the email and click a custom menu item to invoke the save and prompt you for the info. Does this sound ok?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: vba code for outlook ----email saving

    Quote Originally Posted by RobDog888
    Its a bit of code but mostly for invoking the procedure. It may be simplier to select the email and click a custom menu item to invoke the save and prompt you for the info. Does this sound ok?

    hi ,
    thanks for replying.

    well can u plz elaborate on what u have said.

    i am using outlook 2003 , i dont think there is any custom menu which can help me save the email messages in my desired format name.

    thanks


    cheers!!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    No there isnt, we will have to create one via code.

    If I get some time I will write an example for you.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: vba code for outlook ----email saving

    Quote Originally Posted by RobDog888
    No there isnt, we will have to create one via code.

    If I get some time I will write an example for you.

    oh thanks a lot!!
    cheerS!!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: vba code for outlook ----email saving

    Quote Originally Posted by RobDog888
    No there isnt, we will have to create one via code.

    If I get some time I will write an example for you.

    can u just give an eg or a way so that i can get it starting adn perhaps post whatever i code over here so that it saves our time of developing the whole code.

    thanks

    cheers!!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: vba code for outlook ----email saving

    hello???

    sumbody help

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    Sorry for the delay but I havent been online lately.

    Here it is. Add this to your ThisOutlookSession and make sure Macros are enabled.

    With this, you make a selection of the email you want to save. Then go into the "Tools > Save Email As.." custom menu item and click it. Then the item will be saved using the subject. You can add on the reverse date and a user prompt for the filename if you want. Here is the major functionality you asked for.

    You may also want to error trap for a MailItem and multiple selections, etc.

    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Private oCBTools As Office.CommandBarPopup
    4. Private oCBSaveMe As Office.CommandBarButton
    5.  
    6. Public WithEvents oSaveAs As Office.CommandBarButton
    7.  
    8. Private Sub SyncButton(btn As Office.CommandBarButton)
    9.     Set oSaveAs = btn
    10.     If btn Is Nothing Then
    11.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    12.     End If
    13. End Sub
    14.  
    15. Private Sub Application_MAPILogonComplete()
    16.     Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    17.     Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    18.     If TypeName(oCBSaveMe) = "Nothing" Then
    19.         Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
    20.     End If
    21.     With oCBSaveMe
    22.         .BeginGroup = True
    23.         .Caption = "Save Email As..."
    24.         .Enabled = True
    25.         .Style = msoControlCustom
    26.         .Tag = "888"
    27.         .Visible = True
    28.     End With
    29.     Call SyncButton(oCBSaveMe)
    30. End Sub
    31.  
    32. Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    33.    
    34.     Dim oSel As Outlook.Selection
    35.     Dim oEmail As Outlook.MailItem
    36.     Dim sSub As String
    37.    
    38.     Set oSel = Application.ActiveExplorer.Selection
    39.     If oSel.Class = olMail Then
    40.         Set oEmail = oSel
    41.         'Use the email subject as the filename
    42.         sSub = oEmail.Subject
    43.         'Parse out any invalid characters for a filename.
    44.         sSub = Replace(sSub, ":", vbNullString)
    45.         sSub = Replace(sSub, "*", vbNullString)
    46.         sSub = Replace(sSub, "/", vbNullString)
    47.         sSub = Replace(sSub, "\", vbNullString)
    48.         sSub = Replace(sSub, "?", vbNullString)
    49.         sSub = Replace(sSub, "<", vbNullString)
    50.         sSub = Replace(sSub, ">", vbNullString)
    51.         sSub = Replace(sSub, "|", vbNullString)
    52.         sSub = Replace(sSub, Chr(34), vbNullString)
    53.         'Add code for checking for duplicate filenames and implement a index number if so
    54.         oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
    55.         Set oEmail = Nothing
    56.         MsgBox "Saved"
    57.     End If
    58.     Set oSel = Nothing
    59.    
    60. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    New Member
    Join Date
    May 2006
    Posts
    5

    Exclamation Re: vba code for outlook ----email saving

    Hi There,

    This is exactly what I was looking for - except I can't get it to work!!

    I have added it to ThisOutlookSession and the "Save Email As" appears in the tools menu.

    But when I select an email and then click on the command nothing happens.

    I did enter a ' before all the lines in the Private Sub oSaveAs_Click except for the msgbox, and when I click it displays the box.

    Can you help? (I am using Outlook 2003)

    Thanks a million

    Cillian

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    Welcome to the Forums.

    Did you add all the rest of the code? Do you have Macros enabled? did you restart Outlook?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    New Member
    Join Date
    May 2006
    Posts
    5

    Re: vba code for outlook ----email saving

    Hi There,

    Thanks for the fast reply -

    I have definately added all of the code in your post.

    I have set my macro security to low.

    I have re-strated a number of times (each time I made a change to your original)

    This might sound a bit simple but - how do you select the email? I have myu inbox open and am clicking on them in the list of emails - is this right?

    Thanks again

    Cillian

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    Yes, that is how and just try a single selection first as I didnt code it to handle multiple selections.

    Just set your Macro security to Medium and when outlook starts just select Enable. This is safer then Low.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    New Member
    Join Date
    May 2006
    Posts
    5

    Re: vba code for outlook ----email saving

    Hi again,

    Still nothing :-(

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    You did get the menu item?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    This is all you should have as a test to make sure the click event works.

    After you paste it in, save and close it and log off Outlook and exit it. It should not be showing as running in your task manager at all.

    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Private oCBTools As Office.CommandBarPopup
    4. Private oCBSaveMe As Office.CommandBarButton
    5.  
    6. Public WithEvents oSaveAs As Office.CommandBarButton
    7.  
    8. Private Sub SyncButton(btn As Office.CommandBarButton)
    9.     Set oSaveAs = btn
    10.     If btn Is Nothing Then
    11.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    12.     End If
    13. End Sub
    14.  
    15. Private Sub Application_MAPILogonComplete()
    16.     Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    17.     Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    18.     If TypeName(oCBSaveMe) = "Nothing" Then
    19.         Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
    20.     End If
    21.     With oCBSaveMe
    22.         .BeginGroup = True
    23.         .Caption = "Save Email As..."
    24.         .Enabled = True
    25.         .Style = msoControlCustom
    26.         .Tag = "888"
    27.         .Visible = True
    28.     End With
    29.     Call SyncButton(oCBSaveMe)
    30. End Sub
    31.  
    32. Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    33.     MsgBox "Saved"
    34. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    New Member
    Join Date
    May 2006
    Posts
    5

    Re: vba code for outlook ----email saving

    Hi,

    OKay - That definately works - when I click I get the Saved message box

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    Ok, there must be an error from having multiple selections. Update your click event to this new one with error handling.
    VB Code:
    1. Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    2.    
    3.     On Error GoTo MyError
    4.  
    5.     Dim oSel As Outlook.Selection
    6.     Dim oEmail As Outlook.MailItem
    7.     Dim sSub As String
    8.    
    9. 100    Set oSel = Application.ActiveExplorer.Selection
    10. 110    If oSel.Class = olMail Then
    11. 120        Set oEmail = oSel
    12.            'Use the email subject as the filename
    13. 130        sSub = oEmail.Subject
    14.            'Parse out any invalid characters for a filename.
    15.            sSub = Replace(sSub, ":", vbNullString)
    16.            sSub = Replace(sSub, "*", vbNullString)
    17.            sSub = Replace(sSub, "/", vbNullString)
    18.            sSub = Replace(sSub, "\", vbNullString)
    19.            sSub = Replace(sSub, "?", vbNullString)
    20.            sSub = Replace(sSub, "<", vbNullString)
    21.            sSub = Replace(sSub, ">", vbNullString)
    22.            sSub = Replace(sSub, "|", vbNullString)
    23.            sSub = Replace(sSub, Chr(34), vbNullString)
    24.            'Add code for checking for duplicate filenames and implement a index number if so
    25. 140        oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
    26.            Set oEmail = Nothing
    27.            MsgBox "Saved"
    28.     End If
    29.     Set oSel = Nothing
    30.     Exit Sub
    31.        
    32. MyError:
    33.     MsgBox Err.Number & " - " & Err.Description & vbNewline & "On Line: " & Erl, vbOkOnly
    34.  
    35. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18
    New Member
    Join Date
    May 2006
    Posts
    5

    Re: vba code for outlook ----email saving

    Hi There,

    I really appreciate your help with this - but still nothing happens when I click Save-Emails as. I only have 1 email selected.

    This is the code currently in "ThisOutlookSession"

    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Private oCBTools As Office.CommandBarPopup
    4. Private oCBSaveMe As Office.CommandBarButton
    5.  
    6. Public WithEvents oSaveAs As Office.CommandBarButton
    7.  
    8. Private Sub SyncButton(btn As Office.CommandBarButton)
    9.     Set oSaveAs = btn
    10.     If btn Is Nothing Then
    11.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    12.     End If
    13. End Sub
    14.  
    15. Private Sub Application_MAPILogonComplete()
    16.     Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    17.     Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    18.     If TypeName(oCBSaveMe) = "Nothing" Then
    19.         Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
    20.     End If
    21.     With oCBSaveMe
    22.         .BeginGroup = True
    23.         .Caption = "Save Email As..."
    24.         .Enabled = True
    25.         .Style = msoControlCustom
    26.         .Tag = "888"
    27.         .Visible = True
    28.     End With
    29.     Call SyncButton(oCBSaveMe)
    30. End Sub
    31.  
    32. Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    33.    
    34.     On Error GoTo MyError
    35.  
    36.     Dim oSel As Outlook.Selection
    37.     Dim oEmail As Outlook.MailItem
    38.     Dim sSub As String
    39.    
    40. 100    Set oSel = Application.ActiveExplorer.Selection
    41. 110    If oSel.Class = olMail Then
    42. 120        Set oEmail = oSel
    43.            'Use the email subject as the filename
    44. 130        sSub = oEmail.Subject
    45.            'Parse out any invalid characters for a filename.
    46.            sSub = Replace(sSub, ":", vbNullString)
    47.            sSub = Replace(sSub, "*", vbNullString)
    48.            sSub = Replace(sSub, "/", vbNullString)
    49.            sSub = Replace(sSub, "\", vbNullString)
    50.            sSub = Replace(sSub, "?", vbNullString)
    51.            sSub = Replace(sSub, "<", vbNullString)
    52.            sSub = Replace(sSub, ">", vbNullString)
    53.            sSub = Replace(sSub, "|", vbNullString)
    54.            sSub = Replace(sSub, Chr(34), vbNullString)
    55.            'Add code for checking for duplicate filenames and implement a index number if so
    56. 140        oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
    57.            Set oEmail = Nothing
    58.            MsgBox "Saved"
    59.     End If
    60.     Set oSel = Nothing
    61.     Exit Sub
    62.        
    63. MyError:
    64.     MsgBox Err.Number & " - " & Err.Description & vbNewLine & "On Line: " & Erl, vbOKOnly
    65.  
    66. End Sub
    Last edited by RobDog888; May 17th, 2006 at 11:56 AM. Reason: Added [vbcode] tags

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vba code for outlook ----email saving

    What do you mean "nothing happens"? Can you be more descriptive?

    Place a breakpoint on line 110 and then go back and make a single selection, click on the menu and then step through the code by pressing F8 once per line and see where it processes.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: vba code for outlook ----email saving

    Hi guys !

    Sorry if I bring back an old post...

    Im trying to use this code, but I've got a runtime error 13 type mismatch on this line: Set oEmail = oSel

    I dont know if it's because I use Office 2007...

    Code:
    Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    
    Dim oSel As Outlook.Selection
    Dim oEmail As Outlook.MailItem
    Dim sSub As String
    
    Set oSel = Application.ActiveExplorer.Selection
    
    'If oSel.Class = olMail Then
    'I use=74 instead because it doesnt enter in the condition above !
    If oSel.Class = 74 Then
            Set oEmail = oSel
            'Use the email subject as the filename
            sSub = oEmail.Subject
            'Parse out any invalid characters for a filename.
            sSub = Replace(sSub, ":", vbNullString)
            sSub = Replace(sSub, "*", vbNullString)
            sSub = Replace(sSub, "/", vbNullString)
            sSub = Replace(sSub, "\", vbNullString)
            sSub = Replace(sSub, "?", vbNullString)
            sSub = Replace(sSub, "<", vbNullString)
            sSub = Replace(sSub, ">", vbNullString)
            sSub = Replace(sSub, "|", vbNullString)
            sSub = Replace(sSub, Chr(34), vbNullString)
            'Add code for checking for duplicate filenames and implement a index number if so
            oEmail.SaveAs "C:\Documents and Settings\username\Bureau\" & sSub & ".txt", olTXT
            Set oEmail = Nothing
            MsgBox "Saved"
    End If
    
    Set oSel = Nothing
       
    End Sub
    Thanks in advance !!!
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  21. #21
    Addicted Member
    Join Date
    Apr 2009
    Location
    Toronto, Ontario
    Posts
    242

    Re: vba code for outlook ----email saving

    oEmail is defined as a mailItem and at the point where you get the error, the object assigned to oSel is probably not a mailItem (could be a meeting Request, task assignment, for e.g.).

    Before assigning to oEmail, test oSel's type to determine if it is a MailItem or not.

    Cheers.
    -EM.
    ---
    REMEMBER: If your issue is resolved, use the Thread Tools menu to set it as such, and be sure to rate the posts that help you the most!


    Just because I was jealous of g4hsean!

  22. #22
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: vba code for outlook ----email saving

    The oSel type is checked by these lines:

    Code:
    'If oSel.Class = olMail Then 'Note: olMail = 43 for now oSel is olSelection = 74
    
    If oSel.Class = 74 Then
            Set oEmail = oSel
            'Use the email subject as the filename
            sSub = oEmail.Subject
            .........
    oSel is not ok, I dont kwow why ???

    I'm really sure about one thing, the selected item it's a message...

    Thanks in advance !
    Last edited by DubweiserTM; Nov 30th, 2009 at 03:35 PM.
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  23. #23
    Addicted Member
    Join Date
    Apr 2009
    Location
    Toronto, Ontario
    Posts
    242

    Re: vba code for outlook ----email saving

    I think you've identified your own problem here:
    Code:
    'Note: olMail = 43 for now oSel is olSelection = 74
    the code in the IF should only execute for an object that is a MailItem. Since, as you specify, oSel.Class is an olSelection, then you get the type mismatch error (i.e. olSelection <> olMail).

    I think you may need to rewrite your code to use the Selection.Item instead:
    Code:
    Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    
    Dim oEmail As Outlook.MailItem
    Dim sSub As String
    
    'Check if first selected item is a MailItem.
    if Application.ActiveExplorer.Selection.Item(1).Class = olMail Then
            Set oEmail = Application.ActiveExplorer.Selection.Item(1)
            'Use the email subject as the filename
            sSub = oEmail.Subject
            'Parse out any invalid characters for a filename.
            sSub = Replace(sSub, ":", vbNullString)
            sSub = Replace(sSub, "*", vbNullString)
            sSub = Replace(sSub, "/", vbNullString)
            sSub = Replace(sSub, "\", vbNullString)
            sSub = Replace(sSub, "?", vbNullString)
            sSub = Replace(sSub, "<", vbNullString)
            sSub = Replace(sSub, ">", vbNullString)
            sSub = Replace(sSub, "|", vbNullString)
            sSub = Replace(sSub, Chr(34), vbNullString)
            'Add code for checking for duplicate filenames and implement a index number if so
            oEmail.SaveAs "C:\Documents and Settings\username\Bureau\" & sSub & ".txt", olTXT
            Set oEmail = Nothing
            MsgBox "Saved"
    End If
       
    End Sub
    Hope this helps.
    -EM
    ---
    REMEMBER: If your issue is resolved, use the Thread Tools menu to set it as such, and be sure to rate the posts that help you the most!


    Just because I was jealous of g4hsean!

  24. #24
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: vba code for outlook ----email saving

    You're right !

    Resolved for me, but I'm not the thread starter !

    Thanks !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width