Вы находитесь на странице: 1из 13

Stack Overflow

1. Questions
2. Developer Jobs
3. DocumentationBETA
4. Tags
5. Users

1.
2.
Log In Sign Up

Learn, Share, Build


Each month, over 50 million developers come to Stack Overflow to learn, share their
knowledge, and build their careers.

Join the worlds largest developer community.


Sign Up
Writing Excel VBA code/macro to populate Powerpoint Text
boxes with Excel cell values
Ask Question

up I am attempting to take the value in Excel cells and populate PowerPoint text boxes. I
vote0do don't want to link a PowerPoint table to an Excel spreadsheet because the spreadsheet is
wn vote favorite
constantly changing and values are not always in the same rows or the same order.

So I am writing this VBA code to try and populate the text boxes. I've done a lot of
VBA, but never attempted this combination. Below is what I have thus far (more code
will be put in for additional text boxes, but need to get one working first). I realize the
issue has something to do with the object not being properly handled, but not sure how to
correct it.

I'm using Excel and PowerPoint 2007. The bold statement is where I receive the error -
438 object does not support this property or method.

Thanks!

Sub valppt()

Dim PPT As PowerPoint.Application


Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True

PPT.Presentations.Open "C:\Documents\createqchart.pptx"

Range("F2").Activate
slideCtr = 1

Set newslide = ActivePresentation.Slides(slideCtr).Duplicate


Set tb = newslide.Shapes("TextBox1")

slideCtr = slideCtr + 1
' Do Until ActiveCell.Value = ""
Do Until slideCtr > 2
If slideCtr = 2 Then
tb.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
End If
ActiveCell.Offset(0, 1).Activate
slideCtr = slideCtr + 1

If slideCtr = 38 Then
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
ActiveCell.Offset(1, -25).Activate
End If

Loop

End Sub
UPDATE 5/17

While the replication of the slide works, I am still unable to value the textbox. I haven't
been able to come up with the right set statement prior to the statement to have the value
assigned to the textbox. Right now I don't even have a set statement in there right now,
because I haven't been able to get the proper one. Any assistance is appreciated. Below is
the latest code.

Sub shptppt()
'
' shptppt Macro
'

Dim PPT As PowerPoint.Application


Dim pres As PowerPoint.Presentation
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape

Set PPT = CreateObject("PowerPoint.Application")


PPT.Visible = True

Set pres = PPT.Presentations.Open("C:\Documents\createqchart.pptx")

Range("F2").Activate
slideCtr = 1

'Set newslide = ActivePresentation.Slides(slideCtr).Duplicate


' Set tb = newslide.Shapes("TextBox1")

pres.Slides(slideCtr).Copy
pres.Slides.Paste
Set newslide = pres.Slides(pres.Slides.Count)
newslide.MoveTo slideCtr + 1

slideCtr = slideCtr + 1
' Do Until ActiveCell.Value = ""
Do Until slideCtr > 2
If slideCtr = 2 Then
tb.Slides.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
End If
ActiveCell.Offset(0, 1).Activate
slideCtr = slideCtr + 1

If slideCtr = 38 Then
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
ActiveCell.Offset(1, -25).Activate
End If

Loop

End Sub
excel vba excel-vba textbox powerpoint

shareimprove this question edited Jan 19 '16 at 7:38 asked May 16 '13 at 15:03

Kanike Vamshi Krishna Spiderman


13913 26228

1 What is txtReqBase?? It is not declared anywhere in your code, and does not appear to be a valid
object/property/method in PPT. Same error will probably happen with txtReqCurr. David Zemens May 16 '13 at
15:11
add a comment
2 Answers
activeoldestvotes

up txtReqBase is not valid. it's not declared as a variable in your code, and it's certainly not a
vote1d supported property/method in Powerpoint, and that's why you're getting the 438 error.
own To insert text in a shape, you need to identify the shape and then manipulate its .Text. I
vote find it easiest to do this with a shape variable.
'## If you have enabled reference to Powerpoint, then:'
Dim tb As Powerpoint.Shape
'## If you do not enable Powerpoint reference, use this instead'
'Dim tb as Variant '
Set tb = newSlide.Shapes("TextBox1") '## Update this to use the correct name or index of the shapes
collection ##'

tb.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
UPDATE For Mismatch error setting tb.
I'm thinking you're getting the mismatch error because you have PPT As Object rather
than enabling a reference to the Powerpoint Object Library which would allow you to fully
dimension it as a PowerPoint.Application.
Your current code interprets Dim tb as Shape refers to an Excel.Shape, not a
Powerpoint.Shape.
If you enable reference to the Powerpoint Object Library, then you can do

Dim PPT as Powerpoint.Application


Dim newSlide as Powerpoint.Slide
Dim tb as Powerpoint.Shape
If you don't want to, or can't enable reference to the PPT object library, try to Dim tb as
Variant or Dim tb as Object and that might work.
UPDATE 2 How to enable reference to Powerpoint:
In the VBE, from Tools | References, check the box corresponding to the PPT version
supported on your machine. In Excel 2010, this is 14.0. In 2007 I think it is 12.0.
Update 3
The Duplicate Method does not appear to be available in 2007. In any case, it also causes
a strange error in 2010, although the slide is copied correctly, the variable is not set.
Try this instead:

Sub PPTTest()

Dim PPT As PowerPoint.Application


Dim pres As PowerPoint.Presentation
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape

Set PPT = CreateObject("PowerPoint.Application")


PPT.Visible = True

'Control the presentation with a variable


Set pres = PPT.Presentations.Open("C:\users\david_zemens\desktop\Presentation1.pptx")

Range("F2").Activate
slideCtr = 1

'## This only works in 2010/2013 ##


