Solved

Inserting Dynamic Text into Powerpoint

Posted on 2016-11-01
16
77 Views
Last Modified: 2016-11-03
Hi,
I've setup a fairly simple Powerpoint slideshow to use as digital signage in several retail outlets. There are several pieces of text which need to vary by location so I'm looking for an easy way to link this rather than have a different version of the presentation for each outlet.
I'm fully conversant in VBA but not Powerpoint particularly so does anyone have an example of how I might read a small text/csv file at the point of loading the presentation and update several text boxes please?
Any other ideas would be most welcome!
Thanks.
0
Comment
Question by:nigelr99
  • 8
  • 8
16 Comments
 
LVL 9

Accepted Solution

by:
Jamie Garroch earned 500 total points
ID: 41868242
This should get you going:

Option Explicit

' ===============================================================================
' PowerPoint VBA Macro by YOUpresent.co.uk
' Author:   Jamie Garroch 01NOV2016
' Purpose:  Loads text from a CSV text file and replaces the text in the
'           referenced shapes.
' CSV format:
'   [slide index],[shape name],[text]
' Example:
'   1,Rectangle 1,This is some new text
'   2,Rectangle 3,This is some other new text
' Note:     shape names may not be uniquie so shape ID's are better or use
'           the shape's .Tag property to manage shapes as this property can't be
'           accessed via the UI.
' ===============================================================================
Sub LoadTextFromFile()
  ' Source text file for the new text
  Const TextFile = "C:\Temp\textboxes.txt"
  Dim FileLine As String
  Dim LineItems As Variant
  Open TextFile For Input As #1
  Do Until EOF(1)
    Line Input #1, FileLine
    LineItems = Split(FileLine, ",")
    ' If the line has the required 3 component parts, process it
    If UBound(LineItems) - LBound(LineItems) = 2 Then
      With ActivePresentation
        On Error GoTo errorhandler ' In case the slide or shape doesn't exist
        .Slides(CInt(LineItems(0))).Shapes(LineItems(1)).TextFrame.TextRange.Text = LineItems(2)
        On Error GoTo 0
      End With
    End If
  Loop
  Close #1
Exit Sub
errorhandler:
  Debug.Print "Couldn't find " & LineItems(1) & " on slide " & LineItems(0)
  Err.Clear
  Resume Next
End Sub

Open in new window


To automatically run code when opening a presentation, you'll need to use application events as per this article I wrote:

https://www.experts-exchange.com/articles/17410/Initializing-PowerPoint-Events-with-VBA-and-Ribbon-XML.html
0
 

Author Comment

by:nigelr99
ID: 41868274
Perfect.. will trial shortly..
Many thanks
0
 

Author Comment

by:nigelr99
ID: 41868519
Works perfectly and saved me lots of time so much appreciated!
Cheers !!
0
 

Author Comment

by:nigelr99
ID: 41869729
Just one query, everything working brilliantly except when I save as macro-enabled-show to distribute - is there something I'm missing (using App_SlideShowBegin event) ?
0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41869769
Ahh! I suspect that if you're saving as a .ppsm that the ribbon UI won't get loaded so the initiation of the application event handler won't get called as the ribbon's onLoad event won't fire. If this is the case, you could use the legacy pseudo-event instead. I wrote another article here on the pros and cons of psuedo events in PowerPoint:

http://youpresent.co.uk/powerpoint-application-events-in-vba/

I guess you are distributing to automate the start of the slide show? If that is the case, another possible solution would be to save as a .pptm to ensure the ribbon event fires and then within your initialisation code, automatically start the slide show with:

ActivePresentation.SlideShowSettings.Run

Open in new window


That would force the slide show to start automatically when opening the .pptm file.
0
 

Author Comment

by:nigelr99
ID: 41869958
Thanks, I've tried the following but slideshow doesn't start? Sorry, not familiar with Powerpoint events etc.

Public WithEvents App As Application

Private Sub App_PresentationOpen(ByVal Pres As Presentation)
    ActivePresentation.SlideShowSettings.Run
End Sub

Private Sub App_SlideShowBegin(ByVal Wn As SlideShowWindow)
    Populate_Text_From_File
End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41869991
I just spotted that both tables in that article state column 1 as "Psuedo Event". The second table should be "Application Event". Because you cannot use the ribbon to initialise the application events in a .ppsm file, what I was suggesting was to use one of the 4 [very limited] legacy psuedo-events that do not require initialisation. They are:

OnSlideShowPageChange
OnSlideShowTerminate
OnSlideShowNextBuild
OnSlideShowPreviousBuild

I would use the first one to trigger your code and set a flag to prevent it retriggering like so:

Option Explicit

Public bInitApp As Boolean

Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
  Debug.Print "Slide Show Page Change"
  If Not bInitApp Then
    ' Do your text box stuff here
    Debug.Print "Stuff done"
  End If
  bInitApp = True
End Sub

Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow)
  Debug.Print "Slide Show Terminated"
  bInitApp = False
End Sub

Open in new window

0
 

Author Comment

