vba - 为什么创建邮件时要在.Body 中添加一行?

标签 vba batch-file outlook

我使用 Outlook 规则在 Windows 中运行批处理文件,该文件将电子邮件的主题和正文(这意味着两个参数)作为输入。

如果我直接运行该行作为 C:\Users\me\Desktop\firstplink.bat "subjectPartA subjectPartB""bodyA bodyB",它不会添加一行。

有两个参数,用引号引起来。我有 A 和 B,因此主题和正文中至少有两个单词。

我的代码添加了一行,这弄乱了批处理文件。

Sub firstplink(item As Outlook.MailItem)
    strSubject = item.Subject
    MsgBox strSubject
    strBody = item.Body
    MsgBox strBody
    strProgramName = "C:\Users\me\Desktop\firstplink.bat"
    strAll = strSubject & " " & strBody
    all = """" & strProgramName & """ """ & strSubject & """ """ & strBody & """"
    MsgBox all
    Call Shell("""" & strProgramName & """ """ & strSubject & """ """ & strBody & """")
End Sub

Sub TestScript()
    Dim testMail As MailItem
    Set testMail = Application.CreateItem(olMailItem)
    testMail.Subject = "Test subject"
    testMail.Body = "bcTest bbody bb cxcvaa"
    Project1.Module1.firstplink testMail
End Sub

最佳答案

看起来 Outlook.MailItem 上的 VBA 开销会将 vbCRLF 附加到 Item.Body(如果它没有)。由这段代码证明

Option Explicit

Sub firstplink(item As Outlook.MailItem)
    Dim strSubject As String, strBody As String, strAll As String, strProgramName As String
    Dim all As String
    strSubject = item.Subject
    MsgBox strSubject
    Debug.Print Asc(Right(item.Body, 1))
    Debug.Print Asc(Right(item.Body, 2))
    strBody = item.Body
    MsgBox strBody
    strProgramName = "C:\Users\me\Desktop\firstplink.bat"
    strAll = strSubject & " " & strBody
    all = """" & strProgramName & """ """ & strSubject & """ """ & strBody & """"
    MsgBox all
    Call Shell("""" & strProgramName & """ """ & strSubject & """ """ & strBody & """")
End Sub

Sub TestScript()
    Dim testMail As MailItem
    Set testMail = Application.CreateItem(olMailItem)
    testMail.Subject = "Test subject"
    testMail.Body = "bcTest bbody bb cxcvaa"
    Project1.Module1.firstplink testMail
End Sub

立即窗口在两个 Debug.Print 上报告 10 然后是 13。如果手动添加 vbCrLF(例如 testMail.Body = "bcTest bbody bb cxcvaa"& vbCrLf),则第二个 vbCrLf未添加。

这肯定与预期终止 item.body 的 vbCrLf 有关,如果它不存在,则开销会加一以避免崩溃。

关于vba - 为什么创建邮件时要在.Body 中添加一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071874/

相关文章:

excel - 在组合框/列表框中启用鼠标滚轮滚动

c++ - C++ ATL OleAutomation 错误 424

sql - SQLCMD, 'tee'无法识别为内部或外部命令

java - 如何对可用性检查进行编程并在 Outlook 中创建新 session ?

email - Outlook iCal session 邀请说明问题

展望 API : Receiving 'ErrorIrresolvableConflict' when trying to send Draft

excel - 为什么这只生成一对解决方案

Excel VBA - InputBox 和 Autofilter UK 日期格式问题

batch-file - ffmpeg 在尝试处理之前检查文件是否存在

powershell - 如何制作包含今天日期的文件夹,然后将文件复制到其中?