Combining two VBA codes (adding outlook signature)

steamroller123

New Member
Joined
Jul 23, 2015
Messages
3
Dear colleagues ;),

What I am trying to do is to combine 2 vba codes.

I have a vba that automatically prepares personal emails with attachments (newsletter). The thing is that I really need it with default outlook signature. So , another vba that I have is vba that opens new outlook message with signature.

You can see the files here.

This is the main file that I would like with outlook signature.

Sub Send_Files()

Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
Dim strbody As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set sh = Sheets("Sheet1")

Set OutApp = CreateObject("Outlook.Application")

For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)


Set rng = sh.Cells(cell.Row, 1).Range("F1:Z1")

If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)

With OutMail
.to = cell.Value
.Subject = "Newsletter"
.Body = "Dear " & cell.Offset(0, -1).Value & vbNewLine & " " & vbNewLine & " " & vbNewLine & " " & cell.Offset(0, 1).Value & " " & cell.Offset(0, 2).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

.display 'Or use .Display
End With

Set OutMail = Nothing
End If
Next cell

Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub


--------------------------------------------
For example I have this VBA that creates msg with new signature:

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub send()

Dim ret As Long
On Error GoTo aa
ret = ShellExecute(Application.hwnd, vbNullString, "Outlook", vbNullString, "C:\", SW_SHOWNORMAL)
If ret < 3 Then

MsgBox "Outlook is not found.", vbCritical, "SN's Customised Solutions"
End If
aa:

Dim oOutlook As Object

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOutlook Is Nothing Then
Else
'Call NameOfYourMailMacro
End If


ActiveWorkbook.save
Dim OutApp As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = " " & _
"" & _
""

On Error Resume Next

'
With OutMail
.Display
.To = "test@test.com"
.CC = ""
.BCC = ""
.Subject = ""
.HTMLBody = strbody & "<br>" & .HTMLBody


.Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing



End Sub
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try

Code:
[COLOR=#333333]Sub Send_Files()[/COLOR]

[COLOR=#333333]Dim OutApp As Object[/COLOR]
[COLOR=#333333]Dim OutMail As Object[/COLOR]
[COLOR=#333333]Dim sh As Worksheet[/COLOR]
[COLOR=#333333]Dim cell As Range[/COLOR]
[COLOR=#333333]Dim FileCell As Range[/COLOR]
[COLOR=#333333]Dim rng As Range[/COLOR]
[COLOR=#333333]Dim strbody As String[/COLOR]
[COLOR=#333333]Dim signature as string
Dim emailBody as string
With Application[/COLOR]
[COLOR=#333333].EnableEvents = False[/COLOR]
[COLOR=#333333].ScreenUpdating = False[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set sh = Sheets("Sheet1")[/COLOR]

[COLOR=#333333]Set OutApp = CreateObject("Outlook.Application")[/COLOR]

[COLOR=#333333]For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)[/COLOR]


[COLOR=#333333]Set rng = sh.Cells(cell.Row, 1).Range("F1:Z1")[/COLOR]

[COLOR=#333333]If cell.Value Like "?*@?*.?*" And _[/COLOR]
[COLOR=#333333]Application.WorksheetFunction.CountA(rng) > 0 Then[/COLOR]
[COLOR=#333333]Set OutMail = OutApp.CreateItem(0)[/COLOR]

[COLOR=#333333]With OutMail.display
[/COLOR][COLOR=#333333]signature = OutMail.HTMLbody[/COLOR][COLOR=#333333]

With OutMail
    .Body = [/COLOR][COLOR=#333333]"Dear " & cell.Offset(0, -1).Value & vbNewLine & " " & vbNewLine & " " & vbNewLine & " " & cell.Offset(0, 1).Value & " " & cell.Offset(0, 2).Value
[/COLOR][COLOR=#333333]    .Display
End With
emailBody = OutMail.HTMLbody

With OutMail[/COLOR]
[COLOR=#333333].to = cell.Value[/COLOR]
[COLOR=#333333].Subject = "Newsletter"[/COLOR]
[COLOR=#333333].HTMLbody = emailbody & signature[/COLOR]
[COLOR=#333333]For Each FileCell In rng.SpecialCells(xlCellTypeConstants)[/COLOR]
[COLOR=#333333]If Trim(FileCell) <> "" Then[/COLOR]
[COLOR=#333333]If Dir(FileCell.Value) <> "" Then[/COLOR]
[COLOR=#333333].Attachments.Add FileCell.Value[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next FileCell[/COLOR]

[COLOR=#333333].display 'Or use .Display[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set OutMail = Nothing[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next cell[/COLOR]

[COLOR=#333333]Set OutApp = Nothing[/COLOR]
[COLOR=#333333]With Application[/COLOR]
[COLOR=#333333].EnableEvents = True[/COLOR]
[COLOR=#333333].ScreenUpdating = True[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Thank you, unfortunately doesn't work: End if without block if. I have attached a file here: Excel files

Try

Code:
[COLOR=#333333]Sub Send_Files()[/COLOR]

[COLOR=#333333]Dim OutApp As Object[/COLOR]
[COLOR=#333333]Dim OutMail As Object[/COLOR]
[COLOR=#333333]Dim sh As Worksheet[/COLOR]
[COLOR=#333333]Dim cell As Range[/COLOR]
[COLOR=#333333]Dim FileCell As Range[/COLOR]
[COLOR=#333333]Dim rng As Range[/COLOR]
[COLOR=#333333]Dim strbody As String[/COLOR]
[COLOR=#333333]Dim signature as string
Dim emailBody as string
With Application[/COLOR]
[COLOR=#333333].EnableEvents = False[/COLOR]
[COLOR=#333333].ScreenUpdating = False[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set sh = Sheets("Sheet1")[/COLOR]

[COLOR=#333333]Set OutApp = CreateObject("Outlook.Application")[/COLOR]

[COLOR=#333333]For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)[/COLOR]


[COLOR=#333333]Set rng = sh.Cells(cell.Row, 1).Range("F1:Z1")[/COLOR]

[COLOR=#333333]If cell.Value Like "?*@?*.?*" And _[/COLOR]
[COLOR=#333333]Application.WorksheetFunction.CountA(rng) > 0 Then[/COLOR]
[COLOR=#333333]Set OutMail = OutApp.CreateItem(0)[/COLOR]

[COLOR=#333333]With OutMail.display
[/COLOR][COLOR=#333333]signature = OutMail.HTMLbody[/COLOR][COLOR=#333333]

With OutMail
    .Body = [/COLOR][COLOR=#333333]"Dear " & cell.Offset(0, -1).Value & vbNewLine & " " & vbNewLine & " " & vbNewLine & " " & cell.Offset(0, 1).Value & " " & cell.Offset(0, 2).Value
[/COLOR][COLOR=#333333]    .Display
End With
emailBody = OutMail.HTMLbody

With OutMail[/COLOR]
[COLOR=#333333].to = cell.Value[/COLOR]
[COLOR=#333333].Subject = "Newsletter"[/COLOR]
[COLOR=#333333].HTMLbody = emailbody & signature[/COLOR]
[COLOR=#333333]For Each FileCell In rng.SpecialCells(xlCellTypeConstants)[/COLOR]
[COLOR=#333333]If Trim(FileCell) <> "" Then[/COLOR]
[COLOR=#333333]If Dir(FileCell.Value) <> "" Then[/COLOR]
[COLOR=#333333].Attachments.Add FileCell.Value[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next FileCell[/COLOR]

[COLOR=#333333].display 'Or use .Display[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set OutMail = Nothing[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next cell[/COLOR]

[COLOR=#333333]Set OutApp = Nothing[/COLOR]
[COLOR=#333333]With Application[/COLOR]
[COLOR=#333333].EnableEvents = True[/COLOR]
[COLOR=#333333].ScreenUpdating = True[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
I forgot to take out an extra with

Code:
[COLOR=#333333]Sub Send_Files()[/COLOR]

[COLOR=#333333]Dim OutApp As Object[/COLOR]
[COLOR=#333333]Dim OutMail As Object[/COLOR]
[COLOR=#333333]Dim sh As Worksheet[/COLOR]
[COLOR=#333333]Dim cell As Range[/COLOR]
[COLOR=#333333]Dim FileCell As Range[/COLOR]
[COLOR=#333333]Dim rng As Range[/COLOR]
[COLOR=#333333]Dim strbody As String[/COLOR]
[COLOR=#333333]Dim signature as string
Dim emailBody as string
With Application[/COLOR]
[COLOR=#333333].EnableEvents = False[/COLOR]
[COLOR=#333333].ScreenUpdating = False[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set sh = Sheets("Sheet1")[/COLOR]

[COLOR=#333333]Set OutApp = CreateObject("Outlook.Application")[/COLOR]

[COLOR=#333333]For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)[/COLOR]


[COLOR=#333333]Set rng = sh.Cells(cell.Row, 1).Range("F1:Z1")[/COLOR]

[COLOR=#333333]If cell.Value Like "?*@?*.?*" And _[/COLOR]
[COLOR=#333333]Application.WorksheetFunction.CountA(rng) > 0 Then[/COLOR]
[COLOR=#333333]Set OutMail = OutApp.CreateItem(0)[/COLOR]

[COLOR=#333333]OutMail.display 'Extra [/COLOR][COLOR=#ff0000]With[/COLOR][COLOR=#333333] was here
[/COLOR][COLOR=#333333]signature = OutMail.HTMLbody[/COLOR][COLOR=#333333]

With OutMail
    .Body = [/COLOR][COLOR=#333333]"Dear " & cell.Offset(0, -1).Value & vbNewLine & " " & vbNewLine & " " & vbNewLine & " " & cell.Offset(0, 1).Value & " " & cell.Offset(0, 2).Value
[/COLOR][COLOR=#333333]    .Display
End With
emailBody = OutMail.HTMLbody

With OutMail[/COLOR]
[COLOR=#333333].to = cell.Value[/COLOR]
[COLOR=#333333].Subject = "Newsletter"[/COLOR]
[COLOR=#333333].HTMLbody = emailbody & signature[/COLOR]
[COLOR=#333333]For Each FileCell In rng.SpecialCells(xlCellTypeConstants)[/COLOR]
[COLOR=#333333]If Trim(FileCell) <> "" Then[/COLOR]
[COLOR=#333333]If Dir(FileCell.Value) <> "" Then[/COLOR]
[COLOR=#333333].Attachments.Add FileCell.Value[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next FileCell[/COLOR]

[COLOR=#333333].display 'Or use .Display[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set OutMail = Nothing[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next cell[/COLOR]

[COLOR=#333333]Set OutApp = Nothing[/COLOR]
[COLOR=#333333]With Application[/COLOR]
[COLOR=#333333].EnableEvents = True[/COLOR]
[COLOR=#333333].ScreenUpdating = True[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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