Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-22-2018, 04:45 PM
naeemakhtar naeemakhtar is offline Populating email subject from a MS-Word document Windows 10 Populating email subject from a MS-Word document Office 2016
Novice
Populating email subject from a MS-Word document
 
Join Date: Apr 2016
Posts: 8
naeemakhtar is on a distinguished road
Arrow Populating email subject from a MS-Word document

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
Reply With Quote
  #2  
Old 03-23-2018, 04:28 AM
gmayor's Avatar
gmayor gmayor is offline Populating email subject from a MS-Word document Windows 10 Populating email subject from a MS-Word document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply

Tags
email, letter, ms-word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating email subject from a MS-Word document Adding document fields to subject line of email 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
Populating email subject from a MS-Word document run word doc when a email is received with a certain subject based on rules megatronixs Outlook 3 10-18-2014 11:47 AM
Populating email subject from a MS-Word document Program to read Email subject line word by word john23# Outlook 1 02-14-2014 10:21 PM
Lack of email addresses auto-populating LarryK Outlook 0 10-10-2012 08:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft