VBA Macro - Word

MartinEbner

New Member
Joined
Jan 7, 2016
Messages
28
Hi,

Issue
I have modified a macro (please see below) - it encompasses all of the functionality that I require with one exception. I require it to reference an email address that is written in the word document.

Macro
Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document

Application.ScreenUpdating = True
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "Subject"
.Body = "Dear Whoever,
.To = "User.Domain.Com"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.Fullname
.Display
End With

Application.ScreenUpdating = True

Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing

End Sub

-----------

Instead of it having a prescribed email in the macro, I need it to be flexible in the sense that from time to time the email address will change and as such it is impractical go into the macro every time - It needs to source it from the word document itself.

Any help would be greatly appreciated.

Regards,
Martin
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Do these email addresses appear in the document as clickable hyperlinks, or just as plain text? Is the email address in the header or footer; otherwise is it the first email address in the document?
 
Upvote 0
Hi Paul,

It is text not hyperlink. It will be the only email address within the document. The email address will not reside in the header or footer but in the body of the document.

Kind Regards,
 
Upvote 0
Hi,

The code is what I use in word and it serves it's purpose well. However, in order for it to be fully effective I need the macro to reference an email address in the document rather than having to physically put the email recipient into the document.

Regards,
 
Upvote 0
The code is what I use in word and it serves it's purpose well. However, in order for it to be fully effective I need the macro to reference an email address in the document rather than having to physically put the email recipient into the document.
So why in the other thread are you asking about Excel cells?
 
Upvote 0
Because the word document is a mail merge pulling data from the spreadsheet so my word macro can either reference from word or pull from Excel. . Do you understand.. Sorry that I've been unclear :(
 
Upvote 0
In that case, there is no need to automate Excel or Outlook, or refer to anything in the Word document. All you need do is retrieve the email address via the mailmerge code. For example:
Code:
Sub Merge_To_Emails()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToEmail
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
      End With
      .MailAddressFieldName = .DataSource.DataFields("Email_Address")
      .MailSubject = .DataSource.DataFields("Subject_Line")
      .MailAsAttachment = True
      .MailFormat = wdMailFormatHTML
      .Execute Pause:=False
    End With
  Next i
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,054
Members
453,523
Latest member
Don Quixote

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