excel - 使用 Gmail 从 Excel 发送电子邮件

标签 excel email gmail vba

我试图在保存工作簿后自动生成电子邮件。我不想在电子邮件中发送工作簿,只是向人员列表发送电子邮件通知,说它有一个新条目,因此他们实际上必须打开它并回复(如果我可以放置指向位置的链接可以工作的电子表格)。此外,工作簿是“共享的”,因此多人可以一次编辑它,所以我认为如果从电子邮件中下载它,它不会保持“共享”状态并继续更新。大约 25 人可以访问此电子表格,任何人都可以输入/编辑条目。最终,我希望它仅在特定列中输入/编辑数据然后保存时才发送电子邮件。

我的代理机构使用 Gmail,但我们的电子邮件地址没有 @gmail.com在他们中。相反,我们使用我们的 .gov以某种方式通过 gmail 的电子邮件地址。我不确定这是否相关,但我想我会提到它。我搜索了几个在线论坛,但似乎找不到任何东西。

有谁知道任何代码可以做到这一点?

我是 VBA 的新手,我得到了电子邮件部分的工作,但我希望它在保存工作簿时通过电子邮件发送。这是我目前使用的代码:

Sub CDO_Mail_Small_Text()
    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/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@xxx.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxxxx"
        .Update    'Let CDO know we have change the default configuration for this message
    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 = "xxx@xxx.com"
        .CC = ""
        .BCC = ""
        .From = """name"" <xxx@xxx.com>"
        .Subject = "test"
        .TextBody = strbody
        .Send
    End With

End Sub

我收到此错误

enter image description here

最佳答案

好吧,我发现如果我将变量放在 subs 之外,然后在第一个 sub 中调用第二个 sub,并为 .fields 配置添加 .update(感谢 Tim!),它可以工作:

Dim iMsg As Object
Dim iConf As Object
Dim strbody As String

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Call CDO_Mail_Small_Text
End Sub

Private Sub CDO_Mail_Small_Text()

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/sendusing") = 2
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
  .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@xxx.com"
  .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxxxx"
  .Update 'Let CDO know we have changed the default configuration for this message
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 = "xxx@xxx.com"
    .CC = ""
    .BCC = ""
    .From = """name"" <xxx@xxx.com>"
    .Subject = "test"
    .TextBody = strbody
    .Send
End With

End Sub

关于excel - 使用 Gmail 从 Excel 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717950/

相关文章:

php - 再次显示表而不重新处理所有 PHP

gmail - 使用 Google Apps 脚本的 Gmail 短信通知 : how to display content of the email?

javascript - 以编程方式激活电子邮件旧的和更新的电子邮件按钮

python - Gmail 显示电子邮件的 HTML 和文本以及 HTML 部分

excel - 从多个 CSV 文件构建 Excel 电子表格

vba - 更改 Excel ActiveX 列表框的名称

php - 拉维尔 5 : Error in Sending Email

Excel VBA循环遍历包含第一个字母M的所有工作表

excel - VBA Dir 在名为 ".."或 Documents.xlsx 的目录中提取 Ghost 文件

asp.net-mvc-3 - SmtpClient.SendAsync 阻塞了我的 ASP.NET MVC 请求