Adding txt attachment to outbound email from memory

H

Hari Menon

Hello, I have a custom outlook form that takes a value from a custom field
and writes the values to a file. The code then attaches this file. Is
there a way to do this without writing the file to disk?

Here is my code:
Function Item_Send()
Dim myOlApp, myItem, fso, f1

Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(5)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\test.txt", True)

f1.WriteLine("Value
is:"+Item.GetInspector.ModifiedFormPages("Message").Controls("TxtValue1").Text)
f1.Close
Item.Attachments.Add("c:\WebPushRecipientDataAttachment.xml")
End Function

From the MSDN Documentation, it says the parameter to Item.Attachments.add
is: "Source Required Variant. The file (represented by the full path and
file name) OR item that constitutes the attachment."

Is it posible to build the "item that constitutes the attachment" in memory
and then attach it without using the file system?

Thanks
 
P

Peter Huang [MSFT]

Hi

Based on my research, from the outlook 2003/XP's Attachments.Add method as
below.

The Source is a String type, so it should be a path to the actually
attachment file.

Add method as it applies to the Attachments object.

Creates a new attachment in the Attachments collection and returns the new
attachment as an Attachment object.

expression.Add(Source, Type, Position, DisplayName)

expression Required. An expression that returns an Attachments collection
object.

Source Required String. The source of the attachment.

Type Optional String. The type of the attachment.

Position Optional String. In e-mail messages using Microsoft Outlook
Rich Text format, position where the attachment should be placed. A value
of 1 for the Position parameter specifies that the attachment should be
positioned at the beginning of the message body. A value 'n' greater than
the number of characters in the body of the e-mail item specifies that the
attachment should be placed at the end. A value of 0 makes the attachment
hidden.

DisplayName Optional String. This name is displayed in an Inspector
object for the attachment if the mail item is in Rich Text format. If the
mail item is in Plain Text or HTML format, then the attachment is displayed
using the name in the Source parameter.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol11/htm
l/olmthAdd_HV03081690.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol10/htm
l/olmthAdd.asp

Also if you have any concern, with the temp file, you may delete it after
you did not need it.
e.g. Kill <Source>

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hari Menon

I think you have explained how to use the Add method to add an attachment to
the Attachments object.

But this method adds a file on disk (it has to be a physical file on disk)
as the attachment.

What I need to know is whether it is possible to create an attachment
dynamically (programatically) and add it to the email.

For example, I would like to create a file attachment called 'hello.txt'
(containing the string "Hello World") to an email. Questions:

1. Do I have to first programatically create a temporary file on disk called
hello.txt and then use the Attachments.Add method to add this temp file to
the email?

2. Instead, can I programatically create a file attachment in memory
(without physically writing the file out to the disk) and call some API to
add that attachment to the email? Note: Here I am trying to avoid trying to
create temp files on disk.

3. If the answer to question 2 is NO, then I guess I will have to create the
temporary file on disk. Is there an API in outlook that will allow me to
create a temporary file on disk without worrying about file create/write
permissions (regardless of whether the logged in user is a restricted user or
an administrator) ? If such an API does not exist, then what is the best way
to create this temp file on disk (without worrying about file create/write
permissions) and later, after attaching it to the email, delete it ?

Thanks
Hari
 
P

Peter Huang [MSFT]

Hi

Yes, from the VBA help, the Outlook OM's Attachments.Add need a path to the
actually physical file. )refer to the link before.
For a temp file, I think usually you may try to put in the temp dir. %temp%
which will pointed to dir similar with below.
C:\DOCUME~1\<username>\LOCALS~1\Temp

Also because the outlook is built based on MAPI which is a lowlevel API
which may help to create memory attachment.
For MAPI issue, you may try to post in the newsgroup below.
microsoft.public.win32.programmer.mapi
or
microsoft.public.platformsdk.mapi


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

scw-tzg

I believe 'item that constitutes the attachment' refers to an Outlook item.
Could you use an Outlook Note item rather than a file for your attachment?
Create the note. Attach it to your email. Then delete the note.
When I tried this - in Outlook, not via automation --unfortunately
I could not save the attachment as Text only - it inserted
the last modified date of the note!?
Just a thought...
 
H

Hari Menon

Hi Peter,

I tried posting teh question in the microsoft.public.platformsdk.mapi but
haven't received any response.

Is there a "managed" newsgroup for MAPI where I can post it to get the
answer faster.

thanks,
hari
 
P

Peter Huang [MSFT]

Hi

Thanks for your quickly reply!
I think you may also try the queue below which is managed and related with
MAPI.
microsoft.public.win32.programmer.messaging


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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