![]() |
#1
|
|||
|
|||
![]()
I have created a simple macro that will save a MS-Word document and email it to a designated user. I would like to create VBA that would capture the reference line in the MS-Word document and populate the subject of the email with this reference.
The MS-Word document is a letter. The first line is the date. The next three lines are the address. The fourth line is the reference. March 22 2018 Mr. John Doe 123 Main Street Anytown AN 11111 re: Microsoft matter (capture this information in subject heading email) Please provide VBA code that will capture the contents in the reference section of the letter and populate the subject of the email with this information. Below is the code for the save and send. Sub sendeMail() Dim olkApp As Object Dim strSubject As String Dim strTo As String Dim strBody As String Dim strAtt As String strSubject = "Whatever!" strBody = "Please see attached File" strTo = "fred@fred.com" If ActiveDocument.FullName = "" Then MsgBox "activedocument not saved, exiting" Exit Sub Else If MsgBox("Activedocument NOT saved, Proceed?", vbYesNo, "Error") <> vbYes Then Exit Sub End If strAtt = ActiveDocument.FullName Set olkApp = CreateObject("outlook.application") With olkApp.createitem(0) .to = strTo .Subject = strSubject .body = strBody .attachments.Add strAtt '.send .Display End With Set olkApp = Nothing End Sub |
#2
|
||||
|
||||
![]()
You need to define strsubject e.g.
Code:
Dim strSubject As String 'this line already in macro Dim oPara As Range Dim lngPara As Long For lngPara = 1 To ActiveDocument.Paragraphs.Count Set oPara = ActiveDocument.Paragraphs(lngPara).Range If Left(LCase(oPara.Text), 3) = "re:" Then oPara.End = oPara.End - 1 oPara.MoveStartUntil Chr(32) oPara.Start = oPara.Start + 1 strSubject = oPara.Text Exit For End If Next lngPara
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
![]() |
Tags |
email, letter, ms-word |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
russkris | Word VBA | 3 | 09-24-2017 08:18 PM |
Populating a Word document with VBA\SQL | shabbaranks | Mail Merge | 23 | 07-21-2015 01:31 PM |
![]() |
megatronixs | Outlook | 3 | 10-18-2014 11:47 AM |
![]() |
john23# | Outlook | 1 | 02-14-2014 10:21 PM |
Lack of email addresses auto-populating | LarryK | Outlook | 0 | 10-10-2012 08:09 AM |