Open Word. modify, send email and close without saving from Excel

Brian R Armstrong

Board Regular
Joined
Jun 5, 2007
Messages
92
I would like some help please with the following - I have try to merge without success

I would like to open a existing Word document, modify - add some text (Name, Date, Reference number) from an linked to an open Excel Worksheet, send as an email and close the Word document without saving - ready to repeat for other Names ……

I have two separate vba codes

One to open document - add Name, Date, Reference number from Excel Workbook.

The second to send the Word document as the body of an email to Named person again from the Excel Workbook.

Can I combine the codes such that I can close without saving the Word document after the email is sent?

Code:
Sub QpenDoc() 'adds name to Word.doc
Dim objWord
Dim objDoc

Set objWord = CreateObject("Word.Application")

objWord.Visible = True
 
Set objDoc = objWord.Documents.Open("C:\Users\Main User\Documents\Custom Office Templates\Reward Voucher v2.dotm") 'objWord.Documents.Add

Dim i As Integer
Dim strValue As String

objDoc.Activate 'bring focus to the document created

strValue = "Dear " & Cells(i + 1, 1) 

objWord.Selection.TypeText Text:=strValue 'write the value to the document

objDoc.Close savechanges:=False
Set objWord = Nothing
'Set itm = Nothing
'Set wd = Nothing
Code:
Sub SendDocAsMsg()
Dim wd As Word.Application
Dim doc As Word.Document
Dim itm As Outlook.MailItem
Dim ID As String
Dim blnWeOpenedWord As Boolean
On Error Resume Next

Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
blnWeOpenedWord = True
End If
Set doc = wd.Documents.Open _
(Filename:="C:\Users\Main User\Documents\Custom Office Templates\Reward Voucher v2.dotm", ReadOnly:=True)
Set itm = doc.MailEnvelope.Item
With itm
.To = "bra80@tiscali.co.uk" 'bra80@tiscali.co.uk, rozquirk@hotmail.co.uk
.Subject = "Roll Call"
.Send
End With
doc.Close wdDoNotSaveChanges
If blnWeOpenedWord Then
wd.Quit
End If
Set doc = Nothing
Set itm = Nothing
Set wd = Nothing
End Sub

Appreciate any help
Kind Regards Brian A
Windows 10, Office 2016
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is there a reason for not using mailmerge (which you could automate) to do this?
 
Upvote 0

Forum statistics

Threads
1,215,999
Messages
6,128,192
Members
449,431
Latest member
Taekwon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top