'pres.Slides(slideCtr).Duplicate

'## Use this method in Powerpoint 2007 (hopefully it works)


pres.Slides(slideCtr).Copy
pres.Slides.Paste
Set newslide = pres.Slides(pres.Slides.Count)
newslide.MoveTo slideCtr + 1
...
shareimprove this answer edited May 16 '13 at 20:02 answered May 16 '13 at 15:16

David Zemens
38.8k83773
Thanks David. I had actaully changed txtReqBase back to TexBox1 after I posted this. I was originally trying to give it
a unique identifier. I tried your code, but the set statement actually produces a type mismatch. Spiderman May 16 '13
at 17:01

Also why do you Dim tb as a Shape rather than an object? Spiderman May 16 '13 at 17:27

A TextBox is a member of the slide's Shapes collection. Dimensioning it specifically as a Shape, rather than a generic
Object/Variant enables the script-assist feature in the VBE. David Zemens May 16 '13 at 17:35

See revision above, re the mismatch error. David Zemens May 16 '13 at 17:41

I had tried to define references earlier as PowerPoint....., but this version of Excel (2007) does not recognize it as a
valid type. Spiderman May 16 '13 at 17:53
show 11 more comments

up I had forgotten that I had switched from a textbox to an activex control textbox. here's
vote0do the correct code now.
wn vote valppt()
Dim PPT As PowerPoint.Application
Dim newslide As PowerPoint.SlideRange
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True

PPT.Presentations.Open ("C:\Documents\createqchart.pptx")

Range("F2").Activate
slideCtr = 1
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
Set tb = newslide.Shapes("TextBox" & slideCtr)

slideCtr = slideCtr + 1
Do Until ActiveCell.Value = ""
'Do Until slideCtr > 2
If slideCtr = 2 Then
tb.OLEFormat.Object.Value = Format(ActiveCell.Value, "m/d/yyyy")
End If
ActiveCell.Offset(0, 1).Activate
slideCtr = slideCtr + 1

If slideCtr = 38 Then
Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
ActiveCell.Offset(1, -25).Activate
End If

Loop
End Sub
shareimprove this answer answered May 17 '13 at 20:14

Spiderman
26228
add a comment
Your Answer

Sign up or log in
Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name
Email
Post Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
Not the answer you're looking for? Browse other questions tagged excel vba excel-
vba textbox powerpoint or ask your own question.
asked
4 years, 3 months ago
viewed
27,806 times
active
1 year, 7 months ago
BLOG

Where in the World is Mobile Development?

Looking for a job?

Javascript/React Developer (Eastern Europe) (remote)


MobyMaxNo office location
REMOTE
reactjsjavascript

Remote PHP Fullstack Developer


GiteSoftNo office location
$30K - $54KREMOTE
phpmysql

Business Intelligence Developer (w/m)


Sysmex Europe GmbHNorderstedt, Deutschland
50K - 65KVISA SPONSORSHIP
tsqlsql

Software Engineer
DatadogNew York, NY
REMOTE
gopython

Linked

0
Real Time Excel Calculation in powerpoint

0
Applying loop to copy-paste range of cells from excel to PowerPoint 2013

Related

5
EXcel VBA : Excel Macro to create table in a PowerPoint

1
Excel / PowerPoint text size after shrink to fit

5
Close Powerpoint Object with Excel VBA (NOT using Powerpoint.Application)

0
Unable to populate PowerPoint TextBox with value in Excel cell using VBA
0
Populate A Table in a Powerpoint Template Using Cells in Excel - VBA

1
Unable to extract Powerpoint textbox contents into Excel using Excel VBA

1
Loop through excel-spreadsheet-rows until empty using VBA-macro in powerpoint. For each row read values and
write to 2-dim-array. No .select

1
using VBA in Excel to paste into Powerpoint

1
Error in Code to Paste / Copy Shapes from Excel to PowerPoint VBA

1
Powerpoint VBA dont close Excel

Hot Network Questions


sed -e '$!d' not working as expected?
What is this "shadow" called and what's the purpose?
Does the Federation have anything resembling intellectual property laws or agreements?
Is it possible for two symbiotic lifeforms to be born from the same egg?
Why does Daenerys always ride the same dragon?
Is it rude to ask if a pregnancy was planned?
Clara's lost luggage
Is this person wanting to pay my credit card trying to defraud me?
Four matchsticks puzzle
When trying to establish a connection with a STEM field professor, is it appropriate to ask them out for coffee?
Turn an integer n into a list containing it n times
Are there other options besides HTTPS for securing a website to avoid text input warnings in Chrome?
Do PCs understand the leveling system and its experience requirements?
Is the set of all integers odd?
Is it better practice to buy RAID disks individually vs. in bulk?
What does "double vacuum pressure" mean in space-suit testing?
How do I remove this cabinet drawer?
Social etiquette in anime/manga/pop culture Japan conventions
Should I include controversial achievements in my resume?
What's wrong with moving cables for a Space Elevator?
Count forth and back then double up
Counting neighbor cells of a matrix
If a thousand people whisper inaudibly, will the resulting sound be audible?
Align the captions in tikz \subfloat

question feed

STACK OVERFLOW
Questions
Jobs
Developer Jobs Directory
Documentation
Help
Mobile
STACK OVERFLOW
BUSINESS
Talent
Ads
Enterprise
Insights
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Contact Us
STACK EXCHANGE
NETWORK
Technology
Life / Arts
Culture / Recreation
Science
Other

Blog

Facebook

Twitter

LinkedIn
site design / logo 2017 Stack Exchange Inc; user contributions licensed under cc by-sa
3.0with attribution required. rev 2017.8.22.26825

Вам также может понравиться