Excel Hints

Results 1 to 6 of 6

Thread: Create a shape with a predefined format?

  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!

    VB:
    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 
    
    
    Formatting tags added by mark007

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

    VB:
    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 
    
    
    Formatting tags added by mark007

  3. #3
    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.
    VB:
    [COLOR=blue]Dim[/COLOR] shp [COLOR=blue]As[/COLOR] Shape 
     
    [COLOR=blue]Set[/COLOR] shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100) 
    shp.Fill.ForeColor.RGB = RGB(255, 0, 0) 
    
    
    Formatting tags added by mark007
    If you have multiple properties you can set them like this:
    VB:
    [COLOR=blue]Dim[/COLOR] shp [COLOR=blue]As[/COLOR] Shape 
     
    [COLOR=blue]Set[/COLOR] 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 
    
    
    Formatting tags added by mark007

    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.
    VB:
    [COLOR=blue]Dim[/COLOR] shp [COLOR=blue]As[/COLOR] Shape 
     
    [COLOR=blue]Set[/COLOR] shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100) 
    shp.Fill.ForeColor.RGB = RGB(255, 0, 0) 
    
    
    Formatting tags added by mark007
    If you have multiple properties you can set them like this:
    VB:
    [COLOR=blue]Dim[/COLOR] shp [COLOR=blue]As[/COLOR] Shape 
     
    [COLOR=blue]Set[/COLOR] 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 
    
    
    Formatting tags added by mark007

    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...

    VB:
    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 
    
    
    Formatting tags added by mark007

  5. #5
    VB:
    shp.TextFrame.TextRange.Text = "Enter your text here" 
    
    
    Formatting tags added by mark007

  6. #6
    Quote Originally Posted by Cosmo
    VB:
    shp.TextFrame.TextRange.Text = "Enter your text here" 
    
    
    Formatting tags added by mark007
    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
  •