Tell the Twitterverse about your Powerpoint presentation (hmm!)

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  
The code for finding the speaker notes (and I would never have guessed how to do this!) was from Office.mpvs.org:
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
 
And I found out how to add things to the event model thanks to a post from www.pptfaq.com (though I had to fiddle about to try and work out how to get the right event handler to work):
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

 

All of which allowed me to do this:
Private Sub PPTEvent_SlideShowNextSlide(ByVal Wn As SlideShowWindow)
 Dim notes As String
 Dim start As Integer
 Dim fini As Integer
 Dim message As String
 notes = GetNotesText(Wn.View.Slide)
 If InStr(notes, "[twitter]") Then
   start = InStr(notes, "[twitter]") + 9
   If InStr(notes, "[/twitter]") Then
     fini = InStr(notes, "[/twitter]")
     Else
     fini = Len(notes)
   End If
   message = Mid$(notes, start, fini - start)
   tweetThis "PatParslow", "mysecretpassword", message
 End If
End Sub
 
The only problem is, while this worked and I irritated people with test messages, it seemed to have stopped working, and I took a while to figure out why.  Multiple messages with the same content seem to trigger a cool masking feature on Twitter (haven't heard anything about it before) which meant that I was not seeing my tweets appear.  Having changed the text in the notes for my slides, I now get to see them appear in Twitter again.
 

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 

  1. Hard coding username and password
  2. Passing said username and password via basic authentication on a normal http channel

but it was more about proof of concept, and I learned a fair bit while doing it, so it was time well spent.

Trackback URL for this post:

http://brains.parslow.net/trackback/1557