【问题标题】:Can't set up mail with VBA when changed to Windows 10更改为 Windows 10 时无法使用 VBA 设置邮件
【发布时间】:2015-10-07 19:41:45
【问题描述】:

长期以来,我一直使用宏通过邮件发送 Excel 工作表,但是当我更新到 Windows 10 时,一切都失败了,我不明白为什么。这是代码:

TempFilePath = Environ$("temp") & "\"
TempFileName = Range("B2") & " " & Format(Now, "dd-mmm-yy")

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, _
            FileFormat:=FileFormatNum
    On Error Resume Next
   ' Change the mail address and subject in the macro before
   ' running the procedure.
    With ActiveSheet
        Set rngTo = .Range("B1")
        Set rngCc = .Range("B3")
        Set rngSubject = .Range("B2")
        Set rngBody = .Range("B2")
    End With

    With OutMail
        .To = rngTo
        .CC = rngCc
        .BCC = ""
        .Subject = rngSubject
        .Body = "Adjunto para tu revisión y comentarios"
        .Attachments.Add Destwb.FullName
        ' You can add other files by uncommenting the following statement.
        '.Attachments.Add ("C:\test.txt")
        ' In place of the following statement, you can use ".Display" to
        ' display the mail.
        .Send
    End With
    On Error GoTo 0
    .Close SaveChanges:=False
End With

' Delete the file after sending.
Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
    .ScreenUpdating = True
    .EnableEvents = True

问题出在.to, .cc, .subject 部分。

如果我调试代码我可以看到变量被填充,例如:

但是当它发送或显示字段时,它们是空的。另外,如果手动填写.to="a@a.com" 也可以。

你知道它是什么吗?

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    To/CC/BCC 属性是收件人姓名。保存项目时会更新这些属性。您是否在 Outlook 的“已发送邮件”文件夹中看到空的收件人/抄送?

    您还可以为每个收件人使用OutMail.Recpients.Add

    【讨论】:

      【解决方案2】:

      您可以使用“To”:

      Set _To = OutMail.Recipient.Add(Range("A1"))
      _To.Type = olTo
      

      您可以用于“抄送”:

      Set _To = OutMail.Recipient.Add(Range("A2"))
      _To.Type = olCC
      

      我现在的问题是如何修复其他属性,Subject 和 SentOnBehalfOnName

      【讨论】:

        【解决方案3】:

        托马斯

        我刚从 7 转到 Windows 10。出于某种原因,您必须添加 .Value。

        示例 1。

        With ActiveSheet
            Set rngTo = .Range("B1").Value
            Set rngCc = .Range("B3").Value
            Set rngSubject = .Range("B2").Value
            Set rngBody = .Range("B2").Value
        End With
        With OutMail
            .To = rngTo
            .CC = rngCc
            .BCC = ""
            .Subject = rngSubject
        End With
        

        我写的有点不同。请参见示例 2。

        示例 2

         With OutMail
                .To = Range("AA22").Value
                .CC = Range("AB22").Value
                .Subject = Range("AA21").Value
        End With
        

        【讨论】:

        • 欢迎来到 S.O.,Set rngTo = .Range("B1").Value 是类型不匹配。
        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 2017-10-20
        • 2019-03-10
        • 1970-01-01
        • 2012-08-13
        相关资源
        最近更新 更多