by:nigelr99
ID: 41870057
Great thanks - can you just clarify, are we now talking about a pptm or ppsm file. I assumed the ppsm should start the slide show automatically by it's nature so the OnSlideShowPageChange should fire at the start? It doesn't appear to though.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41870134
Yes, I suggested the pseudo events as you wanted to have a .ppsm file. It could be that your macros are being blocked by the trust centre. I always digitally sign mine so I have this set in Options / Trust Center / Trust Center Settings / Macro Settings and with a Authenticode root CA signed .ppsm it works:

PowerPoint Trust Centre Settings for Macros
But you probably need the last/fourth (least secure) option. You can leave the second Developer Macro Settings option unticked as that's only needed when you use VBA to manipulate the VBA project code programmatically.
0
 

Author Comment

by:nigelr99
ID: 41870266
Thanks again - already had the trust centre covered. It seems I might be on a wild goose chase is any case as just read the the Powerpoint viewer does not support any code of any sort which leaves me high and dry with the whole idea - is this correct? (I'm using the viewer on Intel stick pcs at each location)
0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41870331
Oh no. That is indeed a dead end! As per the last sentence in the Details section of this Microsoft PowerPoint Viewer download article, macros are still not supported by this free application.

Thinking out loud, I'm wondering if there is a solution using VBScript. I'm no expert but I'm pretty sure you could develop a script that runs under Windows Script Host to populate your text boxes in a target pptx file.
0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41870382
Out of curiosity, I'm just prototyping a VBScript - give me a few minutes.
0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41870400
Ta da. Copy this to a .vbs file, change the constants for the text file and presentation respectively and then run the VBS. I had to strip out error trapping to save time so it's not yet fully robust but I tested it and it works. You can then distribute a .pptx file (no VBA code) and the VBS/TXT files to modify it before entering slide show mode.

' VBScript file to swap text in a PowerPoint file with that specified in a separate text file.
' Author : Jamie Garroch of YOUpresent Ltd. (http:\\youpresent.co.uk)

' Set the location and file name of the CSV-formatted text file 
Const TextFile = "C:\temp\textboxes.txt"
' Set the location and file name of the PowerPoint slide show file 
Const PresFile = "C:\temp\ReplaceText.pptx"

Dim oPPT 	' Object : PowerPoint Application
Dim oPres ' Object : Presentation
Dim oSld 	' Object : Slide
Dim oShp 	' Object : Shape
Dim oFSO 	' Object : File System
Dim oFile ' Object : Text File Stream
Dim FileLine
Dim LineItems

Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FileExists(TextFile) Then
	MsgBox "Cannot find the input text file:" & vbCrLf & vbCrLf & TextFile,vbOKOnly+vbCritical, "Text File Missing"
	WScript.Quit
End If

If Not oFSO.FileExists(PresFile) Then
	MsgBox "Cannot find the input PowerPoint file:" & vbCrLf & vbCrLf & PresFile,vbOKOnly+vbCritical, "PowerPoint File Missing"
	WScript.Quit
End If

Set oPPT = CreateObject("PowerPoint.Application")
oPPT.Presentations.Open PresFile
Set oPres = oPPT.ActivePresentation

' Swap the text
Set oFile = oFSO.OpenTextFile(TextFile)
Do Until oFile.AtEndOfStream
  FileLine = oFile.ReadLine
  LineItems = Split(FileLine, ",")
  ' If the line has the required 3 component parts, process it
  If UBound(LineItems) - LBound(LineItems) = 2 Then
    With oPres
      .Slides(CInt(LineItems(0))).Shapes(LineItems(1)).TextFrame.TextRange.Text = LineItems(2)
    End With
  End If
Loop
  
oPres.SlideShowSettings.Run

Open in new window

0
 

Author Comment

by:nigelr99
ID: 41871909
You're really going the extra mile here and I appreciate it immensely! This works perfect until I try it on a pc with only the viewer installed and then script fails when trying to create "powerpoint.application" object. I don't suppose there's some sort of powerpoint-viewer object you're aware of instead?
0
 
LVL 9

Expert Comment

by:Jamie Garroch
ID: 41871958
Curiosity drives me on :-)

Damn I forgot about that!

I had a quick search and found a few articles on running a presentation from a script using the viewer (EE example and more complex example) but of course we're asking PowerPoint to EDIT the file and the Viewer is a read only thin client, so this approach isn't going to work either so we are at dead end number 2 and I think the final one.
0
 

Author Comment

by:nigelr99
ID: 41872267
I think you're right but I can't fault your effort! In any case I now have a semi-automated way of creating a range of ppt files based on text file content which will still save me 80% of the time. Otherwise, it looks like a losing battle when using the Viewer and it's (understandable) limitations. Thankfully the presentations don't change that often :-)
Many, many thanks again.
1

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever been sent a PowerPoint presentation file and wondered why it filled your mailbox? Or have you ever sent a PowerPoint presentation by email and received complaints about the size? Or have you ever created a PowerPoint presentation and t…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
This video teaches viewers how to add transitions to their Slideshows and how to set up timing for the transitions.
The viewer will learn how to edit text. This includes Font, Spacing, Resizing, Color, and other special text options.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now