473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Mail merge to E-mail with attachments using VBA [Outlook 2003]

Hello everyone,

Thank you for reading! I am completely new to this so be gentle :)

I am trying to mail merge to e-mail, including attachments with each message. I found this great article by Doug Robbins: http://word.mvps.org/faqs/mailmerge/...ttachments.htm

This article solved my problem, but unfortunately I found out that running the macro strips off all formatting as well as removing the images I had in my original document (I started the mail merge from Word).

Is there any way to alter the code below to send the message as it is (i.e. with formatting and pictures)?

Thank you very much for your help,

Sarah

PS: I am using Windows XP with Microsoft Office 2003.

Expand|Select|Wrap|Line Numbers
  1. Sub emailmergewithattachments()
  2.  
  3. Dim Source As Document, Maillist As Document, TempDoc As Document
  4. Dim Datarange As Range
  5. Dim i As Long, j As Long
  6. Dim bStarted As Boolean
  7. Dim oOutlookApp As Outlook.Application
  8. Dim oItem As Outlook.MailItem
  9. Dim mysubject As String, message As String, title As String
  10.  
  11. Set Source = ActiveDocument
  12.  
  13. ' Check if Outlook is running.  If it is not, start Outlook
  14. On Error Resume Next
  15. Set oOutlookApp = GetObject(, "Outlook.Application")
  16. If Err <> 0 Then
  17.     Set oOutlookApp = CreateObject("Outlook.Application")
  18.     bStarted = True
  19. End If
  20.  
  21. ' Open the catalog mailmerge document
  22. With Dialogs(wdDialogFileOpen)
  23.     .Show
  24. End With
  25. Set Maillist = ActiveDocument
  26.  
  27. ' Show an input box asking the user for the subject to be inserted into the email messages
  28. message = "Enter the subject to be used for each email message."    ' Set prompt.
  29. title = " Email Subject Input"    ' Set title.
  30. ' Display message, title
  31. mysubject = InputBox(message, title)
  32.  
  33. ' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
  34. ' extracting the information to be included in each email.
  35. For j = 1 To Source.Sections.Count - 1
  36.     Set oItem = oOutlookApp.CreateItem(olMailItem)
  37.     With oItem
  38.         .Subject = mysubject
  39.         .Body = Source.Sections(j).Range.Text
  40.         Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
  41.         Datarange.End = Datarange.End - 1
  42.         .To = Datarange
  43.         For i = 2 To Maillist.Tables(1).Columns.Count
  44.             Set Datarange = Maillist.Tables(1).Cell(j, i).Range
  45.             Datarange.End = Datarange.End - 1
  46.             .Attachments.Add Trim(Datarange.Text), olByValue, 1
  47.         Next i
  48.         .Send
  49.     End With
  50.     Set oItem = Nothing
  51. Next j
  52. Maillist.Close wdDoNotSaveChanges
  53.  
  54. '  Close Outlook if it was started by this macro.
  55. If bStarted Then
  56.     oOutlookApp.Quit
  57. End If
  58.  
  59. MsgBox Source.Sections.Count - 1 & " messages have been sent."
  60.  
  61. 'Clean up
  62. Set oOutlookApp = Nothing
  63.  
  64. End Sub
  65.  
Jan 18 '12 #1
1 3927
NeoPa
32,556 Expert Mod 16PB
Sarah, correct me if I'm wrong but doesn't this code try to include the document text inside the email, rather than adding it as an attachment as you state is the requirement?
Jan 19 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Huntjens | last post by:
Ive tried so many things that it drives me crazy: a want to merge / join 2 xml files like this: merge.xml: <?xml version="1.0"?> <merge> <appxml>testapp.xml</appxml>...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
0
by: Private Pyle | last post by:
Version 8.1 fixpack 5, AIX 5.1 32-bit I have a situation where I want to use a merge statement to perform insert else update logic to move data from one table to another. I have a working merge...
3
by: Shyguy | last post by:
I have a MS Word Merge letter that is based on a query. I set up a macro to run the query and then open Word and the merge letter. Word open fine but it can't find the letter. I used RunAPP and...
1
by: law | last post by:
I love the super easy word merge but I have a question. In my database I select multiple records which I then want to merge to a single document in word. I use the single word merge and get one...
4
by: John J. Hughes II | last post by:
Could someone explain how to merge the form menu with the mdi container window. The menu strip items on the form window merge but I either end up with a blank blue menu on the form or top list of...
9
by: anunaygupta | last post by:
Hello all, I have a data structures problem. Assume there are 2 linked lists L1 and L2. They merge into some node. L1 1->2->3->4->5->6 / L2 8->7->9->/ L1 and L2 merge in node with value...
16
by: UDBDBA | last post by:
Hi All: I need some clarification on a MERGE statement. The database is on V8 FP12 (AIX) 64bit. The source table is tableA. The target is a View "FACT" with UNION ALL because of the 512 Gig...
1
by: sparks | last post by:
I just got a chance to dl the work and printed out the docs and noticed on one of the screen shots it says print and there is a button for merge all. So this is for single as well as a complete...
1
by: joproulx | last post by:
Hello all, Here is my problem: I am trying to merge 2 datasets but I don't want to overwrite rows that are already modified in my working dataset. Example: I have one Dataset with only one...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.