Outlook VBA电子邮件附件已过时

Outlook VBA电子邮件附件已过时,vba,outlook,outlook-2010,Vba,Outlook,Outlook 2010,我有一个MS Outlook VBA脚本,用于侦听收到的带有特定主题行的电子邮件。收到后,它使用Shell函数运行Python脚本。Python脚本将主题行字符串的一部分作为参数,并将其工作结果保存到一个.html文件中,该文件最终被提取并附加到VBA层的回复消息中 目前,这种联系一直是陈旧的。我的意思是,最终输出消息中的任何内容都不是来自最近的查询,而是来自之前的查询。这就好像VBA不允许目录在附加my.html文件之前自行更新一样。我目前正在使写电子邮件的函数在附加任何文件之前等待50秒(比

我有一个MS Outlook VBA脚本,用于侦听收到的带有特定主题行的电子邮件。收到后,它使用
Shell
函数运行Python脚本。Python脚本将主题行字符串的一部分作为参数,并将其工作结果保存到一个.html文件中,该文件最终被提取并附加到VBA层的回复消息中

目前,这种联系一直是陈旧的。我的意思是,最终输出消息中的任何内容都不是来自最近的查询,而是来自之前的查询。这就好像VBA不允许目录在附加my.html文件之前自行更新一样。我目前正在使写电子邮件的函数在附加任何文件之前等待50秒(比我的Python脚本运行时间长10倍),因此这不是时间问题

想法?下面是VBA代码

这将设置我的代码,以便在收到任何电子邮件时进行侦听和操作

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private WithEvents myOlItems  As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
      Set olApp = Outlook.Application
      Set objNS = olApp.GetNamespace("MAPI")
      Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
这将检查电子邮件是否有特定的主题行,并在满足条件时通过
Shell
命令运行Python脚本

Private Sub myOlItems_ItemAdd(ByVal Item As Object)

Debug.Print "received"

On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem

  If TypeName(Item) = "MailItem" Then

    Set Msg = Item
    Dim cnt As Integer
    cnt = InStr(Msg.Subject, "**2step")

    If cnt > 0 Then

        Dim WrdArr() As String
        WrdArr() = Split(Msg.Subject)
        Debug.Print WrdArr(1)
        Debug.Print WrdArr(2)


        Shell ("python C:\Users\fileloc\pythonscript.py " & Chr(34) & WrdArr(1) & " " & WrdArr(2) & Chr(34))

        createEmail Msg.sender, WrdArr(1) & " " & WrdArr(2)

        Debug.Print "Woo made an email"

    Else
        GoTo ProgramExit
    End If

  End If

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub
这将在Python脚本创建文件后写入电子邮件

Sub createEmail(sender As String, subj As String)

Debug.Print "OK"
Sleep 50000

' images in stationery need to include full path
strFile = "C:\Users\fileloc\output.html"


' You need to set a reference to the scripting object
 Dim objMail As Outlook.MailItem
 Dim fso As Scripting.FileSystemObject
 Dim tsTextIn As Scripting.TextStream
 Dim strTextIn As String

 Set fso = New Scripting.FileSystemObject
 'read html
 Set tsTextIn = fso.OpenTextFile(strFile)
 strTextIn = tsTextIn.ReadAll

 'Create e-mail item
 Set objMail = Application.CreateItem(olMailItem)

 With objMail
 .To = sender
 .BodyFormat = olFormatHTML
' use .body when inserting .txt file
 .HTMLBody = strTextIn
 .Subject = subj
 .Display
 End With


End Sub

我没有包括Python脚本。我相信这不是问题所在。它创建/覆盖的输出文件是正确的,该文件不会附加到最后一封电子邮件。

我记得Excel VBA也发生过这种情况。我需要让VBA“放手”或暂停什么的?这有什么意义吗?使用“WaitForSingleObject”来运行Python脚本,而不是“Shell”。也许您在Python脚本完成之前调用createEmail。