Avatar billede sjokoman Juniormester
19. oktober 2007 - 05:32 Der er 14 kommentarer og
1 løsning

sende excel fil automatisk, når der er aviklet en VBA kode

Hej jeg fandt denne kode her på eksperten og satte den ind i et nyt regneark under ark 1, og det virkede fint:

Option Explicit
Sub SendTil()
  ActiveWorkbook.SendMail Recipients:="xxxxx@email.dk", Subject:="Vedhæftet ark"
End Sub

Mit spørgsmål er, jeg har et regneark med 2 ark og 4 moduler, hvor jeg har behov for at sende ark1 videre til to faste modtagere.
I modul 1 er der en masse kommandoer, der sender mail til forskellige modtagere, alt efter hvad jeg har tastet ind i ark1. Det er, når jeg "bruger" dette modul, at jeg også vil have sendt ark1. Jeg har sat ovennævnte ind først i arket, men den virker ikke.
Kan I hjælpe mig?


Modul1


Option Explicit


Sub SendTil()
  ActiveWorkbook.SendMail Recipients:="johnnymadsen@email.dk", Subject:="Vedhæftet ark"
End Sub





Sub SendEMail()
  Dim Email As String, Subj As String
  Dim MSG As String, URL As String
  Dim r As Integer, x As Double
 
  For r = 5 To 26  'data in rows 2-4 Rem Get the email address
      Email = Cells(r, 3)
    If Cells(r, 3) > "" Then
        bygMail r
  '  Else
  '      Cells(r, 7).Interior.ColorIndex = 3
       
    End If
  Next r
End Sub
Private Sub bygMail(r)
  Dim mailThis As Outlook.MailItem
  Dim MSG As String
  Dim Email As String
  Dim Subj As String
  Dim Disk As String, Filnavn As String
  Dim x As Long
  Disk = Cells(2, 6) & Format(Cells(2, 7), "dddd") & "\"
  ChDir Disk
  Filnavn = ".pdf"

  Set mailThis = CreateItem(olMailItem)
  Email = Cells(r, 3)
  '      Message subject Format(Cells(r, 3), "hh:mm:ss")
  If (Cells(r, 18)) <> "" Then
        Subj = "Rådighedstid " & "Fra " & Format(Cells(r, 9), "hh:mm") & " Til " & Format(Cells(r, 10), "hh:mm") & " Rådighedstid " & "Fra " & Format(Cells(r, 18), "hh:mm") & " Til " & Format(Cells(r, 19), "hh:mm")
  Else
        Subj = "1. tur skal køres " & "kl. " & Format(Cells(r, 10), "hh:mm  ") & "Fra " & Format(Cells(r, 13), "") & "        Sluttid " & Format(Cells(r, 11), "hh:mm  ") & "" & Format(Cells(r, 15), "")
  End If
 
  '      Compose the message
 
  MSG = MSG & "Hej  " & Cells(r, 4) & vbCrLf & vbCrLf
    If (Cells(r, 6)) <> "" Then
      MSG = MSG & Cells(r, 6) & vbCrLf & vbCrLf
    End If
  MSG = MSG & "Du har fået: " & vbCrLf & vbCrLf

 
 
  Select Case (Cells(r, 9))
      Case 1 To 500
        MSG = MSG & "HURLØB nr. " & Cells(r, 9) & vbCrLf & vbCrLf '& "  Rådighedstid fra:  " & Format(Cells(r, 10), "hh:mm") & " Til:  " & Format(Cells(r, 11), "hh:mm") & vbCrLf & vbCrLf & " " & Format(Cells(r, 12), "hh:mm") & " " & Cells(r, 13) & " " & Format(Cells(r, 14), "hh:mm") & " " & Cells(r, 15) & "" & Cells(r, 16) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KL" & Filnavn
      Case Else
  End Select
 
  Select Case (Cells(r, 17))
        Case 1 To 500
        MSG = MSG & "Og" & vbCrLf & vbCrLf
        MSG = MSG & "HURLØB " & Cells(r, 16) & "Rådighedstid " & Format(Cells(r, 17), "hh:mm") & " " & Format(Cells(r, 18), "hh:mm") & " Fra " & Format(Cells(r, 19), "hh:mm") & " " & Cells(r, 20) & " Til " & Format(Cells(r, 21), "hh:mm") & " " & Cells(r, 22) & " " & Cells(r, 23) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KL" & Filnavn
      Case Else
  End Select
 
  For x = 1 To 1
 
     
        If (Cells(5, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(5, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(5, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(5, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(6, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(6, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(6, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(6, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(7, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(7, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(7, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(7, 10), "hh:mm") & vbCrLf
        End If
       
               
        If (Cells(8, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(8, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(8, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(8, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(9, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(9, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(9, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(9, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(10, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(10, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(10, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(10, 10), "hh:mm") & vbCrLf
        End If
       
        If (Cells(11, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(11, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(11, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(11, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(12, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(12, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(12, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(12, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(13, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(13, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(13, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(13, 10), "hh:mm") & vbCrLf
        End If
       
        If (Cells(14, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(14, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(14, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(14, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(15, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(15, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(15, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(15, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(16, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(16, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(16, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(16, 10), "hh:mm") & vbCrLf
        End If
       
        If (Cells(17, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(17, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(17, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(17, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(18, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(18, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(18, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(18, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(19, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(19, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(19, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(19, 10), "hh:mm") & vbCrLf
        End If
       
        If (Cells(20, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(20, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(20, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(20, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(21, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(21, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(21, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(21, 10), "hh:mm") & vbCrLf
        End If
             
        If (Cells(22, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(22, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(22, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(22, 10), "hh:mm") & vbCrLf
        End If
       
        If (Cells(23, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(23, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(23, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(23, 10), "hh:mm") & vbCrLf
        End If

        If (Cells(24, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(24, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(24, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(24, 10), "hh:mm") & vbCrLf
        End If
       
        MSG = MSG & "" & vbCrLf & vbCrLf
        MSG = MSG & "Idag: " & vbCrLf & vbCrLf

     
        If (Cells(19, 26)) <> "" Then
        MSG = MSG & Cells(19, 26) & vbCrLf
        End If
        If (Cells(20, 26)) <> "" Then
        MSG = MSG & Cells(20, 26) & vbCrLf
        End If
       
        If (Cells(21, 26)) <> "" Then
        MSG = MSG & Cells(21, 26) & vbCrLf
        End If
        If (Cells(22, 26)) <> "" Then
        MSG = MSG & Cells(22, 26) & vbCrLf
        End If
        If (Cells(23, 26)) <> "" Then
        MSG = MSG & Cells(23, 26) & vbCrLf
        End If
        If (Cells(24, 26)) <> "" Then
        MSG = MSG & Cells(24, 26) & vbCrLf
        End If
       
       
        MSG = MSG & "Din rådighedstid starter ca dagens timetal trukket fra sluttiden. Spontanture kan forekomme!!" & vbCrLf
       
       
        If (Cells(1, 3)) <> "" Then
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & Cells(1, 3) & vbCrLf
        Else
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & "Vagten" & vbCrLf
        End If
       
       
       
       
       
        If Application.Wait(Now + TimeValue("0:00:02")) Then
        Cells(r, 7) = "X"
        End If

  Next
  mailThis.Recipients.Add Email
  mailThis.Subject = Subj
  mailThis.Body = MSG
  mailThis.Send 'Send mailen med det samme!!
'  mailThis.Save 'Gem mailen i kladder. Men hvis outlook er lukket bliver de lavt i inbox!!
'  mailThis.Display

End Sub
Avatar billede kabbak Professor
19. oktober 2007 - 09:25 #1
Jeg har bygget lidt om på  makroen "bygMail", prøv at se om det virker.

den første burde se sådan ud


Sub SendTil()
Sheets("Ark1").Copy
  ActiveWorkbook.SendMail Recipients:="johnnymadsen@email.dk", Subject:="Vedhæftet ark"
  ActiveWindow.Close False
End Sub





Sub SendEMail()
  Dim Email As String, Subj As String
  Dim MSG As String, URL As String
  Dim r As Integer, x As Double

  For r = 5 To 26  'data in rows 2-4 Rem Get the email address
      Email = Cells(r, 3)
    If Cells(r, 3) > "" Then
        bygMail r
  '  Else
  '      Cells(r, 7).Interior.ColorIndex = 3
     
    End If
  Next r
End Sub
Private Sub bygMail(r)
  Dim mailThis As Outlook.MailItem
  Dim MSG As String
  Dim Email As String
  Dim Subj As String
  Dim Disk As String, Filnavn As String
  Dim x As Long
  Disk = Cells(2, 6) & Format(Cells(2, 7), "dddd") & "\"
  ChDir Disk
  Filnavn = ".pdf"

  Set mailThis = CreateItem(olMailItem)
  Email = Cells(r, 3)
  '      Message subject Format(Cells(r, 3), "hh:mm:ss")
  If (Cells(r, 18)) <> "" Then
        Subj = "Rådighedstid " & "Fra " & Format(Cells(r, 9), "hh:mm") & " Til " & Format(Cells(r, 10), "hh:mm") & " Rådighedstid " & "Fra " & Format(Cells(r, 18), "hh:mm") & " Til " & Format(Cells(r, 19), "hh:mm")
  Else
        Subj = "1. tur skal køres " & "kl. " & Format(Cells(r, 10), "hh:mm  ") & "Fra " & Format(Cells(r, 13), "") & "        Sluttid " & Format(Cells(r, 11), "hh:mm  ") & "" & Format(Cells(r, 15), "")
  End If

  '      Compose the message

  MSG = MSG & "Hej  " & Cells(r, 4) & vbCrLf & vbCrLf
    If (Cells(r, 6)) <> "" Then
      MSG = MSG & Cells(r, 6) & vbCrLf & vbCrLf
    End If
  MSG = MSG & "Du har fået: " & vbCrLf & vbCrLf



  Select Case (Cells(r, 9))
      Case 1 To 500
        MSG = MSG & "HURLØB nr. " & Cells(r, 9) & vbCrLf & vbCrLf '& "  Rådighedstid fra:  " & Format(Cells(r, 10), "hh:mm") & " Til:  " & Format(Cells(r, 11), "hh:mm") & vbCrLf & vbCrLf & " " & Format(Cells(r, 12), "hh:mm") & " " & Cells(r, 13) & " " & Format(Cells(r, 14), "hh:mm") & " " & Cells(r, 15) & "" & Cells(r, 16) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KL" & Filnavn
      Case Else
  End Select

  Select Case (Cells(r, 17))
        Case 1 To 500
        MSG = MSG & "Og" & vbCrLf & vbCrLf
        MSG = MSG & "HURLØB " & Cells(r, 16) & "Rådighedstid " & Format(Cells(r, 17), "hh:mm") & " " & Format(Cells(r, 18), "hh:mm") & " Fra " & Format(Cells(r, 19), "hh:mm") & " " & Cells(r, 20) & " Til " & Format(Cells(r, 21), "hh:mm") & " " & Cells(r, 22) & " " & Cells(r, 23) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KL" & Filnavn
      Case Else
  End Select

  For x = 5 To 24

   
        If (Cells(x, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(x, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(x, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(x, 10), "hh:mm") & vbCrLf
        End If

     
        MSG = MSG & "" & vbCrLf & vbCrLf
        MSG = MSG & "Idag: " & vbCrLf & vbCrLf

   
        If (Cells(19, 26)) <> "" Then
        MSG = MSG & Cells(19, 26) & vbCrLf
        End If
        If (Cells(20, 26)) <> "" Then
        MSG = MSG & Cells(20, 26) & vbCrLf
        End If
     
        If (Cells(21, 26)) <> "" Then
        MSG = MSG & Cells(21, 26) & vbCrLf
        End If
        If (Cells(22, 26)) <> "" Then
        MSG = MSG & Cells(22, 26) & vbCrLf
        End If
        If (Cells(23, 26)) <> "" Then
        MSG = MSG & Cells(23, 26) & vbCrLf
        End If
        If (Cells(24, 26)) <> "" Then
        MSG = MSG & Cells(24, 26) & vbCrLf
        End If
     
     
        MSG = MSG & "Din rådighedstid starter ca dagens timetal trukket fra sluttiden. Spontanture kan forekomme!!" & vbCrLf
     
     
        If (Cells(1, 3)) <> "" Then
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & Cells(1, 3) & vbCrLf
        Else
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & "Vagten" & vbCrLf
        End If
     
     
     
     
     
        If Application.Wait(Now + TimeValue("0:00:02")) Then
        Cells(r, 7) = "X"
        End If

  Next
  mailThis.Recipients.Add Email
  mailThis.Subject = Subj
  mailThis.Body = MSG
  mailThis.Send 'Send mailen med det samme!!
'  mailThis.Save 'Gem mailen i kladder. Men hvis outlook er lukket bliver de lavt i inbox!!
'  mailThis.Display

End Sub
Avatar billede kabbak Professor
19. oktober 2007 - 10:00 #2
Vi skal nok have flyttet next

Private Sub bygMail(r)
  Dim mailThis As Outlook.MailItem
  Dim MSG As String
  Dim Email As String
  Dim Subj As String
  Dim Disk As String, Filnavn As String
  Dim x As Long
  Disk = Cells(2, 6) & Format(Cells(2, 7), "dddd") & "\"
  ChDir Disk
  Filnavn = ".pdf"

  Set mailThis = CreateItem(olMailItem)
  Email = Cells(r, 3)
  '      Message subject Format(Cells(r, 3), "hh:mm:ss")
  If (Cells(r, 18)) <> "" Then
        Subj = "Rådighedstid " & "Fra " & Format(Cells(r, 9), "hh:mm") & " Til " & Format(Cells(r, 10), "hh:mm") & " Rådighedstid " & "Fra " & Format(Cells(r, 18), "hh:mm") & " Til " & Format(Cells(r, 19), "hh:mm")
  Else
        Subj = "1. tur skal køres " & "kl. " & Format(Cells(r, 10), "hh:mm  ") & "Fra " & Format(Cells(r, 13), "") & "        Sluttid " & Format(Cells(r, 11), "hh:mm  ") & "" & Format(Cells(r, 15), "")
  End If

  '      Compose the message

  MSG = MSG & "Hej  " & Cells(r, 4) & vbCrLf & vbCrLf
    If (Cells(r, 6)) <> "" Then
      MSG = MSG & Cells(r, 6) & vbCrLf & vbCrLf
    End If
  MSG = MSG & "Du har fået: " & vbCrLf & vbCrLf



  Select Case (Cells(r, 9))
      Case 1 To 500
        MSG = MSG & "HURLØB nr. " & Cells(r, 9) & vbCrLf & vbCrLf '& "  Rådighedstid fra:  " & Format(Cells(r, 10), "hh:mm") & " Til:  " & Format(Cells(r, 11), "hh:mm") & vbCrLf & vbCrLf & " " & Format(Cells(r, 12), "hh:mm") & " " & Cells(r, 13) & " " & Format(Cells(r, 14), "hh:mm") & " " & Cells(r, 15) & "" & Cells(r, 16) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 10) & Cells(r, 9) & "KL" & Filnavn
      Case Else
  End Select

  Select Case (Cells(r, 17))
        Case 1 To 500
        MSG = MSG & "Og" & vbCrLf & vbCrLf
        MSG = MSG & "HURLØB " & Cells(r, 16) & "Rådighedstid " & Format(Cells(r, 17), "hh:mm") & " " & Format(Cells(r, 18), "hh:mm") & " Fra " & Format(Cells(r, 19), "hh:mm") & " " & Cells(r, 20) & " Til " & Format(Cells(r, 21), "hh:mm") & " " & Cells(r, 22) & " " & Cells(r, 23) & vbCrLf & vbCrLf
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KV" & Filnavn
        mailThis.Attachments.Add Disk & Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now() + 1, "dd") & Cells(2, 9) & Cells(r, 16) & "KL" & Filnavn
      Case Else
  End Select

  For x = 5 To 24

 
        If (Cells(x, 2)) <> "" Then
        MSG = MSG & vbCrLf & "Løb  "
        MSG = MSG & Cells(x, 1)
        MSG = MSG & " Køres af:  "
        MSG = MSG & Cells(x, 2)
        MSG = MSG & "  med 1.tur kl:  "
        MSG = MSG & Format(Cells(x, 10), "hh:mm") & vbCrLf
        End If

    Next 
        MSG = MSG & "" & vbCrLf & vbCrLf
        MSG = MSG & "Idag: " & vbCrLf & vbCrLf

 
        If (Cells(19, 26)) <> "" Then
        MSG = MSG & Cells(19, 26) & vbCrLf
        End If
        If (Cells(20, 26)) <> "" Then
        MSG = MSG & Cells(20, 26) & vbCrLf
        End If
   
        If (Cells(21, 26)) <> "" Then
        MSG = MSG & Cells(21, 26) & vbCrLf
        End If
        If (Cells(22, 26)) <> "" Then
        MSG = MSG & Cells(22, 26) & vbCrLf
        End If
        If (Cells(23, 26)) <> "" Then
        MSG = MSG & Cells(23, 26) & vbCrLf
        End If
        If (Cells(24, 26)) <> "" Then
        MSG = MSG & Cells(24, 26) & vbCrLf
        End If
   
   
        MSG = MSG & "Din rådighedstid starter ca dagens timetal trukket fra sluttiden. Spontanture kan forekomme!!" & vbCrLf
   
   
        If (Cells(1, 3)) <> "" Then
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & Cells(1, 3) & vbCrLf
        Else
        MSG = MSG & vbCrLf & "Med Venlig Hilsen" & vbCrLf
        MSG = MSG & "Vagten" & vbCrLf
        End If
   
   
   
   
   
        If Application.Wait(Now + TimeValue("0:00:02")) Then
        Cells(r, 7) = "X"
        End If


  mailThis.Recipients.Add Email
  mailThis.Subject = Subj
  mailThis.Body = MSG
  mailThis.Send 'Send mailen med det samme!!
'  mailThis.Save 'Gem mailen i kladder. Men hvis outlook er lukket bliver de lavt i inbox!!
'  mailThis.Display

End Sub
Avatar billede sjokoman Juniormester
19. oktober 2007 - 18:36 #3
Tak for hjælpen, prøver i weekenden og vender tilbage.

Johnny
Avatar billede sjokoman Juniormester
20. oktober 2007 - 21:45 #4
Det virkede ikke. Jeg kalder første ark for "Plan", men det har jeg naturligvis ændret i koden. Jeg kunne godt tænke mig, at alle de mails jeg sender bliver sendt først, de bliver nemlig krydset af i arket, når de er sendt, inden det første ark bliver sendt til mig. Kan man ikke bare sætte et nyt modul på efter, eller hvordan er det?
mvh Johnny
Avatar billede kabbak Professor
20. oktober 2007 - 22:26 #5
jo det kan du sagtens, virkede denne kode ikke




Sub SendTil()
Sheets("Ark1").Copy
  ActiveWorkbook.SendMail Recipients:="johnnymadsen@email.dk", Subject:="Vedhæftet ark"
  ActiveWindow.Close False
End Sub
Avatar billede sjokoman Juniormester
20. oktober 2007 - 22:46 #6
Næh, den virkede ikke. :-(
Jeg har prøvet at sætte den alene ind i et modul, det virkede ikke

Men det andet virker som normalt. Jeg bruger noget der hedder noget i retning af rendemtion og et lille program der siger ja til at sende email, så jeg ikke skal sidde og acceptere hver enkelt email, men det skulle vel ikke have indflydelse?
Avatar billede kabbak Professor
21. oktober 2007 - 09:36 #7
Avatar billede sjokoman Juniormester
21. oktober 2007 - 13:32 #8
Hvis jeg bruger nedenstående og kører makroen manuelt, så virker den fint. Jeg har flere moduler og har sat koden ind i modul 5. Men meningen er, at når Modul 1 er blevet kørt, så skal denne kode automatisk gå i gang og sende en kopi af det hændte til mig.
Eller skal jeg indsætte en knap? til at afvikle den

Mvh johnny  godt link! selvom jeg ikke helt forstår, hvordan præcist koden sættes ind, om der er i ThisWorkBook osv,


Sub Mail_workbook_1()
'Working in 97-2007
    Dim wb As Workbook

    Set wb = ActiveWorkbook



    If Val(Application.Version) >= 12 Then
        If wb.FileFormat = 51 And wb.HasVBProject = True Then
            MsgBox "There is VBA code in this xlsx file, there will be no VBA code in the file you send." & vbNewLine & _
                  "Save the file first as xlsm and then try the macro again.", vbInformation
            Exit Sub
        End If
    End If



    On Error Resume Next
    wb.SendMail "johnnymadsen@email.dk", _
                "This is the Subject line"
    On Error GoTo 0

End Sub
Avatar billede kabbak Professor
21. oktober 2007 - 14:08 #9
du sætter bareet kald nederst i koden på Modul 1

Call Mail_workbook_1
Avatar billede sjokoman Juniormester
21. oktober 2007 - 20:54 #10
Send et svar, det virker! Foreløbig, så kommer den endelige test, når det skal bruges ude i livet :-)
mvh Johnny
Avatar billede kabbak Professor
21. oktober 2007 - 21:01 #11
et svar ;-))
Avatar billede sjokoman Juniormester
22. oktober 2007 - 07:31 #12
Jeg takker og bygger videre til nye spørgsmål....
Avatar billede sjokoman Juniormester
22. oktober 2007 - 19:33 #13
Alligevel ikke helt godt. Hver gang der bliver sendt en mail, bliver der også sendt en kopi af regnearket. I dag sendte vi 18 mails og jeg fik 18 kopier af arket. hvordan kan jeg begrænse til 1?

mvh Johnny
Avatar billede kabbak Professor
22. oktober 2007 - 19:37 #14
Hvis du bruer den kode du viser i spørgsmålet, så skal den ind sådan, uden for løkken.

Sub SendEMail()
  Dim Email As String, Subj As String
  Dim MSG As String, URL As String
  Dim r As Integer, x As Double

  For r = 5 To 26  'data in rows 2-4 Rem Get the email address
      Email = Cells(r, 3)
    If Cells(r, 3) > "" Then
        bygMail r
  '  Else
  '      Cells(r, 7).Interior.ColorIndex = 3
   
    End If
  Next r

Call Mail_workbook_1' ***************************************

End Sub
Avatar billede sjokoman Juniormester
25. oktober 2007 - 05:29 #15
Virker fint nu, mange tak

Johnny
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester