Advertisement
Guest User

VBExcel

a guest
Mar 26th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub CheckBox2_Click()
  2.     ChangeCheckBoxValue "CheckBox2"
  3.    
  4.     If (ActiveSheet.CheckBoxes("CheckBox2").Value = True) Then
  5.         EnableCheckbox "CheckBox3"
  6.         DisableCheckbox "CheckBox2"
  7.     Else
  8.         EnableCheckbox "CheckBox2"
  9.         DisableCheckbox "CheckBox3"
  10.     End If
  11.     SendEmail "szymon.kolanowski@suez.com"
  12. End Sub
  13.  
  14. Sub CheckBox3_Click()
  15.     ChangeCheckBoxValue "Check Box 3"
  16.    
  17.     If (ActiveSheet.CheckBoxes("Check Box 3").Value = True) Then
  18.         EnableCheckbox "Check Box 4"
  19.         DisableCheckbox "Check Box 3"
  20.     Else
  21.         EnableCheckbox "Check Box 3"
  22.         DisableCheckbox "Check Box 4"
  23.     End If
  24.     SendEmail "s.kolano@amorki.pl"
  25. End Sub
  26. Sub ChangeCheckBoxValue(checkBoxName)
  27.     Dim checkBoxValue As Boolean
  28.     checkBoxValue = ActiveSheet.CheckBoxes(checkBoxName).Value
  29.     If (checkBoxValue = True) Then
  30.         ActiveSheet.CheckBoxes(checkBoxName).Value = False
  31.     Else
  32.         ActiveSheet.CheckBoxes(checkBoxName).Value = True
  33.     End If
  34. End Sub
  35. Sub EnableCheckbox(checkBoxName)
  36.     ActiveSheet.CheckBoxes(checkBoxName).Enabled = True
  37. End Sub
  38.  
  39. Sub DisableCheckbox(checkBoxName)
  40.     For Each cb In ActiveSheet.Shapes
  41.         If cb.Name = checkBoxName Then
  42.        
  43.         Else
  44.             If cb.Type = msoFormControl Then
  45.                 If cb.FormControlType = xlCheckBox Then
  46.                     cb.ControlFormat.Enabled = False
  47.                 End If
  48.             End If
  49.         End If
  50.         ActiveSheet.CheckBoxes(checkBoxName).Enabled = False
  51.     Next cb
  52. End Sub
  53. Sub SendEmail(toAddress As String)
  54.     On Error Resume Next
  55.     Dim iMsg As Object, iConf As Object
  56.     Const SendUsingPort = 2
  57.     Set iMsg = CreateObject("CDO.Message")
  58.     Set iConf = CreateObject("CDO.Configuration")
  59.    
  60.     Set wb1 = ActiveWorkbook
  61.  
  62.     'Make a copy of the file/Open it/Mail it/Delete it
  63.    'If you want to change the file name then change only TempFileName
  64.    TempFilePath = Environ$("temp") & "\"
  65.     TempFileName = wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
  66.     FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
  67.  
  68.     wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
  69.  
  70.     'konfiguracja
  71.    With iConf.Fields
  72.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = SendUsingPort
  73.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  74.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.48.1.115"
  75.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
  76.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0
  77.         .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 0
  78.         .Update
  79.     End With
  80.    
  81.     'wiadomość
  82.    With iMsg
  83.         Set .Configuration = iConf
  84.         .TextBody = "Zerknij do załącznika"
  85.         .Subject = "Wypełnij załącznik"
  86.         .From = "helpdesk.polska@suez.com"
  87.         .To = toAddress
  88.         .AddAttachment TempFilePath & TempFileName & FileExtStr
  89.         .Send
  90.     End With
  91.     Set iMsg = Nothing
  92.     Set iConf = Nothing
  93.    
  94.     'obsługa błędów
  95.    If Err.Number <> 0 Then
  96.         If Err.Number = &H80040213 Then
  97.             MsgBox Err.Description, 48, "Błąd serwera SMTP"
  98.         Else
  99.             MsgBox Err.Description, 48, "Błąd Hx" & Hex(Err.Number)
  100.         End If
  101.     Else
  102.         MsgBox "OK, wysłano.", 64
  103.     End If
  104. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement