Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new Outlook Email - adding attachment 1

Status
Not open for further replies.

Phantek

Programmer
Apr 25, 2011
29
US
I am trying to create a new Outlook Email from code in Access. In the email, I wish to be adding one or more attachments, based on the situation.

By searching, I have found some great code examples showing how to add an attachment, but I wish to be able to use Outlook's ability to specifically place an attachment in a certain location within the email. The emails are being sent out exclusively to individuals with the same email client, so I know the formatting would be the same for all recipients.

Is there any way to add an attachment at a specific place in the email body, or is it only possible to add to the general attachments when using VBA?

Thanks in advance!
 
One thing I do know is that all emails formats don't allow attachment embedding in the body. I think it's RTF that does allow it.
 
Okay... does that mean that I should be able to draft the email as an rtf file and have that placed as the body of the message?
 
I don't know if it means that or not. Never done it. It does mean that you have a lead though!

Let us know how and if it works for you!
 
Gruuuu is correct: the mail item must be in RTF format to show attachments in the body of the text.

Positioning the attachments is touchy, even if you are manipulating them with a mouse... I have yet to find a satisfactory way to do it with code. There is a 'Position' property for the Attachment, but it is an integer value and does not seem to correspond to a specific location in the message. I think you have to understand PropertyAssessors to know how to manipulate the position properly, and I have not dabbled in Outlook quite enough to go there.

Good luck.
 
I haven't dealt with this much either, but I do know that you can manipulate the body through the WordEditor.Range functionality.

I have some code from excel adding to the body using this:

.GetInspector.WordEditor.Range(Start:=0, End:=0).Paste


I'm not sure if this corresponds to the integer Gammachaser reffers to.




OCD, it’s not obsessive if you can control it…
 
The code below works for out look. I use it to send the contents of a word document that has data from access ( populate the word document then send it)

Private Sub Document_Close()

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Get Outlook if it's running
'Set oOutlookApp = GetObject(, "Outlook.Application")
'If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
'End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for the new email
.To = "me@mail.com"
'Set the recipient for a copy
'.CC = "recipient2@mail.com"
'Set the subject
.Subject = "New title"
'The content of the document is used as the body for the email
.Body = ActiveDocument.Content
.Send
End With

If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing


End Sub

Never give up never give in.

There are no short cuts to anything worth doing :)
 
Thank you to everyone who contributed - it helped me track down the solution. In case anyone else has this problem, I will document the solution.

Gammachaser was right that the email had to be in rtf format in order to place an attachment directly in the text. The problem with this is that I could no longer programmatically format the font, colours, etc. in the email document. Instead, it was necessary to have a template in an .rtf document, use Word as the editor, and then load that template into an Outlook email (specifying type as RTF). Once the email is created, you may then use the attachment positioning to state an integer value - corresponding to the number of characters in the message - to indicate where to place the attachment.

Sadly, this may be difficult to use if programmatically creating the email content, rather than using a pre-set email template.

Code:
Private Sub Command0_Click()

Dim itm As Object
Dim ID As String
Dim wd As Word.Application
Dim Doc As Word.Document
Dim objApp As Outlook.Application
Dim l_Msg As MailItem
Dim x As Integer, y As Integer, i As Integer


Set wd = CreateObject("Word.Application")

Set Doc = wd.Documents.Open(FileName:="C:\Location\file.doc", ReadOnly:=True)

    Set itm = Doc.MailEnvelope.Item
    With itm
        .To = DEFAULT_EMAIL
        .Subject = "THIS IS THE EMAIL SUBJECT"
        .Save
        ID = .EntryID
    End With

'clear references
Doc.Close wdDoNotSaveChanges
wd.Quit False

Set itm = Nothing
Set Doc = Nothing
Set wd = Nothing

' start email and get saved item
    Set objApp = CreateObject("Outlook.Application")

    Set l_Msg = objApp.GetNamespace("MAPI").GetItemFromID(ID)
    
    Set fs = Application.FileSearch
    fs.NewSearch
    
    fs.SearchSubFolders = False
    fs.FileName = "*.doc"
    fs.LookIn = "C:\Files"
    
    y = 10 ' IMPORTANT = this is the starting point for where the files are to be placed in your email
    
    l_Msg.BodyFormat = olFormatRichText ' This forces the email into RTF format, instead of HTML or TXT
    
    l_Msg.Recipients.Add ("Recipient1@address.com")
    l_Msg.Recipients.Add ("Recipient2@address.com")
        
    If fs.Execute > 0 Then
    	For x = 1 To fs.FoundFiles.Count
        
            l_Msg.Attachments.Add fs.FoundFiles(x), olByValue, y ' Add the attachments and places them
            
        
            y = y + 1

        Next x

    End If

    l_Msg.Display

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top