Automate attachment selection

P

Padds

I would like to create a form, essentially a newsletter, that will
automatically attach a file from a particular location each time the form is
opened. The contents of the file change, however, the name and path remain
constant.

e.g.
I would like G:\folder1\file1.zip to be attached to a form each time the
form is opened (rather than have the attachment saved along with the form
which would mean that the new data in this file would not be attached after
the form was created).

Is this possible? If so, how do I do it?
 
K

karlman

I would like to create a form, essentially a newsletter, that will
automatically attach a file from a particular location each time the form is
opened.  The contents of the file change, however, the name and path remain
constant.  

e.g.
I would like G:\folder1\file1.zip to be attached to a form each time the
form is opened (rather than have the attachment saved along with the form
which would mean that the new data in this file would not be attached after
the form was created).  

Is this possible?  If so, how do I do it?  

How about just create a link to the file in the form. The actual file
will not be saved in the form, just the link information on wher it is
located. You could change the file all you wanted as long as it's name
remained the same.

Karl
 
P

Padds

This won't work in my situation I'm afraid.

The attachment will be sent to external recipients (I should have specified
that in my original question).

Thank's for your reply though - do you have any more suggestions?
 
S

Sue Mosher [MVP-Outlook]

An Outlook custom form is not a good solution for this scenario, given that custom forms can cause attachment problems for external recipients.

Instead, you could write a little VBA macro to attach the file.
 
P

Padds

VBA Macros don't fall into my area of expertise - could you give me some
pointers on where I should start?
 
S

Sue Mosher [MVP-Outlook]

Because Word is your email editor, you would need to create this macro in Word. Insert a new module in the Normal.dot template and put the macro there.

Sub InsertFile()
' requires reference to Microsoft Outlook library
Dim objOL As Outlook.Application
Dim objInsp As Outlook.Inspector
Dim objMail As Outlook.MailItem
On Error Resume Next
Set objOL = GetObject(, "Outlook.Application")
Set objInsp = objOL.ActiveInspector
Set objMail = objInsp.CurrentItem
objMail.Attachments.Add "C:\data\myfile.txt"
Set objMail = Nothing
Set objInsp = Nothing
Set objOL = Nothing
End Sub

You can then run it from an open Outlook message by using Alt+F8 or add a custom toolbar that includes that macro as a button.
 
M

Marc

So sorry about this bump but I have been struggling with a similar problem as well. I am trying to attach one Excel file with all my outgoing e-mails. It will be the same Excel file and I am looking to have the attachment already attached to the e-mail when I click on "New" to send a new e-mail.

Is that possible with the VB code suemvp provided above? Much thanks for your help. :blush:
 
S

Sue Mosher [MVP-Outlook]

What code are you referring to? "above" doesn't have a concrete meaning in a discussion forum that can be accessed any number of different ways.

Also, does the Excel file itself change?
 
M

Marc

Hello,

This is the code I was referring to:

Quote:
Sub InsertFile()
' requires reference to Microsoft Outlook library
Dim objOL As Outlook.Application
Dim objInsp As Outlook.Inspector
Dim objMail As Outlook.MailItem
On Error Resume Next
Set objOL = GetObject(, "Outlook.Application")
Set objInsp = objOL.ActiveInspector
Set objMail = objInsp.CurrentItem
objMail.Attachments.Add "C:\data\myfile.txt"
Set objMail = Nothing
Set objInsp = Nothing
Set objOL = Nothing
End Sub


And the Excel file does not change. And it's location (in C drive) does not change either.
 
S

Sue Mosher [MVP-Outlook]

You'd want to use the events related to the Inspector window. Put the following code in the built-in ThisOutlookSession module, then run the Application_Startup procedure.


Dim WithEvents m_colInsp As Outlook.Inspectors
Dim WithEvents m_objInsp As Outlook.Inspector

Private Sub Application_Startup()
Set m_colInsp = Application.Inspectors
End Sub

Private Sub m_colInsp_NewInspector(ByVal Inspector As Inspector)
Set m_objInsp = Inspector
End Sub

Private Sub m_objInsp_Activate()
Dim objMail As Outlook.mailItem
If m_objInsp.CurrentItem.Class = olMail Then
Set objMail = m_objInsp.CurrentItem
If objMail.Size = 0 Then
' it's a new message
objMail.Attachments.Add "C:\Data\myfile.xls"
End If
End If
Set objMail = Nothing
Set m_objInsp = Nothing
End Sub
 
M

Marc

Hello suemvp,

Thank you so much for your help. The code works perfectly. I greatly appreciate it for taking your time to help me.

I also managed to learn a few things about using the built-in VB Editor.

Best Regards,

Starbuzz
 

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