Been lurking for a long while then joined and lurked some more. I'm creating and altering exisitng functions at work and it has been kinda fun (finding the code regarding the use of Concatenate on a array from this forum was fantastic) so I've started learning VBA from the beginning.
But this issue is me leaping forward and creating a spliced abomination of code apparently.
The Goal: For the Macro to check the yes/no flag in Col C and if yes, proceed with sending an email with the file attachments that columns D and maybe E will point to. (Each contact is stable and will have different files that are also stable.) The send list changes from time to time and the yes/no flag feels like the simplest control.
I have scripts that send email fine and use the yes/no flag fine for another process but not together. The section in bold is either completly ignored and sends email regardless or when I tinker with it it hangs up with various errors dealing with the control variable already in use or unexpected line ending.
I understand the terms used in VBA but the structure knowledge is the real challenge. (a good portion of my learning was simply recording a macro and finding the unneeded code/comparing it to similar macros)
And advice would be appreciated.
(I really need to invest in excel genie, I believe the ABCDE makes sense since the data order is simple and I can't get it to space correctly for this post.)
A B C D E
<TABLE style="WIDTH: 427pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=568><COLGROUP><COL style="WIDTH: 71pt; mso-width-source: userset; mso-width-alt: 3474" width=95><COL style="WIDTH: 128pt; mso-width-source: userset; mso-width-alt: 6217" width=170><COL style="WIDTH: 40pt; mso-width-source: userset; mso-width-alt: 1938" width=53><COL style="WIDTH: 94pt; mso-width-source: userset; mso-width-alt: 4571" width=125><COL style="WIDTH: 94pt; mso-width-source: userset; mso-width-alt: 4571" width=125><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 71pt; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 height=20 width=95>Contact Name</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 128pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=170>Email</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 40pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=53>Yes/No</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 94pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=125>File Location 1</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 94pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=125>File Location 2</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" height=20>John Doe</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl73>Jane.Doe@Unknown.com</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl74>Yes</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" height=20>Jane Doe</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl73>John.Doe@Unknown.com</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl74>No</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD></TR></TBODY></TABLE>
But this issue is me leaping forward and creating a spliced abomination of code apparently.
The Goal: For the Macro to check the yes/no flag in Col C and if yes, proceed with sending an email with the file attachments that columns D and maybe E will point to. (Each contact is stable and will have different files that are also stable.) The send list changes from time to time and the yes/no flag feels like the simplest control.
I have scripts that send email fine and use the yes/no flag fine for another process but not together. The section in bold is either completly ignored and sends email regardless or when I tinker with it it hangs up with various errors dealing with the control variable already in use or unexpected line ending.
I understand the terms used in VBA but the structure knowledge is the real challenge. (a good portion of my learning was simply recording a macro and finding the unneeded code/comparing it to similar macros)
And advice would be appreciated.
(I really need to invest in excel genie, I believe the ABCDE makes sense since the data order is simple and I can't get it to space correctly for this post.)
A B C D E
<TABLE style="WIDTH: 427pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=568><COLGROUP><COL style="WIDTH: 71pt; mso-width-source: userset; mso-width-alt: 3474" width=95><COL style="WIDTH: 128pt; mso-width-source: userset; mso-width-alt: 6217" width=170><COL style="WIDTH: 40pt; mso-width-source: userset; mso-width-alt: 1938" width=53><COL style="WIDTH: 94pt; mso-width-source: userset; mso-width-alt: 4571" width=125><COL style="WIDTH: 94pt; mso-width-source: userset; mso-width-alt: 4571" width=125><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 71pt; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 height=20 width=95>Contact Name</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 128pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=170>Email</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 40pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=53>Yes/No</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 94pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=125>File Location 1</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 94pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl75 width=125>File Location 2</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" height=20>John Doe</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl73>Jane.Doe@Unknown.com</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl74>Yes</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" height=20>Jane Doe</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl73>John.Doe@Unknown.com</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl74>No</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl77>System path to file</TD></TR></TBODY></TABLE>
Code:
Sub Send_Files()
'Working in 2000-2010
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Set sh = Sheets("Mailinfo")
<o:p></o:p>
Set OutApp = CreateObject("Outlook.Application")
<o:p></o:p>
[B]'Checks y/n before all other processes<o:p></o:p>[/B]
[B]On Error GoTo cleanup<o:p></o:p>[/B]
[B]For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)<o:p></o:p>[/B]
[B]If cell.Value Like "?*@?*.?*" And _<o:p></o:p>[/B]
[B]LCase(Cells(cell.Row, "C").Value) = "yes" Then<o:p></o:p>[/B]
[B]End If<o:p></o:p>[/B]
[B]Next cell<o:p></o:p>[/B]
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
<o:p></o:p>
'Enter the file names in the D:E column in each row
Set rng = sh.Cells(cell.Row, 1).Range("D1:E1")
<o:p></o:p>
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)
<o:p></o:p>
With OutMail
.To = cell.Value
.Subject = "Testfile"
.Body = "Hi " & cell.Offset(0, -1).Value
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Send 'Or use Display
End With
<o:p></o:p>
Set OutMail = Nothing
End If
Next cell
<o:p></o:p>
Set OutApp = Nothing
<o:p></o:p>
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
<o:p></o:p>
End Sub