VBA Outlook附件发布到URL

时间:2018-06-18 00:55:33

标签: vba outlook outlook-vba email-attachments

我为outlook创建了一个宏,它打开了一个用户窗体,询问了几个问题,然后有一个提交按钮将消息和用户表单数据发送到POST网址(我们的售票系统)。我被困在附件的最后一部分。我可以看到附件列表并循环遍历附件项目对象,但我不知道要将实际文件数据发送到POST表单的哪个对象调用...我只能看到文件大小,文件名等

我会获取文件内容和mime类型的任何想法,以便将它们作为表单var添加到URL post动作中吗?

Public senderaddress As String
Public thisEmailContent As String
Public thisEmailTEXT As String
Public thisStaffMember As Integer
Public emailAttachments As String

Sub MacroName()
    thisStaffMember = 1
    Set objItem = GetCurrentItem()
    senderaddress = objItem.SenderEmailAddress
    thisEmailContent = objItem.HTMLBody
    thisEmailTEXT = objItem.Body
    emailAttachments = objItem.Attachments
    UserformName.Show
End Sub

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
        Set GetCurrentItem = _
        objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
        Set GetCurrentItem = _
        objApp.ActiveInspector.CurrentItem
    Case Else
    End Select
End Function

然后,这是Userform代码的最后一部分,当点击立即发送按钮时,它实际上将数据发送到我们的票务系统...

Public Function encodeURL(str As String)
    Dim ScriptEngine As Object
    Dim encoded As String

    Set ScriptEngine = CreateObject("scriptcontrol")
    ScriptEngine.Language = "JScript"

    encoded = ScriptEngine.Run("encodeURIComponent", str)

    encodeURL = encoded
End Function

Private Sub SendNow_Click()
    Set sendReq = CreateObject("MSXML2.XMLHTTP")
    With sendReq
        .Open "POST", "https://ticketsystemurl.whatever/outlookinterface.php?action=openticket&fromEmail=" & encodeURL(senderaddress) & "&selectedCustomer=" & encodeURL(CustomerSelect.Value) & "&selectedContact=" & encodeURL(ContactSelect.Value), False
        .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .Send ("notifyCustomer=" & NotifyCustomer.Value & "&staffID=" & thisStaffMember & "&emailContent=" & encodeURL(thisEmailContent) & "&emailTEXT=" & encodeURL(thisEmailTEXT) & "&appendTicket=" & AppendToTicket.Value & "&createNewTicket=" & CreateNewTicket.Value & "&selectedTicket=" & encodeURL(SelectTicket.Value) & "&attachments=" & encodeURL(emailAttachments))
    End With

    Unload Me
End Sub

到目前为止提交的所有内容除了我无法弄清楚如何用实际的附件filedata填充post var“attachments”。这甚至可能吗?

1 个答案:

答案 0 :(得分:0)

Outlook不会为你做那件事。您需要将附件保存为文件(Attachment.SaveAsFile),读取该文件的内容(Scripting.FileSystemObject对象?)以在您的Web请求中使用它。