如何使用Excel VBA发布带有IBM Connections附件的论坛主题

时间:2015-07-17 06:28:24

标签: excel vba excel-vba ibm-connections

我正在尝试使用Excel VBA将附带图像文件的论坛主题发布到IBM Connections 5.0。 根据{{​​3}},此处将需要多部分请求。

我已经管理的是发布没有附件的论坛主题,并将文本或图像文件附加到现有的Wiki页面。因此,我假设问题与这些方面无关,而是与多部分请求的正确格式相关。这里的API描述不是很清楚,我在其他帮助论坛中尝试了几个关于多部分请求的内容。但我得到的只是一个回应" 400错误请求"。

也许你们中的一些专家可以给我一些关于我的代码的提示:

Public Sub CreateForumPost()
    Const sBoundary As String = "2588eb82-2e1c-4aec-9f4f-d65a3ecf8fab"
    Dim oHttp As MSXML2.xmlhttp
    Dim sUrl As String
    Dim sBody As String

    'create XMLHTTP object and URL
    Set oHttp = CreateObject("MSXML2.XMLHTTP")
    sUrl = "https://my-connect-server/forums/atom/topics?forumUuid=9e51cbfb-4b1d-405d-9835-dbd087c49a65"

    'create forum post
    sBody = "--" & sBoundary & vbCrLf
    sBody = sBody & "<?xml version=""1.0"" encoding=""UTF-8""?>"
    sBody = sBody & "<entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:app=""http://www.w3.org/2007/app"" xmlns:snx=""http://www.ibm.com/xmlns/prod/sn"">"
    sBody = sBody & "<category scheme=""http://www.ibm.com/xmlns/prod/sn/type"" term=""forum-topic""/>"
    sBody = sBody & "<title type=""text""> " & "My Title" & " </title>"
    sBody = sBody & "<category term=""question"" scheme=""http://www.ibm.com/xmlns/prod/sn/flags""/>"
    sBody = sBody & "<category term=""" & "my-tag" & """/>"
    sBody = sBody & "<content type=""html""> " & "My post content" & " </content>"
    sBody = sBody & "</entry>" & vbCrLf

    sBody = sBody & "--" & sBoundary & vbCrLf
    sBody = sBody & "Content-Disposition: attachment; filename=""dummy.txt""" & vbCrLf & vbCrLf
    sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf
    sBody = sBody & "--" & sBoundary & "--" & vbCrLf

    Call oHttp.Open("POST", sUrl, False)
    Call oHttp.setRequestHeader("Content-Type", "multipart/related;boundary=" & sBoundary & ";type=""application/atom+xml""")
    Call oHttp.send(pvToByteArray(sBody))

    If oHttp.Status = 201 Then
        Call MsgBox("success")
    Else
        Call MsgBox("error")
        Stop
    End If
End Sub

Private Function sGetFile(sName As String) As String
    Dim abyContent() As Byte
    Dim iNumber As Integer
    Dim lLen As Long

    lLen = FileLen(sName)
    If lLen > 0 Then
        ReDim abyContent(lLen - 1)
        iNumber = FreeFile
        Open sName For Binary Access Read As iNumber
          Get iNumber, , abyContent
        Close iNumber
        sGetFile = StrConv(abyContent, vbUnicode)
    Else
        sGetFile = ""
    End If
End Function

Function pvToByteArray(sText As String) As Byte()
    pvToByteArray = StrConv(sText, vbFromUnicode)
End Function

1 个答案:

答案 0 :(得分:0)

我们发现了问题所在。确实是关于多部分请求的格式化。你需要非常小心CrLf字符...

Public Sub CreateForumPost()
    '...

    'create forum post
    sBody = vbCrLf & "--" & sBoundary & vbCrLf & vbCrLf

    '...
    sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf
    sBody = sBody & "--" & sBoundary & "--"

    '...
End Sub

现在它有效。不过非常感谢您的支持!