当前位置:  开发笔记 > 编程语言 > 正文

使用Gmail从Excel发送电子邮件

如何解决《使用Gmail从Excel发送电子邮件》经验,为你挑选了1个好方法。

保存工作簿后,我正在尝试自动生成电子邮件。我不想通过电子邮件发送工作簿,而只是通过电子邮件通知某人,说该工作簿有一个新条目,因此他们实际上必须打开它并做出响应(如果我可以在其中放置链接)可以正常工作的电子表格)。此外,工作簿是“共享的”,因此多个人可以一次对其进行编辑,因此,我认为它不会保留为“共享的”,并且如果从电子邮件中下载,则不会继续更新。大约有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"" "
        .Subject = "test"
        .TextBody = strbody
        .Send
    End With

End Sub

我得到这个错误



1> niklas24..:

好吧,我弄清楚了,如果我将变量放在子对象之外,然后在第一个子对象中调用第二个子对象,然后为.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"" "
    .Subject = "test"
    .TextBody = strbody
    .Send
End With

End Sub

推荐阅读
手机用户2402851155
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有