Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2015, 11:14 AM
mikey386 mikey386 is offline Help with macro not working - Pull from fields > attaches doc from folder > sends email Windows 8 Help with macro not working - Pull from fields > attaches doc from folder > sends email Office 2010 64bit
Novice
Help with macro not working - Pull from fields > attaches doc from folder > sends email
 
Join Date: Dec 2014
Posts: 10
mikey386 is on a distinguished road
Default Help with macro not working - Pull from fields > attaches doc from folder > sends email

Yikes - I hardly know where to begin -

I need help getting this macro to work -

In this worksheet I need to be able to input values in E1 - E6

(Subject) E1: Value from Vendor Name Column

(Body) E2: Will be copy pasted text



(Start Row) E3: Row number of first vendor entry to start with

(Last Row) E4: Row Number of last vendor entry to start with

(CC
Email
Addresses) E5: Single string of all the email addresses - separate addresses with a semi-colon

(From
Address) E5: Sender's Address

We need to be able to enter an appropriate value in cells E1: E6 - and upon selecting the "Create Draft Email" macro button currently occupies D78 - show an outlook message appropriately prepared for sending.

Also (not sure how this would be accomplished) - there is a folder path where there will be documents stored by Vendor Name:

C:\Users\SERGIL\Desktop\VATS

I need to somehow incorporate the ability to have the macro search by name (to be entered in cell E7?) for the .pdf / .doc / .xlsx entered in the cell - and have it automatically added as an attachment to the prepared Outlook email.

2 questions:

(#1) Is this possible?

(#2) How would I go about sharing the worksheet (containing the macro as developed thus far) securely to be helped?

Any & all help is GREATLY appreciated

I'm an idiot - but I try my best to be descriptive, at least.
Reply With Quote
  #2  
Old 01-07-2015, 01:51 PM
mikey386 mikey386 is offline Help with macro not working - Pull from fields > attaches doc from folder > sends email Windows 8 Help with macro not working - Pull from fields > attaches doc from folder > sends email Office 2010 64bit
Novice
Help with macro not working - Pull from fields > attaches doc from folder > sends email
 
Join Date: Dec 2014
Posts: 10
mikey386 is on a distinguished road
Default

I've added a link to the worksheet & I've removed any sensitive info. Also, I've included the macro code to my original post. Hth anyone who may be viewing this.

Link to Worksheet: https://app.box.com/s/wrpxoigkpy9x1502h6t0

Code:
Public Sub SendEmails()
    Const cSUBJECT As String = "E1"
    Const cBODY As String = "E2"
    Const cSTART_ROW_INDEX As String = "E3"
    Const cEND_ROW_INDEX As String = "E4"
    
    Const cMAIL_TO_COLUMN As String = "G" ' The column with the email addresses in it
    Const cCOMPANY_NAME_COLUMN As String = "D" ' The column with the Vendor/Company Names in it
    
    
    'Put as many email addresses here as you want, just seperate them with a semicolon
    Const cCC_EMAIL_ADDRESSES As String = "E5"
    
    Const cFROM_ADDRESS As String = "E6"
    
    Dim iRowCount As Integer
    Dim iEndRow As Integer
    
    'Grab the current open worksheet object
    Dim oSheet As Worksheet
    Set oSheet = ActiveSheet
    
    iRowCount = oSheet.Range(cSTART_ROW_INDEX).Value2 ' Get the Start Value
    iEndRow = oSheet.Range(cEND_ROW_INDEX).Value2 ' Get the End Value
    
    Dim dBatchStart As Date
    Dim dBatchEnd As Date
    Dim sVendorName As String
    Dim sEmail As String
    Dim sSubject As String
    Dim sBody As String
    
    'Outlook must already be open, attach to the open instance
    Dim oOutlook As Outlook.Application
    Set oOutlook = GetObject(, "Outlook.Application")
    
    'Declare a new draft email object
    Dim oMail As Object
    Set oMail = oOutlook.CreateItem("olMailItem")
            
    'Start iterating through all the rows of mail, creating a new draft each loop
    Do Until iRowCount = (iEndRow + 1)
    
        'Actually instantiate the new draft email object
        Set oMail = oOutlook.CreateItem(olMailItem)
        
        'Display the draft on screen to the user can see and validate it
        oMail.Display
                      
        'Get the subject, also, substitute the tags for Company and Start Date with the values in the sheet
        sSubject = oSheet.Range(cSUBJECT).Value2
        sSubject = Replace(sSubject, "", oSheet.Range(cCOMPANY_NAME_COLUMN & iRowCount).Value2)
        
        'Now insert the formatted subject into the draft email
        oMail.Subject = sSubject
        
        'Get the Body, substitute the tags for Start Date and End Date with the values in the sheet
        sBody = oSheet.Range(cBODY).Value2
          
        'Now insert the formatted Body into the draft email
        oMail.HTMLBody = sBody
        
        'Set the CC address based on the Constant at the top
        oMail.CC = oSheet.Range(cCC_EMAIL_ADDRESSES).Value2
        
        oMail.Save
        'Set the actual sender of the name. It won't display for the user, but will actually sent as that address
        oMail.SentOnBehalfOfName = oSheet.Range(cFROM_ADDRESS).Value2
        oMail.Save
Reply With Quote
  #3  
Old 01-08-2015, 12:20 AM
gmayor's Avatar
gmayor gmayor is offline Help with macro not working - Pull from fields > attaches doc from folder > sends email Windows 7 64bit Help with macro not working - Pull from fields > attaches doc from folder > sends email Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

Given that you only have one line of data, it is difficult to see the whole picture. Is the aim to produce one e-mail message with all the recipient data collated, or several e-mail messages, each with its own data? I suspect the former, though your macro suggests otherwise.

If I suspect correctly then I must further assume that you want to add a raft of recipients (as CC?) and an equal number of attachments?

How EXACTLY do the attachment names relate to the fields in the section from row 9 on?

While you can do that (though I would have to look at the limits) might it not be better to send separate messages - in effect mail merge?
__________________
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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to pull data out of a Word table shanemarkley Word VBA 20 05-07-2015 01:36 PM
auto populate fields for multiple files w/in folder jbyrd Word 0 07-21-2014 07:35 AM
Tab Between Fields Not Always Working cbalogh Word 4 01-25-2014 08:08 PM
Mail merge only sends some email from Access database rsakai2 Mail Merge 4 10-30-2013 10:32 PM
Help with macro not working - Pull from fields > attaches doc from folder > sends email macro to pull data from multiple files psrs0810 Excel 2 10-25-2010 01:49 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:15 PM.


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