metaWeblog.newPost posting to Wordpress from Word
juust | 26/10/2009Quick and dirty : posting to Wordpress from Word with VBA. I put the text source on the server.
It works, to a point. You can take a Word document, run the PostToWordpress macro (with your own blogs settings) and it puts the content in Wordpress.
I haven’t worked with Word for a few years, you’d have to ask someone with up-to-date Word vba skills how to grab the document title, and sanitize and format the document text into proper html-output. For the tests I grabbed a snippet off the net, it does not do a perfect job but the basic idea is kosher.
metaWeblog.newPost
Most sites (including blogger) have skipped from blogger.newPost to newer formats. I prefer using the metaWeblog.newPost rpc method, it is a bit more versatile, and supported by Wordpress as well.
(ref. msdn library article on metaWeblog.newPost)
The methodcall is structured like this :
- methodname : metaWeblog.newPost
- blogid
- blog user name
- blog password
- the post struct with it’s members
- categories array
- description (the doc text)
- title
- date created
- publish (0=save as draft, 1=publish immediately)
So let’s build that step by step as vba routine :
-
-
Sub PostToWordpress()
-
-
'the basic blog settings
-
txtURL = "http://www.blog.com/xmlrpc.php"
-
txtBlogId = "1"
-
txtUserName = "MyUserName"
-
txtPassWord = "MyPassword"
-
-
'the document settings
-
txtTitle = "MyTitle"
-
'note : you can use the documents creation date here
-
txtDateCreated = Format(Now(), "yyyyMMdd") & "T" & Format(Now(), "hh:mm:ss")
-
-
'Categories is an array in the post,
-
'you can use category names,
-
'and you can always use "Uncategorized"
-
'as all wordpress blogs have it
-
-
Dim MyCategories(2) As String
-
MyCategories(1) = "Uncategorized"
-
MyCategories(2) = "example_category_one"
-
-
'note: this function grabs the document text,
-
'replaces linebreaks with html linebreaks etc.
-
-
'it needs a lot more work
-
txtDocument = getCurrentDocAsSimpleHtml()
Once I have the blog settings and the content + attributes for the new Wordpress post, I set up the XMLHttpRequest object :
-
-
Dim objSvrHTTP As ServerXMLHTTP
-
Dim strT As String
-
Set objSvrHTTP = New ServerXMLHTTP
-
-
objSvrHTTP.Open "POST", txtURL, False, CStr(txtUserName), _
-
CStr(txtPassWord)
-
-
objSvrHTTP.setRequestHeader "Accept", "application/xml"
-
objSvrHTTP.setRequestHeader "Content-Type", "application/xml"
Then I start building the XML to send to Wordpress,
-
-
'methodcall
-
strT = strT & "<methodcall>"
-
-
'methodname
-
strT = strT & "<methodname>metaWeblog.newPost</methodname>"
-
-
'parameters : blog settings
-
strT = strT & "<params>"
-
strT = strT & "<param><value><string>" & txtBlogId & "</string></value></param>"
-
strT = strT & "<param><value><string>" & txtUserName & "</string></value></param>"
-
strT = strT & "<param><value><string>" & txtPassWord & "</string></value></param>"
-
-
'parameters : the post structure
-
strT = strT & "<param>"
-
strT = strT & "<struct>"
-
-
'parameters : post : category array
-
strT = strT & "<member><name>categories</name>"
-
strT = strT & "<value>"
-
strT = strT & "<array>"
-
strT = strT & "<data>"
-
-
For i = 1 To UBound(MyCategories)
-
strT = strT & "<value>" & MyCategories(i) & "</value>"
-
Next i
-
strT = strT & "</data>"
-
strT = strT & "</array>"
-
strT = strT & "</value>"
-
strT = strT & "</member>"
-
-
'parameters : post : the content
-
strT = strT & "<member><name>description</name>"
-
strT = strT & "<value>< [!CDATA[" & txtDocument & "]]></value>"
-
strT = strT & "</member>"
-
-
'parameters : post : title
-
strT = strT & "<member><name>title</name><value>" & txtTitle & "</value></member>"
-
-
'parameters : post : date created
-
strT = strT & "<member><name>dateCreated</name><value><datetime .iso8601>" & txtDateCreated & "</datetime></value></member>"
-
strT = strT & "</struct>"
-
strT = strT & "</param>"
-
-
'parameters : post : store as draft (0) or publish immediately (1)
-
strT = strT & "<param><value><boolean>0</boolean></value></param>"
-
-
'end parameters
-
strT = strT & "</params>"
-
-
'end methodcall
-
strT = strT & "</methodcall>"
-
-
'send it to wordpress
-
objSvrHTTP.send strT
-
-
'send the response to the debug window
-
Debug.Print objSvrHTTP.responseText
-
-
End Sub
conversion to html
The main practical problem is converting the Word document content to html. I grabbed two quick functions to select the current document text, convert the most common html entities and replace linebreaks with their html counterparts, but it is far from perfect.
-
Private Function getCurrentDocAsSimpleHtml() As String
-
'… see source text
-
End Function
-
-
' simple HTML entity encoder
-
Private Function encode(ByVal s As String) As String
-
If s = "&" Then
-
encode = "&"
-
ElseIf s = "< " Then
-
encode = "<"
-
ElseIf s = ">" Then
-
encode = ">"
-
ElseIf s = Chr(13) Then
-
encode = "<br />"
-
Else
-
encode = s
-
End If
-
End Function
The Wordpress Incutio xml-parser will not pass content it cannot make sense of, which leaves you with an empty post.
using CDATA
A rather common hack I used above is using < ![CDATA[ ]]> to wrap the post content, which indicates to the wordpress xml-parser that I pass a string of raw character data as content. The parser ignores it, and Wordpress stuffs everything in the database.
Works great, but it can backfire and slip stuff in the database that wordpress cannot handle and display correctly.
I’d test without using CDATA if you want to post from Word on a regular basis, or use a more professional html-converter on the word documents first.










