使用Gmail

时间:2015-11-09 10:27:36

标签: excel excel-vba email gmail vba

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

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

有没有人知道要执行此操作的任何代码?

我是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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

简单的Sendkeys解决方案。您必须登录gmail

Sub ActivateGmail()
    MessageText = Range("Email!StockOptionAnalysis")
    Handle = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    RetVal = Shell(Handle, 1) '      Open
    Application.Wait Now + TimeValue("00:00:03")

    SendKeys ("https://mail.google.com/mail/u/0/#inbox?compose=new"), True
    SendKeys ("{ENTER}"), True
    Application.Wait Now + TimeValue("00:00:03")

    SendKeys ("""boss"" <wife@gmail.com>"), True
    SendKeys ("{ENTER}"), True
    Application.Wait Now + TimeValue("00:00:03")
    SendKeys ("{TAB}"), True

    SendKeys ("Optigon"), True
    Application.Wait Now + TimeValue("00:00:03")
    SendKeys ("{TAB}"), True

    SendKeys (MessageText), True
    Application.Wait Now + TimeValue("00:00:03")

    SendKeys ("{TAB},{ENTER}"), True
    Application.Wait Now + TimeValue("00:00:03")
    SendKeys ("%{F4}"), True
'       Adjust the wait time as needed
End Sub