Log in

View Full Version : PowerPoint: .Add method acting up



Viewbasic
11-08-2008, 01:23 AM
Hello, could some one proof this code, I have tried numerous times to remedy the present error, however I have been Unsucessful.

:banghead: Please Help

Much Appreciated

I'm sorry I'm having problems uploading the code so here it is below:



Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim intCountSldes As Integer
Dim intSldNum As Integer
Dim sldComp As Slide
Dim strCompany As String
strCompany = txtCompany

'Remove existing slides

With ActivePresentation
For intCountSldes = 1 To .Slides.Count
.Slides(1).Delete
Next
End With

'Adds Title page to presentation
Set sldComp = ActivePresentation.Slides(intCountSldes)
sldComp.Layout = ppLayoutTitle

'Inserts Text into Title Shape

With sldComp.Shapes(1).TextFrame.TextRange
.Lines(1) = strCompany
End With

'Inserts Time and Date

With sldComp.Shapes(2).TextFrame.TextRange
.Lines(3).InsertDateTime ppDateTimeMMMMdyyyy
End With

'Inserts code which formats and adds new slides

With ActivePresentation.Slides
.Add .ppLayoutTitle
End With
'Trouble area: I'm having trouble creating a code which adds new slides and formats the new slides to text
'
Any suggestions?

With ActivePresentation.Slides
.Add .Count + 1, ppLayoutText
End With

'Deletes textbox sets focus

txtCompany = ""
txtCompany.SetFocus

'end code
End Sub

John Wilson
11-08-2008, 09:03 AM
See if this helps
Private Sub cmdOK_Click()
Dim intCountSldes As Integer
Dim intSldNum As Integer
Dim sldComp As Slide
Dim strCompany As String

strCompany = txtCompany

'****************Your code will only remove one slide********

' With ActivePresentation
' For intCountSldes = 1 To .Slides.Count
' .Slides(1).Delete
' Next
' End With
'*********************************************************************
'new code
With ActivePresentation
For intCountSldes = .Slides.Count To 1 Step -1 'must count backwards!
.Slides(intCountSldes).Delete
Next intCountSldes
End With

'Adds Title page to presentation

'**************THIS CAN'T WORK CAN IT?********************

' Set sldComp = ActivePresentation.Slides(intCountSldes)
' sldComp.Layout = ppLayoutTitle

'Inserts Text into Title Shape
'**********************************************************

'try

Set sldComp = ActivePresentation.Slides.Add(1, ppLayoutTitle)

With sldComp.Shapes(1).TextFrame.TextRange
.Lines(1) = strCompany
End With

'Inserts Time and Date

With sldComp.Shapes(2).TextFrame.TextRange
.Lines(3).InsertDateTime ppDateTimeMMMMdyyyy
End With

'To add eg six text slides
For Count = 2 To 7
ActivePresentation.Slides.Add Count, ppLayoutText
Next Count

Set sldComp=Nothing
'end code
End Sub

Viewbasic
11-08-2008, 12:25 PM
Thank you for the Help it works like a charm. :beerchug:

But I have a couple of questions if you don't mind. (I'm a bit of a newer coder)


For this code, I thought by using the For...Next Loop to delete the first slide, it would eventually delete all of them. For example: I delete slide one then slide two becomes slide one, therefore slide two is deleted and slide three becomes slide one etc.
Why was it that this code did not perform the specified task?

' With ActivePresentation
' For intCountSldes = 1 To .Slides.Count
' .Slides(1).Delete
' Next
' End With


Why must this count backwards? Also at.Slides(intCountSldes) is the code saying that at whatever slide number ( thats what inCountSldes represents) after one it must be deleted?

'new code
With ActivePresentation
For intCountSldes = .Slides.Count To 1 Step -1 'must count backwards!
.Slides(intCountSldes).Delete
Next intCountSldes
End With
I have tried to set sldComp to ActivePresenation.slides.add numerous times, but the last part (1,ppLayoutTitle) I have not done.
Is it like excel's RowCounter method were it determines where the data will be placed in column? Just this time it refers to slide number and layout?


Set sldComp = ActivePresentation.Slides.Add(1, ppLayoutTitle



Once again, I thank you for your help, but I would greatly appreciate it if you or anyone else viewing, offer answers to my questions above. Thanks much :)

Viewbasic
11-08-2008, 12:27 PM
I'm almost forgot
for

'To add eg six text slides
For Count = 2 To 7
ActivePresentation.Slides.Add Count, ppLayoutText
Next Count

How can I make it that the number of slides are base upon intCountSldes?
Or is that possible?

John Wilson
11-09-2008, 04:01 AM
Having thought about it your code to delete would work and leave IntCountSlides = the original number of slides (this isn't true if you use my code)


After .Add the number and layout are the position to add and the layout to add


To create the original number of slides



For i = 2 to IntcountSlides

ActivePresentation.Slides.Add(i,ppLayoutText)
Next i