View Single Post
 
Old 03-10-2015, 02:16 AM
amentinho amentinho is offline Windows 8 Office 2013
Novice
 
Join Date: Mar 2015
Posts: 4
amentinho is on a distinguished road
Default Mail Merge with correct format

Hi all,

I'm new to Word Vba and I'm trying to do a Macro to send various mails with attachment by Merge Mail tool help. I copy some indications from various webs but I cannot find a solution to my problem

The macro is working, but it send the mail without format. I'd like to send the mail in the same format as it appear in word.

I'm using this code



Code:
Sub emailmergewithattachments()

Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
Set Source = ActiveDocument

On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
End If

With Dialogs(wdDialogFileOpen)
    .Show
End With
Set Maillist = ActiveDocument

message = "Enter the subject to be used for each email message."
title = " Email Subject Input"

mysubject = InputBox(message, title)

For j = 1 To Source.Sections.Count - 1
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
        .Subject = mysubject
        .Body = Source.Sections(j).Range.Text
        Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
        Datarange.End = Datarange.End - 1
        .To = Datarange
        .BCC= "amentinho@example.com"
        For i = 2 To Maillist.Tables(1).Columns.Count
            Set Datarange = Maillist.Tables(1).Cell(j, i).Range
            Datarange.End = Datarange.End - 1
            .Attachments.Add Trim(Datarange.Text), olByValue, 1
        Next i
        .Send
    End With
    Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges

If bStarted Then
    oOutlookApp.Quit
End If
MsgBox Source.Sections.Count - 1 & " messages have been sent."

Set oOutlookApp = Nothing
End Sub
How can I use this macro manteining the original word file format?

Any help would be really appreciate.

Thanks
Reply With Quote