Consulting

Results 1 to 6 of 6

Thread: Create a shape with a predefined format?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Create a shape with a predefined format?

    Hi there, Im trying to create a macro that paste "flags" (shapes of different colors) to clasify the slides of a presentation.

    I´m totally new to VBA. I managed to create a rectangular shape in the TOP RIGHT corner of the current slide, but it create a blue shape with blue border, and i want this shape to be red, 35% transparency and no border.

    Is there any way to pre-define the format of the shape before creating it? or should I change the shape´s format once it´s created?

    Thanks!

    [VBA]Sub AZ_Banderitas()
    'if there´s no slide selected the shape will be pasted in the previous slide
    On Error Resume Next
    Err.Clear
    Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
    If Err <> 0 Then
    ActiveWindow.ViewType = ppViewNotesPage
    ActiveWindow.ViewType = ppViewNormal
    Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
    End If

    'creates shape
    slideLocation = ActiveWindow.View.Slide.SlideIndex

    myDocument.Shapes.AddShape Type:=msoShapeRectangle, Left:=570, Top:=0, width:=150, Height:=100

    End Sub[/VBA]

  2. #2
    I´ve changed the code a bit..

    [VBA]Sub AZ_Banderitas()
    'if there´s no slide selected the shape will be pasted in the previous slide
    On Error Resume Next
    Err.Clear
    Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
    If Err <> 0 Then
    ActiveWindow.ViewType = ppViewNotesPage
    ActiveWindow.ViewType = ppViewNormal
    Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
    End If

    'creates shape
    slideLocation = ActiveWindow.View.Slide.SlideIndex

    Set myDocument = ActivePresentation.Slides(slideLocation)

    Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)

    End Sub[/VBA]

  3. #3
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Creating a new shape will use the default properties for that shape. If you want it to always be a certain color (or change any other property) when you run the code, then you should set the properties after creating it.
    [vba] Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
    shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
    [/vba]

    If you have multiple properties you can set them like this:
    [vba]
    Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
    with shp
    .Fill.ForeColor.RGB = RGB(255, 0, 0)
    .Height = 200
    '... and any other properties here
    end with
    [/vba]


    Otherwise, your script will always use whatever are set as the default shape properties when the script runs.

  4. #4
    Quote Originally Posted by Cosmo
    Creating a new shape will use the default properties for that shape. If you want it to always be a certain color (or change any other property) when you run the code, then you should set the properties after creating it.
    [vba] Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
    shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
    [/vba]

    If you have multiple properties you can set them like this:
    [vba]
    Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
    with shp
    .Fill.ForeColor.RGB = RGB(255, 0, 0)
    .Height = 200
    '... and any other properties here
    end with
    [/vba]


    Otherwise, your script will always use whatever are set as the default shape properties when the script runs.
    Thanks! That worked, now im trying to put some text in those boxes...

    [VBA] Set myDocument = ActivePresentation.Slides(slideLocation)

    Dim shp As Shape

    Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)

    With shp.Fill
    .Transparency = 0.35
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Solid
    End With

    With shp.Line
    .Visible = msoFalse
    End With[/VBA]

  5. #5
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    [VBA]shp.TextFrame.TextRange.Text = "Enter your text here"
    [/VBA]

  6. #6
    Quote Originally Posted by Cosmo
    [VBA]shp.TextFrame.TextRange.Text = "Enter your text here"
    [/VBA]
    Thanks again!

Posting Permissions

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