vba - How do I create and email by taking the adresses from word and having the word as an attachment using visual basic? -
i have word document contains table email addresses. want addresses document , and open lotus notes email set default email service having addresses added "to:" field , document attachment. connected lotus notes, want mail start having addresses , attachment in place , not sent automatically. have code gets addresses table:
sub send_mail_recipients() 'nimo 08-jun-2013 'send-mail distribution list dim text string dim char string dim rowcount, n_address, n_cells, cell_crt, charno integer dim recipient(100) variant 'with application.activewindow.document 'activate document 'n_address = 0 text = "" activedocument.tables(2).select n_cells = selection.cells.count cell_crt = 1 n_cells if selection.cells(cell_crt).range.text "*@*" 'recipient(n_address) = selection.cells(cell_crt).range.text text = text + selection.cells(cell_crt).range.text + ", " 'n_address = n_address + 1 end if 'text = selection.cells(cell_crt).range.text next
visual basic provides method open mail havin document attachment:
'if n_address = 0 if text = "" myerrmessage = msgbox("the document has no email addresses!", vbokonly, "error") else options.sendmailattach = true activedocument.sendmail
and found function adds email addresses give parameter mail:
private declare function shellexecute lib "shell32.dll" alias _ "shellexecutea" (byval hwnd long, byval lpoperation _ string, byval lpfile string, byval lpparameters _ string, byval lpdirectory string, _ byval nshowcmd long) long private function openemail(byval emailaddress string, _ optional subject string, optional body string) _ boolean dim lwindow long dim lret long dim sparams string sparams = emailaddress if lcase(left(sparams, 7)) <> "mailto:" _ sparams = "mailto:" & sparams if subject <> "" sparams = sparams & "?subject=" & subject if body <> "" sparams = sparams & iif(subject = "", "?", "&") sparams = sparams & "body=" & body end if lret = shellexecute(lwindow, "open", sparams, _ vbnullstring, vbnullstring, sw_show) openemail = lret = 0 end function openemail text, "", ""
but need way have both addresses , attachment in same mail.
this function i've used in past notes. doesn't require passwords etc typed afaik.
sub sendnotesmail(subject string, attachment string, recipient variant, bodytext string, saveit boolean) 'public sub sendnotesmail(subject string, attachment string, 'recipient string, bodytext string,saveit boolean) 'this public sub send mail , attachment if neccessary 'recipient including body text. 'requires notes client installed on system. 'set objects required automation lotus notes dim maildb object 'the mail database dim username string 'the current users notes name dim maildbname string 'the current users notes mail database name dim maildoc object 'the mail document dim attachme object 'the attachment richtextfile object dim session object 'the notes session dim embedobj object 'the embedded object (attachment) 'start session notes set session = createobject("notes.notessession") 'next line works 5.x , above. replace password password 'get sessions username , calculate mail file name 'you may or may not need maildbname systems 'can pass empty string or using above password can use other mailboxes. username = session.username maildbname = left$(username, 1) & right$(username, (len(username) - instr(1, username, " "))) & ".nsf" 'open mail database in notes set maildb = session.getdatabase("", maildbname) if maildb.isopen = true 'already open mail else maildb.openmail end if 'set new mail document set maildoc = maildb.createdocument maildoc.form = "memo" maildoc.sendto = recipient maildoc.subject = subject maildoc.body = bodytext maildoc.savemessageonsend = saveit 'set embedded object , attachment , attach if attachment <> "" set attachme = maildoc.createrichtextitem("attachment") set embedobj = attachme.embedobject(1454, "", attachment, "attachment") ' maildoc.createrichtextitem ("attachment") end if 'send document maildoc.posteddate = now() 'gets mail appear in sent items folder maildoc.send 0, recipient 'clean set maildb = nothing set maildoc = nothing set attachme = nothing set session = nothing set embedobj = nothing end sub
Comments
Post a Comment