PDA

View Full Version : PowerPoint VB issue



hannas3
06-06-2023, 10:14 AM
Hi all

I've done a lot of Excel VB but only just starting with VB in PowerPoint. A colleague had the following VB created by Chat GPT (no really!) so no idea if it's just rubbish or if it just needs a minor tweak. When we run the code, it brings up a run time error at the line in bold, below. It creates the title slide and the first content slide but goes no further; unfortunately I have no PPT VB knowledge, so don't know if this should work! TIA


Sub CreatePrescribingConsiderationsDeck()
Dim PowerPointApp As Object
Dim PowerPointPres As Object
Dim PowerPointSlide As Object

' Create a new PowerPoint application
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True

' Create a new PowerPoint presentation
Set PowerPointPres = PowerPointApp.Presentations.Add

' Add a title slide
Set PowerPointSlide = PowerPointPres.Slides.Add(1, 1)
PowerPointSlide.Shapes.Title.TextFrame.TextRange.Text = "Prescribing Considerations in Older People"

' Add content slide - Problems with Polypharmacy
Set PowerPointSlide = PowerPointPres.Slides.Add(2, 11)
PowerPointSlide.Shapes.Title.TextFrame.TextRange.Text = "Problems with Polypharmacy"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = "Polypharmacy, the use of multiple medications, can pose significant challenges in older people:"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Increased risk of drug interactions"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Higher likelihood of medication non-adherence"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Increased potential for adverse drug reactions"

' Add content slide - Cholinergic Burden
Set PowerPointSlide = PowerPointPres.Slides.Add(3, 11)
PowerPointSlide.Shapes.Title.TextFrame.TextRange.Text = "Cholinergic Burden"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = "Cholinergic burden refers to the cumulative effect of medications with anticholinergic properties. In older people:"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Anticholinergic medications can contribute to cognitive impairment, falls, and delirium"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Assess the cholinergic burden when prescribing medications"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Consider non-pharmacological alternatives when appropriate"

' Add content slide - Pain Management
Set PowerPointSlide = PowerPointPres.Slides.Add(4, 11)
PowerPointSlide.Shapes.Title.TextFrame.TextRange.Text = "Pain Management"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = "Pain management in older people requires careful consideration:"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Balance the need for effective pain relief with the risk of adverse effects"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Consider individualized approaches based on pain intensity, comorbidities, and functional status"
PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text = PowerPointSlide.Shapes.Placeholders(2).TextFrame.TextRange.Text & vbCrLf & "- Non-pharmacological interventions, such as physical therapy or heat/cold therapy, should be explored"

' Save the PowerPoint presentation
PowerPointPres.SaveAs "H:\PrescribingConsiderations.pptx"

' Close the PowerPoint application
PowerPointApp.Quit

' Release the PowerPoint objects from memory
Set PowerPointSlide = Nothing
Set PowerPointPres = Nothing
Set PowerPointApp = Nothing

MsgBox "PowerPoint deck created successfully!"
End Sub

John Wilson
06-06-2023, 12:31 PM
It is pretty bad coding but the initial problem is:

Set PowerPointSlide = PowerPointPres.Slides.Add(2, 11)

This adds a type 11 slide at number 2 (the other similar lines do the same,

THE PROBLEM IS type 11 is title only so there is no second placeholder. As a quick fix try type 2


Set PowerPointSlide = PowerPointPres.Slides.Add(2, 2) etc

Paul_Hossler
06-08-2023, 07:39 AM
The original looked like Excel macro recorder produced code :)

This is a little cleaner and includes John's fix




Option Explicit


Sub CreatePrescribingConsiderationsDeck()
Dim PowerPointPres As Object

' Create a new PowerPoint presentation
Set PowerPointPres = Application.Presentations.Add

' Add a title slide
With PowerPointPres.Slides.Add(1, ppLayoutTitle)
.Shapes.Title.TextFrame.TextRange.Text = "Prescribing Considerations in Older People"
End With

' Add content slide - Problems with Polypharmacy
With PowerPointPres.Slides.Add(2, ppLayoutText)
.Shapes(ppPlaceholderTitle).TextFrame.TextRange.Text = "Problems with Polypharmacy"
.Shapes.Placeholders(ppPlaceholderBody).TextFrame.TextRange.Text = _
"Polypharmacy, the use of multiple medications, can pose significant challenges in older people:" & vbCrLf & _
"- Increased risk of drug interactions" & vbCrLf & _
"- Higher likelihood of medication non-adherence" & vbCrLf & _
"- Increased potential for adverse drug reactions"
End With

' Add content slide - Cholinergic Burden
With PowerPointPres.Slides.Add(3, ppLayoutText)
.Shapes(ppPlaceholderTitle).TextFrame.TextRange.Text = "Cholinergic Burden"
.Shapes(ppPlaceholderBody).TextFrame.TextRange.Text = _
"Cholinergic burden refers to the cumulative effect of medications with anticholinergic properties. In older people:" & vbCrLf & _
"- Anticholinergic medications can contribute to cognitive impairment, falls, and delirium" & vbCrLf & _
"- Assess the cholinergic burden when prescribing medications" & vbCrLf & _
"- Consider non-pharmacological alternatives when appropriate"
End With

' Add content slide - Pain Management
With PowerPointPres.Slides.Add(4, ppLayoutText)
.Shapes(ppPlaceholderTitle).TextFrame.TextRange.Text = "Pain Management"
.Shapes(ppPlaceholderBody).TextFrame.TextRange.Text = _
"Pain management in older people requires careful consideration:" & vbCrLf & _
"- Balance the need for effective pain relief with the risk of adverse effects" & vbCrLf & _
"- Consider individualized approaches based on pain intensity, comorbidities, and functional status" & vbCrLf & _
"- Non-pharmacological interventions, such as physical therapy or heat/cold therapy, should be explored"
End With

' Save the PowerPoint presentation
' PowerPointPres.SaveAs "H:\PrescribingConsiderations.pptx"
'
' ' Close the PowerPoint application
' PowerPointApp.Quit
'
' ' Release the PowerPoint objects from memory
' Set PowerPointSlide = Nothing
' Set PowerPointPres = Nothing
' Set PowerPointApp = Nothing

MsgBox "PowerPoint deck created successfully!"
End Sub