At the recent JISC CETIS conference, Lawrie Phipps and I discussed automatic tweeting from Powerpoint. Lawrie found the rather wonderful SAP Free PowerPoint Tools by Tim Elliot, but I was already interested in writing one of my own.
I always forget how different VBA can be to VB.net, and the voyage of experience I have each time I have to find things in the object model of the MS Office products, but as usual it was an enlightening journey. To be honest, this isn't really much to do with me - all the code is, essentially, from other people.
The Twittering code is from an example on how to twitter from Excel over at chandoo.org:
Public Sub tweetThis(username As String, password As String, message As String) 'from http://chandoo.org/wp/2009/02/05/twitter-from-excel/ Dim xml, tResult Set xml = CreateObject("MSXML2.XMLHTTP") xml.Open "POST", "http://twitter.com/statuses/update.xml?status=" & message, False, username,password xml.SetRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1" xml.Send tResult = xml.ResponseText 'you can view Twitter’s response in debug window Debug.Print tResult Set xml = Nothing End Sub
Function GetNotesText(ByVal Sld As Slide) As String 'from http://officeone.mvps.org/vba/get_notes_text.html Dim Shp As Shape Dim S As String On Error Resume Next S = "" With Sld.NotesPage For Each Shp In .Shapes If Shp.Type = msoPlaceholder Then If Shp.PlaceholderFormat.Type = ppPlaceholderBody Then S = Shp.TextFrame.TextRange.Text Exit For End If End If Next End With GetNotesText = S End Function
Dim cPPTObject As New cEventClass Dim TrapFlag As Boolean Sub TrapEvents() If TrapFlag = True Then MsgBox "Relax, my friend, the EventHandler is already active.", vbInformation + vbOKOnly, "PowerPoint Event Handler Example" Exit Sub End If Set cPPTObject.PPTEvent = Application TrapFlag = True End Sub Sub ReleaseTrap() If TrapFlag = True Then Set cPPTObject.PPTEvent = Nothing Set cPPTObject = Nothing TrapFlag = False End If End Sub
Private Sub PPTEvent_SlideShowNextSlide(ByVal Wn As SlideShowWindow)Dim notes As StringDim start As IntegerDim fini As IntegerDim message As Stringnotes = GetNotesText(Wn.View.Slide)If InStr(notes, "[twitter]") Thenstart = InStr(notes, "[twitter]") + 9If InStr(notes, "[/twitter]") Thenfini = InStr(notes, "[/twitter]")Elsefini = Len(notes)End Ifmessage = Mid$(notes, start, fini - start)tweetThis "PatParslow", "mysecretpassword", messageEnd IfEnd Sub
A note such as "This is testing the [twitter]OK so it looks like Twitter is blocking multiple tweets with the same text, which is fair enough[/twitter] functionality" will tweet the bit between [twitter] and [/twitter].
It isn't clever code, and can currently only manage a single tweet in each notes section for a slide. More importantly, it represents extremely bad practice in
but it was more about proof of concept, and I learned a fair bit while doing it, so it was time well spent.
This work is licensed under a Attribution Non-commercial Creative Commons license