Results 1 to 6 of 6

Thread: PowerPoint VB issue

  1. #1
    VBAX Newbie
    Joined
    Jun 2023
    Posts
    1
    Location

    PowerPoint VB issue

    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
    Last edited by Aussiebear; 06-06-2023 at 02:01 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,096
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Banned VBAX Newbie
    Joined
    Apr 2024
    Posts
    4
    Location
    Quote Originally Posted by hannas3 View Post
    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
    PowerPoint slides typically have specific placeholders (e.g., Title, Content, etc.), but the indexing for placeholders might not be consistent depending on the slide layout. For instance, you are using PowerPointSlide.Shapes.Placeholders(2) to access the content placeholder, which may not exist or may be in a different position depending on the layout used.
    Last edited by Aussiebear; 04-27-2025 at 11:55 AM. Reason: Edited out the spam link

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,096
    Location
    The problem (apart from ChatGPT being rubbish and creates very poor code) is you are adding a type 11 slider (Title ONLY. There is no Placeholder2) and it is bound to error when you try top adapt Placeholder (2). Try adding a type 7 - Content (or maybe type 2 text )

    Example (do similar for 3 and 4 etc)

    Set PowerPointSlide = PowerPointPres.Slides.Add(2, 11)
    Last edited by Aussiebear; 04-28-2025 at 02:29 PM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,391
    Location
    Sorry to have troubled you on this thread John, but for the second time Annaat29 has posted with spam in the content, hence the saying "Elvis has left the building" when referring to Annaat29.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •