Any sample VBA code to change text/numbers on a slide

S

Steve

Can anyone provide some sample code for altering text and numbers that appear
on a slide so it can be updated to current values? Preferably the values
would be read first from a file.
 
D

David M. Marcovitz

I'm not sure I have exactly what you are looking for, but if you go to my
site and go to "Examples From Real People," you might look at the Flash
Card example (currently the first one). It reads questions from a .csv
file:

http://www.loyola.edu/education/PowerfulPowerPoint/

Under "More Tricks," I have examples that write to a file (tricks #1 and #
2). I thought I had an example that reads from a file, but I guess I didn't
post it, but I have plenty of examples that change the text on slides (go
to "Examples by Chapter" and look for examples 6.6, 6.7, and 6.8. Finally,
the following code reads the first 3 lines from a file (mytestfile.txt) and
puts those lines in shapes 1, 2, and 3, on slide 2:

Sub AdjustQuestion()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs, f
Dim theQuestion As String
Dim answer1 As String
Dim answer2 As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("mytestfile.txt", ForReading, TristateFalse)
theQuestion = f.readline
answer1 = f.readline
answer2 = f.readline
ActivePresentation.Slides(2).Shapes(1).TextFrame.TextRange.Text _
= theQuestion
ActivePresentation.Slides(2).Shapes(2).TextFrame.TextRange.Text _
= answer1
ActivePresentation.Slides(2).Shapes(3).TextFrame.TextRange.Text _
= answer2

f.Close
End Sub

David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
S

Steve Jackson

Thank you Steve and David. Sorry for the late reply.

The trick is finding out what the object's reference name is or being able
to change the name. I might just write code that creates the objects in the
first place, then I'll have total control over them ;->
 
D

David M. Marcovitz

Check out Example 8.7 on my site to get and set names of objects:

http://www.loyola.edu/education/PowerfulPowerPoint/

Of course, you are right; if you create the objects with VBA, you have
control of the objects and their names.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/

"=?Utf-8?B?U3RldmUgSmFja3Nvbg==?=" <Steve
(e-mail address removed)> wrote in
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top