
Sub inviaelenco()
Dim MyWd As Object
cartella = Range("I2")
fname = Dir(cartella & "*.docx")
r = 2
Sheets(1).Range("B2:B500").ClearContents
Do While fname <> ""
Sheets(2).Columns(1).ClearContents
Set MyWd = GetObject(cartella & "" & fname)
MyWd.ActiveWindow.Selection.WholeStory
MyWd.ActiveWindow.Selection.Copy
With Sheets(2)
.Range("A1").PasteSpecial xlPasteValues
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
mmail = .Range("A" & LR - 1)
mmail = Trim(Right(mmail, Len(mmail) - 7))
End With
Sheets(1).Range("B" & r) = mmail
Sheets(1).Range("E" & r) = fname
r = r + 1
MyWd.Close
fname = Dir
Loop
Sheets(1).Select
LR = Cells(Rows.Count, "B").End(xlUp).Row
For r = 2 To LR
Call InviaMail(r)
Next
End Sub
Sub InviaMail(r)
Da = Range("A2") ' indirizzo accesso
Text = Range("D2") ' Testo
Ogg = Range("C2") ' breve descrizione dell'oggetto del messaggio
achiTo = Range("B" & r) ' indirizzi di posta elettronica dei destinatari principali.
codice = Range("F2") 'password accesso
server = Range("G2") ' server
port = Range("H2") ' porta
Att1 = Range("I2") & Range("E" & r)
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpserver") = server
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smptserverport") = port
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendusername") = Da ' indirizzo accesso
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendpassword") = codice 'password accesso
.Update
End With
With cdomsg
.To = achiTo
.From = Da
.Subject = Ogg
.TextBody = Text
.AddAttachment Att1
.Send
End With
Set cdomsg = Nothing
End Sub
|
se esegui il form con vba dentro word mi da errore di sintassi!!! Dove ho sbagliato? |
Loop Sheets(1).Select LR = Cells(Rows.Count, "B").End(xlUp).Row Stop For r = 2 To LR Call InviaMail(r) Next End Sub Sub InviaMail(r) |
Sub inviaelenco()
Dim MyWd As Object
cartella = Range("I2")
fname = Dir(cartella & "*.docx")
r = 2
Sheets(1).Range("B2:B500").ClearContents
Do While fname <> ""
Sheets(2).Columns(1).ClearContents
Set MyWd = GetObject(cartella & "" & fname)
MyWd.ActiveWindow.Selection.WholeStory
MyWd.ActiveWindow.Selection.Copy
With Sheets(2)
.Range("A1").PasteSpecial xlPasteValues
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
mmail = .Range("A" & LR - 1)
mmail = Trim(Right(mmail, Len(mmail) - 7))
fname = Left(fname, Len(fname) - 4) & "pdf"
pdfname = cartella & "" & fname
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfname, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
Sheets(1).Range("B" & r) = mmail
Sheets(1).Range("E" & r) = fname
r = r + 1
MyWd.Close
fname = Dir
Loop
Sheets(1).Select
LR = Cells(Rows.Count, "B").End(xlUp).Row
For r = 2 To LR
Call InviaMail(r)
Next
End Sub
Sub InviaMail(r)
Da = Range("A2") ' indirizzo accesso
Text = Range("D2") ' Testo
Ogg = Range("C2") ' breve descrizione dell'oggetto del messaggio
achiTo = Range("B" & r) ' indirizzi di posta elettronica dei destinatari principali.
codice = Range("F2") 'password accesso
server = Range("G2") ' server
port = Range("H2") ' porta
Att1 = Range("I2") & "" & Range("E" & r)
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpserver") = server
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smptserverport") = port
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("h t t p://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendusername") = Da ' indirizzo accesso
.Item("h t t p://schemas.microsoft.com/cdo/configuration/sendpassword") = codice 'password accesso
.Update
End With
With cdomsg
.To = achiTo
.From = Da
.Subject = Ogg
.TextBody = Text
.AddAttachment Att1
.Send
End With
Set cdomsg = Nothing
End Sub |
