sending selected email as attachment in new email

D

draco

Hi

I'm writing a macro in Outlook, and part of the functionality is to
reference the currently selected mail and to send it as an attachment
in another mail.

I know this functionality exists in Outlook, so it should be possible
to reproduce it in a macro.

So I have my function that sets up a mail:

'send mail
Dim objMail As Outlook.MailItem
Set objMail = ThisOutlookSession.CreateItem(olMailItem)
objMail.To = ...
objMail.Attachments.Add (GetSelectedMail)
objMail.Send

I'm not sure about that Attachments.Add function - it should specify
the parameter is a reference type rather than a file path but I'm not
sure how.

And here is the function to get the attachment:

Private Function GetSelectedMail() As Outlook.MailItem

Dim objSelection As Outlook.Selection
Set objSelection = ThisOutlookSession.ActiveExplorer.Selection
Dim objMailItem As Outlook.MailItem
If objSelection.Count = 0 Then
objMailItem = Nothing
Else
objMailItem = objSelection.Item(1)
End If
GetSelectedMail = objMailItem

End Function

This is not working. I get an error at this line:
objMailItem = objSelection.Item(1)
...object variable or with block variable not set...

Any ideas??
 
K

Ken Slovak - [MVP - Outlook]

Set objMailItem = objSelection.Item(1)

But I'd check for the Class of the selection item in case it's not an email
item (it could be a task request, a meeting request, etc.).

Also, I'd check for objSelection Is Nothing to make sure something is
selected.

Use objMail.Attachments.Add (GetSelectedMail, olByReference) to add an item
by reference.
 
D

draco

I managed to work out the first part by myself, but the extra tests
you proposed were very useful. Thanks very much.

Set objMailItem = objSelection.Item(1)

But I'd check for the Class of the selection item in case it's not an email
item (it could be a task request, a meeting request, etc.).

Also, I'd check for objSelection Is Nothing to make sure something is
selected.

Use objMail.Attachments.Add (GetSelectedMail, olByReference) to add an item
by reference.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm



I'm writing a macro in Outlook, and part of the functionality is to
reference the currently selected mail and to send it as an attachment
in another mail.
I know this functionality exists in Outlook, so it should be possible
to reproduce it in a macro.
So I have my function that sets up a mail:
'send mail
Dim objMail As Outlook.MailItem
Set objMail = ThisOutlookSession.CreateItem(olMailItem)
objMail.To = ...
objMail.Attachments.Add (GetSelectedMail)
objMail.Send
I'm not sure about that Attachments.Add function - it should specify
the parameter is a reference type rather than a file path but I'm not
sure how.
And here is the function to get the attachment:
Private Function GetSelectedMail() As Outlook.MailItem
Dim objSelection As Outlook.Selection
Set objSelection = ThisOutlookSession.ActiveExplorer.Selection
Dim objMailItem As Outlook.MailItem
If objSelection.Count = 0 Then
objMailItem = Nothing
Else
objMailItem = objSelection.Item(1)
End If
GetSelectedMail = objMailItem
End Function
This is not working. I get an error at this line:
objMailItem = objSelection.Item(1)
...object variable or with block variable not set...
Any ideas??- Hide quoted text -- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top