Thev
Thev

Reputation: 1125

Powerpoint VBA - write function to create shape

I have the following function, which I intended to create a shape:

Public Function LinkToAddInfo(ShapeName As String, BoxName As String, DisplayNumber As Long, AddName As String, TrueNumber As Long) As Shape

        Dim ShapeName As Shape
        Set ShapeName = .Shapes.AddShape(msoShapeRoundedRectangle, 640, 470, 71, 27)

        With ShapeName
            .Fill.ForeColor.RGB = RGB(191, 191, 191)
            .Fill.Transparency = 0
            .Name = BoxName

            With .Line
            .Weight = 0
            .ForeColor.RGB = RGB(191, 191, 191)
            .Transparency = 0
            End With ' Outline

            With .TextFrame.TextRange
              .Text = "Add. Info " & DisplayNumber & vbNewLine & AddName
               With .Font
                .Name = "Arial"
                .Size = 8
                .Bold = msoTrue
                .BaselineOffset = 0
                .AutoRotateNumbers = msoFalse
                .Color.RGB = RGB(255, 255, 255)
            End With   ' Font

           End With   ' TextFrame

        End With ' Square itself

End Function

I tried to call it from within a module using:

LinkToAddInfo("tiny1", "Yedinfo1", DisplayNumber, AddName, AddNumber)

But it throws an error (the code is shown in red within the editor).

When I have all the code within the module itself, it works fine. I'm just struggling to transcribe it into an external function (which I want to do so that I don't have to repeat this code again and again).

How can I achieve this?

Upvotes: 1

Views: 222

Answers (1)

John Korchok
John Korchok

Reputation: 4913

You have several problems with this.

  • You don't need a Function for this, since you aren't returning data to the program. Instead, use a Sub.
  • The first argument is a string, but then you try to use the same variable name in a declaration as a shape. But you don't use the string argument for anything, so it can be deleted. You also don't use TrueNumber, so that can be taken out.
  • To access a slide master, you need to use Designs with a numeric argument, not SlideMaster. The following should do what you want:
Public Sub LinkToAddInfo(BoxName As String, DisplayNumber As Long, AddName As String)
    Dim oShape As Shape
    Set oShape = ActivePresentation.Designs(1).SlideMaster.Shapes.AddShape(msoShapeRoundedRectangle, 640, 470, 71, 27)

    With oShape
        .Fill.ForeColor.RGB = RGB(191, 191, 191)
        .Fill.Transparency = 0
        .Name = BoxName
        With .Line
        .Weight = 0
        .ForeColor.RGB = RGB(191, 191, 191)
        .Transparency = 0
        End With ' Outline
        With .TextFrame.TextRange
            .Text = "Add. Info " & DisplayNumber & vbNewLine & AddName
            With .Font
                .Name = "Arial"
                .Size = 8
                .Bold = msoTrue
                .BaselineOffset = 0
                .AutoRotateNumbers = msoFalse
                .Color.RGB = RGB(255, 255, 255)
            End With   ' Font
        End With   ' TextFrame
    End With ' Square itself
End Sub
```


Upvotes: 1

Related Questions