|
-
Aug 23rd, 2005, 12:26 PM
#1
Thread Starter
Hyperactive Member
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!!!
-
Aug 23rd, 2005, 12:40 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 24th, 2005, 04:03 AM
#3
Thread Starter
Hyperactive Member
Re: vba code for outlook ----email saving
 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!!
-
Aug 24th, 2005, 03:11 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 25th, 2005, 03:45 AM
#5
Thread Starter
Hyperactive Member
Re: vba code for outlook ----email saving
 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!!
-
Aug 26th, 2005, 05:19 AM
#6
Thread Starter
Hyperactive Member
Re: vba code for outlook ----email saving
 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!!
-
Sep 5th, 2005, 02:38 PM
#7
Thread Starter
Hyperactive Member
Re: vba code for outlook ----email saving
-
Sep 6th, 2005, 01:55 PM
#8
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:
Option Explicit
'Behind ThisOutlookSession
Private oCBTools As Office.CommandBarPopup
Private oCBSaveMe As Office.CommandBarButton
Public WithEvents oSaveAs As Office.CommandBarButton
Private Sub SyncButton(btn As Office.CommandBarButton)
Set oSaveAs = btn
If btn Is Nothing Then
MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
End If
End Sub
Private Sub Application_MAPILogonComplete()
Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
If TypeName(oCBSaveMe) = "Nothing" Then
Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
End If
With oCBSaveMe
.BeginGroup = True
.Caption = "Save Email As..."
.Enabled = True
.Style = msoControlCustom
.Tag = "888"
.Visible = True
End With
Call SyncButton(oCBSaveMe)
End Sub
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
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:\MyEmails\" & sSub & ".txt", olTXT
Set oEmail = Nothing
MsgBox "Saved"
End If
Set oSel = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 16th, 2006, 12:44 PM
#9
New Member
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
-
May 16th, 2006, 12:50 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 16th, 2006, 12:56 PM
#11
New Member
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
-
May 16th, 2006, 01:03 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 16th, 2006, 01:16 PM
#13
New Member
Re: vba code for outlook ----email saving
Hi again,
Still nothing :-(
-
May 16th, 2006, 01:22 PM
#14
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 16th, 2006, 01:25 PM
#15
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:
Option Explicit
'Behind ThisOutlookSession
Private oCBTools As Office.CommandBarPopup
Private oCBSaveMe As Office.CommandBarButton
Public WithEvents oSaveAs As Office.CommandBarButton
Private Sub SyncButton(btn As Office.CommandBarButton)
Set oSaveAs = btn
If btn Is Nothing Then
MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
End If
End Sub
Private Sub Application_MAPILogonComplete()
Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
If TypeName(oCBSaveMe) = "Nothing" Then
Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
End If
With oCBSaveMe
.BeginGroup = True
.Caption = "Save Email As..."
.Enabled = True
.Style = msoControlCustom
.Tag = "888"
.Visible = True
End With
Call SyncButton(oCBSaveMe)
End Sub
Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "Saved"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 17th, 2006, 04:05 AM
#16
New Member
Re: vba code for outlook ----email saving
Hi,
OKay - That definately works - when I click I get the Saved message box
-
May 17th, 2006, 04:42 AM
#17
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:
Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
On Error GoTo MyError
Dim oSel As Outlook.Selection
Dim oEmail As Outlook.MailItem
Dim sSub As String
100 Set oSel = Application.ActiveExplorer.Selection
110 If oSel.Class = olMail Then
120 Set oEmail = oSel
'Use the email subject as the filename
130 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
140 oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
Set oEmail = Nothing
MsgBox "Saved"
End If
Set oSel = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description & vbNewline & "On Line: " & Erl, vbOkOnly
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 17th, 2006, 05:00 AM
#18
New Member
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:
Option Explicit
'Behind ThisOutlookSession
Private oCBTools As Office.CommandBarPopup
Private oCBSaveMe As Office.CommandBarButton
Public WithEvents oSaveAs As Office.CommandBarButton
Private Sub SyncButton(btn As Office.CommandBarButton)
Set oSaveAs = btn
If btn Is Nothing Then
MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
End If
End Sub
Private Sub Application_MAPILogonComplete()
Set oCBTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
Set oCBSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
If TypeName(oCBSaveMe) = "Nothing" Then
Set oCBSaveMe = oCBTools.Controls.Add(msoControlButton, 1, "888", , True)
End If
With oCBSaveMe
.BeginGroup = True
.Caption = "Save Email As..."
.Enabled = True
.Style = msoControlCustom
.Tag = "888"
.Visible = True
End With
Call SyncButton(oCBSaveMe)
End Sub
Private Sub oSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
On Error GoTo MyError
Dim oSel As Outlook.Selection
Dim oEmail As Outlook.MailItem
Dim sSub As String
100 Set oSel = Application.ActiveExplorer.Selection
110 If oSel.Class = olMail Then
120 Set oEmail = oSel
'Use the email subject as the filename
130 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
140 oEmail.SaveAs "C:\MyEmails\" & sSub & ".txt", olTXT
Set oEmail = Nothing
MsgBox "Saved"
End If
Set oSel = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description & vbNewLine & "On Line: " & Erl, vbOKOnly
End Sub
Last edited by RobDog888; May 17th, 2006 at 11:56 AM.
Reason: Added [vbcode] tags
-
May 17th, 2006, 11:58 AM
#19
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 26th, 2009, 02:42 PM
#20
Hyperactive Member
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...
-
Nov 26th, 2009, 04:51 PM
#21
Addicted Member
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! 
-
Nov 30th, 2009, 11:52 AM
#22
Hyperactive Member
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...
-
Nov 30th, 2009, 03:59 PM
#23
Addicted Member
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! 
-
Nov 30th, 2009, 04:08 PM
#24
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|