Public ArtikelLinkAdres As String
Public BestandsNaam As String
Option Explicit
Sub create_and_email_pdf()
Rows("54:60").EntireRow.Hidden = True
Call DatumBestellingOpslaan
Call LeverancierBestellingOpslaan
Call DoelBestellingOpslaan
Call StatusBestellingOpslaan
Dim EmailSubject As String, EmailSignature As String
Dim CurrentMonth As String, DestFolder As String, PDFFile As String
Dim Email_To As String, Email_CC As String, Email_BCC As String
Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
Dim OverwritePDF As VbMsgBoxResult
Dim OutlookApp As Object, OutlookMail As Object
CurrentMonth = ""
EmailSubject = "Bestelling" & " " & Range("B4").Value & ", " & Range("B5").Value & ", " & Range("E5").Value
OpenPDFAfterCreating = False
AlwaysOverwritePDF = False
DisplayEmail = True
Email_To = EmailSectie("Algemeen")
Email_CC = ""
Email_BCC = ""
'Prompt for file destination
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = True Then
DestFolder = .SelectedItems(1)
Else
MsgBox "Je moet eerst een map kiezen waar om het PDF-bestand in op te slaan" & vbCrLf & vbCrLf & "Klik op OK om af te sluiten", vbCritical, "Je moet een map kiezen om de PDF in op te slaan."
Exit Sub
End If
End With
'Create new PDF file name including path and file extension
PDFFile = DestFolder & Application.PathSeparator & "Bestelling" & " " & Range("B4").Value & ", " & Range("B5").Value & ", " & Range("E5").Value & ".pdf"
BestandsNaam = "(" & DestFolder & Application.PathSeparator & ")" & "Bestelling" & " " & Range("B4").Value & ", " & Range("B5").Value & ", " & Range("E5").Value & ".pdf"
'If the PDF already exists
If Len(Dir(PDFFile)) > 0 Then
If AlwaysOverwritePDF = False Then
OverwritePDF = MsgBox(PDFFile & " bestaat al." & vbCrLf & vbCrLf & "Wil je dit bestand overschrijven, Leen de Slijper?", vbYesNo + vbQuestion, "Bestand bestaat al")
On Error Resume Next
'If you want to overwrite the file then delete the current one
If OverwritePDF = vbYes Then
Kill PDFFile
Else
MsgBox "Ok, als je hem niet wilt overschrijven, komen we niet verder" _
& vbCrLf & vbCrLf & "Klik op OK om af te sluiten.", vbCritical, "Macro beëindigen"
Exit Sub
End If
Else
On Error Resume Next
Kill PDFFile
End If
If Err.Number <> 0 Then
MsgBox "Het bestaande bestand kan niet worden verwijdered" _
& vbCrLf & vbCrLf & "Klik OK om de macro te stoppen", vbCritical, "Verwijderen niet mogelijk"
Exit Sub
End If
End If
'Create the PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating
'Create an Outlook object and new mail message
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
'Display email and specify To, Subject, etc
With OutlookMail
.Display
.To = Email_To
.CC = Email_CC
.BCC = Email_BCC
.Subject = EmailSubject
.Body = "Groeten," & vbNewLine & "- Leendert"
.Attachments.Add PDFFile
If DisplayEmail = False Then
.Send
End If
End With
Call BestandsnaamBestellingOpslaan
End Sub
Sub CommandButton23_Click()
Call create_and_email_pdf
End Sub
'Formulier leegmaken
Sub CommandButton1_Click()
Range("C7").Value = 1
CommandButton3.Visible = False
Rows("13:39").EntireRow.Hidden = True
Range("B4").Value = ""
Range("B5").Value = ""
Range("E5").Value = ""
Range("A10").Value = ""
Range("D10").Value = ""
Range("E10").Value = ""
Range("A13").Value = ""
Range("D13").Value = ""
Range("E13").Value = ""
Range("A16").Value = ""
Range("D16").Value = ""
Range("E16").Value = ""
Range("A19").Value = ""
Range("D19").Value = ""
Range("E19").Value = ""
Range("A22").Value = ""
Range("D22").Value = ""
Range("E22").Value = ""
Range("A25").Value = ""
Range("D25").Value = ""
Range("E25").Value = ""
Range("A28").Value = ""
Range("D28").Value = ""
Range("E28").Value = ""
Range("A31").Value = ""
Range("D31").Value = ""
Range("E31").Value = ""
Range("A34").Value = ""
Range("D34").Value = ""
Range("E34").Value = ""
Range("A37").Value = ""
Range("D37").Value = ""
Range("E37").Value = ""
Range("A41").Value = ""
End Sub
'Meer artikelen bestellen
Sub CommandButton4_Click()
If Range("C7").Value = 9 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:39").EntireRow.Hidden = False
CommandButton4.Visible = False
ElseIf Range("C7").Value = 8 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:36").EntireRow.Hidden = False
Rows("37:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 7 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:33").EntireRow.Hidden = False
Rows("34:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 6 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:30").EntireRow.Hidden = False
Rows("31:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 5 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:27").EntireRow.Hidden = False
Rows("28:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 4 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:24").EntireRow.Hidden = False
Rows("25:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 3 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:21").EntireRow.Hidden = False
Rows("22:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 2 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:18").EntireRow.Hidden = False
Rows("19:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 1 Then
Range("C7").Value = Range("C7").Value + 1
Rows("10:15").EntireRow.Hidden = False
Rows("16:39").EntireRow.Hidden = True
CommandButton3.Visible = True
End If
End Sub
'Minder artikelen bestellen
Sub CommandButton3_Click()
If Range("C7").Value = 2 Then
CommandButton3.Visible = False
Range("C7").Value = Range("C7").Value - 1
Rows("10:12").EntireRow.Hidden = False
Rows("13:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 3 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:15").EntireRow.Hidden = False
Rows("16:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 4 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:18").EntireRow.Hidden = False
Rows("19:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 5 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:21").EntireRow.Hidden = False
Rows("22:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 6 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:24").EntireRow.Hidden = False
Rows("25:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 7 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:27").EntireRow.Hidden = False
Rows("28:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 8 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:30").EntireRow.Hidden = False
Rows("31:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 9 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:33").EntireRow.Hidden = False
Rows("34:39").EntireRow.Hidden = True
ElseIf Range("C7").Value = 10 Then
Range("C7").Value = Range("C7").Value - 1
Rows("10:36").EntireRow.Hidden = False
Rows("37:39").EntireRow.Hidden = True
CommandButton4.Visible = True
End If
End Sub
Sub DatumBestellingOpslaan()
Dim R As Range, lastrow As Long
lastrow = Sheets("Overzicht bestellingen").Range("A:A").SpecialCells(4).Row
Sheets("Overzicht bestellingen").Range("A" & lastrow).Value = Range("B4").Value
End Sub
Sub LeverancierBestellingOpslaan()
Dim R As Range, lastrow As Long
lastrow = Sheets("Overzicht bestellingen").Range("B:B").SpecialCells(4).Row
Sheets("Overzicht bestellingen").Range("B" & lastrow).Value = Range("B5").Value
End Sub
Sub DoelBestellingOpslaan()
Dim R As Range, lastrow As Long
lastrow = Sheets("Overzicht bestellingen").Range("C:C").SpecialCells(4).Row
Sheets("Overzicht bestellingen").Range("C" & lastrow).Value = Range("E5").Value
End Sub
Sub StatusBestellingOpslaan()
Dim R As Range, lastrow As Long
lastrow = Sheets("Overzicht bestellingen").Range("D:D").SpecialCells(4).Row
Sheets("Overzicht bestellingen").Range("D" & lastrow).Value = "Formulier verwerkt"
End Sub
Sub BestandsnaamBestellingOpslaan()
Dim R As Range, lastrow As Long
lastrow = Sheets("Overzicht bestellingen").Range("E:E").SpecialCells(4).Row
Sheets("Overzicht bestellingen").Range("E" & lastrow).Value = BestandsNaam
End Sub
Function GroepWaarde(Groepnaam As String) As String
Dim objX As Object
With ActiveSheet
For Each objX In .OLEObjects
If TypeName(objX.Object) = "OptionButton" Then
If objX.Object.GroupName = Groepnaam And objX.Object Then
GroepWaarde = objX.Object.Caption
Exit Function
End If
End If
Next
End With
End Function
Function EmailSectie(AdresType) As String
If GroepWaarde("emailNadine") = AdresType Then EmailSectie = EmailSectie & "nadine@bedrijf.nl" & ";"
If GroepWaarde("emailEsther") = AdresType Then EmailSectie = EmailSectie & "esther@bedrijf.nl" & ";"
If GroepWaarde("emailJack") = AdresType Then EmailSectie = EmailSectie & "jack@bedrijf.nl" & ";"
If GroepWaarde("emailMilo") = AdresType Then EmailSectie = EmailSectie & "milo@bedrijf.nl" & ";"
If GroepWaarde("emailLeendert") = AdresType Then EmailSectie = EmailSectie & "leendert@bedrijf.nl" & ";"
End Function