Consulting

Results 1 to 4 of 4

Thread: Solved: Add Textbox to an array of slides

  1. #1
    VBAX Newbie
    Joined
    Jul 2012
    Location
    Finger Lakes Region of NYS
    Posts
    2
    Location

    Solved: Add Textbox to an array of slides

    This is basic but I'm not sure how to index through a set of slides --- I'd like to add a textbox to the first four or five slides in an existing presentation programmatically.... here is what I have --- my code produces 4 copies of the text box on slide 1 instead of one textbox on Slide 1,2, 3, 4...... I know it's the 'Active.Window' that is incorrect but I'm not sure how to fix it....


    Sub addTxtBox()
    Dim oTextBox As Shape
    Dim strText As String
    Dim oSlide As Slide '* Slide Object
    Dim sldCurrSlides As PowerPoint.SlideRange
    Set sldCurrSlides = ActivePresentation.Slides.Range(Array(1, 2, 3, 4))

    strText = "Replace this text with your analysis comments........."

    For Each oSlide In ActivePresentation.Slides.Range(Array(1, 2, 3, 4))

    'Add text box with text string.
    Set oTextBox = ActiveWindow.Selection.SlideRange.Shapes.AddTextbox(msoTextOrientationHoriz ontal, 25, 575, 1050, 70)

    With oTextBox
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(255, 0, 0)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    .TextFrame.TextRange.Text = strText
    .TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
    .TextFrame.TextRange.Font.Name = "Arial"
    .TextFrame.TextRange.Font.Size = "16"
    .TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft
    .TextFrame.TextRange.Select
    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(255, 255, 255)
    .Fill.Transparency = 0#
    .Line.Weight = 2#
    End With
    Next oSlide

    End Sub

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Here's two ways, the first uses your array the other adds the box only to selected slides.:
    [vba]Sub addTxtBox()
    Dim oTextBox As Shape
    Dim strText As String
    Dim oSlide As Slide '* Slide Object
    Dim sldCurrSlides As SlideRange
    Set sldCurrSlides = ActivePresentation.Slides.Range(Array(1, 2, 3, 4))

    strText = "Replace this text with your analysis comments........."

    For Each oSlide In sldCurrSlides

    'Add text box with text string.
    Set oTextBox = oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, 575, 1050, 70)

    With oTextBox
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(255, 0, 0)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    .TextFrame.TextRange.Text = strText
    .TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
    .TextFrame.TextRange.Font.Name = "Arial"
    .TextFrame.TextRange.Font.Size = "16"
    .TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft
    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(255, 255, 255)
    .Fill.Transparency = 0#
    .Line.Weight = 2#
    End With
    Next oSlide

    End Sub

    Sub addTxtBoxALT()
    Dim oTextBox As Shape
    Dim strText As String
    Dim oSlide As Slide '* Slide Object
    Dim sldCurrSlides As SlideRange
    On Error Resume Next
    'basic error trap
    If ActiveWindow.Selection.Type = ppSelectionNone Then Exit Sub
    Set sldCurrSlides = ActiveWindow.Selection.SlideRange

    strText = "Replace this text with your analysis comments........."

    For Each oSlide In sldCurrSlides

    'Add text box with text string.
    Set oTextBox = oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, 575, 1050, 70)

    With oTextBox
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(255, 0, 0)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    .TextFrame.TextRange.Text = strText
    .TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
    .TextFrame.TextRange.Font.Name = "Arial"
    .TextFrame.TextRange.Font.Size = "16"
    .TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft
    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(255, 255, 255)
    .Fill.Transparency = 0#
    .Line.Weight = 2#
    End With
    Next oSlide

    End Sub
    [/vba]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Newbie
    Joined
    Jul 2012
    Location
    Finger Lakes Region of NYS
    Posts
    2
    Location
    John --- thanks for the quick response --- this is excellent ----

    I was stuck on the 'ActiveWindow' ---- you cleared it up nicely ----

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Note that you should NOT select the textrange too.

    It is hardly ever necessary to select and will often throw errors if the object is not selectable (eg if it is on a non active slide)
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

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