Het bericht maar even terug gezet anders is het zo'n raar iets.
Goeiemiddag,
Ik ben bezig met het schrijven van een macro om automatisch selecties te mailen naar contactpersonen.
Nu was de macro zo geschreven dat hij een e-mailadres mailt dat met behulp van verticaal zoeken uit een ander tabblad wordt opgehaald. Nu wil ik ook de mogelijkheid hebben om mensen in de CC te zetten, alleen werkt de macro niet goed meer.
- Wanneer er een e-mailadres in de Kolom 'CC' is ingevuld wordt deze netjes opgehaald en kan het mailtje verzonden worden.
- Wanneer de cel in de Kolom 'CC' leeg is komt i.p.v een e-mailadres een 0 tevoorschijn in de verwijzing. Hierdoor wordt het mailtje niet verzonden, ook niet naar het hoofdcontact.
Zie bijlage voor mijn macro tot nu toe. Verder heb ik onderaan het bericht een format van mijn bestand geplaatst. Hoe krijg ik mijn macro nu wel werkend als er geen e-mailadres in de kolom van CC staat?
Ik hoor graag van jullie!
[SQL]
Sub MailLev()
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object
Dim strTo As String
Set Source = Nothing
On Error Resume Next
Set Source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If
If ActiveWindow.SelectedSheets.Count > 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count > 1 Then
MsgBox "Helaas Pindakaas:" & vbNewLine & vbNewLine & _
"Je bent geen regels geselecteerd," & vbNewLine & _
"selecteer de gewenste regels & de juiste leverancier.", vbOKOnly
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)
Source.Copy
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Overzicht openstaande orderregels " & Format(Now, "dd-mmm-yy h-mm-ss")
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = ThisWorkbook.Sheets("Orderregels").Range("A4").Value
.CC = ThisWorkbook.Sheets("Orderregels").Range("A6").Value
.BCC = ""
.Subject = "Openstaande order(s)"
.Body = "Beste," & vbNewLine & vbNewLine & "In de bijlage vindt u een Excelbestand met een overzicht van openstaande orderregels "
.Attachments.Add Dest.FullName
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
[/SQL]Bekijk bijlage Format overzicht helpmij.nl 2.0.xlsx
Goeiemiddag,
Ik ben bezig met het schrijven van een macro om automatisch selecties te mailen naar contactpersonen.
Nu was de macro zo geschreven dat hij een e-mailadres mailt dat met behulp van verticaal zoeken uit een ander tabblad wordt opgehaald. Nu wil ik ook de mogelijkheid hebben om mensen in de CC te zetten, alleen werkt de macro niet goed meer.
- Wanneer er een e-mailadres in de Kolom 'CC' is ingevuld wordt deze netjes opgehaald en kan het mailtje verzonden worden.
- Wanneer de cel in de Kolom 'CC' leeg is komt i.p.v een e-mailadres een 0 tevoorschijn in de verwijzing. Hierdoor wordt het mailtje niet verzonden, ook niet naar het hoofdcontact.
Zie bijlage voor mijn macro tot nu toe. Verder heb ik onderaan het bericht een format van mijn bestand geplaatst. Hoe krijg ik mijn macro nu wel werkend als er geen e-mailadres in de kolom van CC staat?
Ik hoor graag van jullie!
[SQL]
Sub MailLev()
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object
Dim strTo As String
Set Source = Nothing
On Error Resume Next
Set Source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If
If ActiveWindow.SelectedSheets.Count > 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count > 1 Then
MsgBox "Helaas Pindakaas:" & vbNewLine & vbNewLine & _
"Je bent geen regels geselecteerd," & vbNewLine & _
"selecteer de gewenste regels & de juiste leverancier.", vbOKOnly
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)
Source.Copy
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Overzicht openstaande orderregels " & Format(Now, "dd-mmm-yy h-mm-ss")
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = ThisWorkbook.Sheets("Orderregels").Range("A4").Value
.CC = ThisWorkbook.Sheets("Orderregels").Range("A6").Value
.BCC = ""
.Subject = "Openstaande order(s)"
.Body = "Beste," & vbNewLine & vbNewLine & "In de bijlage vindt u een Excelbestand met een overzicht van openstaande orderregels "
.Attachments.Add Dest.FullName
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
[/SQL]Bekijk bijlage Format overzicht helpmij.nl 2.0.xlsx
Laatst bewerkt: