Avatar billede splokit Nybegynder
05. september 2006 - 09:25 Der er 1 kommentar og
2 løsninger

VB Mail from excel

Hey jeg har fundet en vb kode til at sende emails
har lavet lidt om på det men kan ikke få det til at virke.
jeg kunne også godt tænke mig at den kunne vedhæfte filer
for hver (r, 4 & 10 & 16) = et nummer som er en del af et filnavn
FileName(r, 4) & "a" & ".pdf" og FileName(r, 4) & "b" & ".pdf" for hver at de 3 celler i rækken som har talværdi skal den vedhæfte de filer a og b for hver celle i r, 4 & 10 & 16
filerne finder den i Dir// C:/Text/1/
Hvis det kan lade sig gøre.

Ellers bare hvis der ikke er en mail gå til næste række!!
200 for at løse hele problemet og 100 for hvis mail ikke er der!!

Private 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 SendEMail()
    Dim Email As String, Subj As String
    Dim Msg As String, URL As String
    Dim r As Integer, x As Double
    For r = 2 To 25 'data in rows 2-4
'      Get the email address
        Email = Cells(r, 21)
       
       
        If Cells(r, 21) = "" Then
          Next r
            If Cells(r, 21) <> "" Then
           
                       

'      Message subject Format(Cells(r, 2), "hh:mm:ss")
        Subj = "Filer" & Cells(r, 4) & ", " & Cells(r, 10) & ", " & Cells(r, 16)

'      Compose the message
        Msg = "KL" & Cells(r, 2) & vbCrLf
                                    'Tid
        Msg = Msg & "" & Format(Cells(r, 1), "hh:mm") & vbCrLf & vbCrLf
        Msg = Msg & "" & Cells(r, 4) & " Fra " & Format(Cells(r, 5), "hh:mm") & " " & Cells(r, 6) & " Til " _
        & Format(Cells(r, 7), "hh:mm") & " " & Cells(r, 8) & " Info " & Cells(r, 9) & vbCrLf
        Msg = Msg & "Og Vif " & Cells(r, 10) & " Fra " & Format(Cells(r, 11), "hh:mm") & " " & Cells(r, 12) & " Til " _
        & Format(Cells(r, 13), "hh:mm") & " " & Cells(r, 14) & " Info " & Cells(r, 15) & vbCrLf
        Msg = Msg & "Og Vif " & Cells(r, 16) & " Fra " & Format(Cells(r, 17), "hh:mm") & " " & Cells(r, 18) & " Til " _
        & Format(Cells(r, 19), "hh:mm") & " " & Cells(r, 20) & vbCrLf
       
       
       
        Msg = Msg & "Hilsen" & vbCrLf
        Msg = Msg & "Mig"
       
'      Replace spaces with %20 (hex)
        Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
        Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")
               
'      Replace carriage returns with %0D%0A (hex)
        Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")
'      Create the URL
        URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg
       
'      Execute the URL (start the email client)
        ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

'      Wait two seconds before sending keystrokes
        Application.Wait (Now + TimeValue("0:00:02"))
        Application.SendKeys "%s"
        End If
    Next r
End Sub
Avatar billede supertekst Ekspert
05. september 2006 - 15:24 #1
Et skridt på vejen - kan nu opbygge en mail:

Private 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 SendEMail()
    Dim Email As String, Subj As String
    Dim Msg As String, URL As String
    Dim r As Integer, x As Double
   
    For r = 2 To 25 'data in rows 2-4
Rem Get the email address
        Email = Cells(r, 21)
       
        If Cells(r, 21) <> "" Then
            bygMail r
        End If
    Next r
End Sub
Private Sub bygMail(r)
'      Message subject Format(Cells(r, 2), "hh:mm:ss")
        Subj = "Filer" & Cells(r, 4) & ", " & Cells(r, 10) & ", " & Cells(r, 16)

Rem Compose the message
        Msg = "KL" & Cells(r, 2) & vbCrLf
                                    'Tid
        Msg = Msg & "" & Format(Cells(r, 1), "hh:mm") & vbCrLf & vbCrLf
        Msg = Msg & "" & Cells(r, 4) & " Fra " & Format(Cells(r, 5), "hh:mm") & " " & Cells(r, 6) & " Til " _
        & Format(Cells(r, 7), "hh:mm") & " " & Cells(r, 8) & " Info " & Cells(r, 9) & vbCrLf
        Msg = Msg & "Og Vif " & Cells(r, 10) & " Fra " & Format(Cells(r, 11), "hh:mm") & " " & Cells(r, 12) & " Til " _
        & Format(Cells(r, 13), "hh:mm") & " " & Cells(r, 14) & " Info " & Cells(r, 15) & vbCrLf
        Msg = Msg & "Og Vif " & Cells(r, 16) & " Fra " & Format(Cells(r, 17), "hh:mm") & " " & Cells(r, 18) & " Til " _
        & Format(Cells(r, 19), "hh:mm") & " " & Cells(r, 20) & vbCrLf
       
       
        Msg = Msg & "Hilsen" & vbCrLf
        Msg = Msg & "Mig"
       
Rem Replace spaces with %20 (hex)
        Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
        Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")
               
Rem Replace carriage returns with %0D%0A (hex)
        Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")
Rem Create the URL
        URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg
       
Rem Execute the URL (start the email client)
        ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

Rem Wait two seconds before sending keystrokes
        Application.Wait (Now + TimeValue("0:00:02"))
        Application.SendKeys "%s"
End Sub
Avatar billede splokit Nybegynder
06. september 2006 - 07:24 #2
Private Sub bygMail(r)
Email = Cells(r, 21)

så virker dem.
Avatar billede supertekst Ekspert
06. september 2006 - 09:20 #3
OK!
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