如何添加其他代码,然后通过Gmail通过Vba发送电子邮件?

时间:2015-12-11 14:42:19

标签: excel vba excel-vba email

我见过类似的问题,但我的具体问题让我解释一下。

我有一个从按钮运行的代码,如果单击它将活动工作表作为pdf打印到与我的工作簿相同的路径,这可以正常工作,因为活动工作表信息通过列表更改以显示每个客户信息每个财政期间。

每个月我都需要将这些pdf文件中的每一个附加到电子邮件中并发送给客户,这是一个漫长的过程,如果我输入电子邮件以在选择客户时自动出现在单元格中,例如(“E1”)我可以调整我的代码来打开电子邮件并将pdf发送到该电子邮件地址吗?

Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = ActiveSheet

'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(Range("B1"), "", ""), "", "") _
        & " Period " _
        & Format(Now(), Cells.Range("J1")) _
        & ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

If myFile <> "False" Then
        ws.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False


 End If

exitHandler:
     Exit Sub
errHandler:
     MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub

2 个答案:

答案 0 :(得分:1)

您可以通过Outlook自动发送电子邮件,但似乎访问Gmail并通过网络发送邮件似乎完全不同。 Gmail确实有一个API,您可以在此处获取文档:https://developers.google.com/gmail/api/

我的建议是使用您的Gmail帐户设置Outlook,然后发送,这更容易。

答案 1 :(得分:0)

Ron DeBruins网站上我发现了这个并成功测试过。我必须启用&#34;所有安全性较低的应用程序&#34;在我的Gmail设置中。这是代码,以防他的网站出现故障。

Sub CDO_Mail_Small_Text_2()
    Dim iMsg As Object
    Dim iConf As Object
    Dim strbody As String
    Dim Flds As Variant

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1    ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Your gmail address"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "gmail pw"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
End With

strbody = "Hi there" & vbNewLine & vbNewLine & _
          "This is line 1" & vbNewLine & _
          "This is line 2" & vbNewLine & _
          "This is line 3" & vbNewLine & _
          "This is line 4"

With iMsg
    Set .Configuration = iConf
    .To = ""
    .CC = ""
    .BCC = ""
    ' Note: The reply address is not working if you use this Gmail example
    ' It will use your Gmail address automatic. But you can add this line
    ' to change the reply address  .ReplyTo = "Reply@something.nl"
    .From = """FROM??"" <Reply@something.nl>"
    .Subject = "Important message"
    .TextBody = strbody
    .Send
End With

End Sub