簡體   English   中英

Powerpoint VBA - 編寫函數來創建形狀

[英]Powerpoint VBA - write function to create 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

我嘗試使用以下方法從模塊中調用它:

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

但它會引發錯誤(代碼在編輯器中以紅色顯示)。

當我擁有模塊本身中的所有代碼時,它工作正常。 我只是在努力將它轉錄成一個外部函數(我想這樣做,這樣我就不必一次又一次地重復這段代碼)。

我怎樣才能做到這一點?

你有幾個問題。

  • 您不需要為此使用函數,因為您沒有將數據返回給程序。 相反,使用 Sub。
  • 第一個參數是一個字符串,但隨后您嘗試在聲明中使用相同的變量名稱作為形狀。 但是您不將字符串參數用於任何內容,因此可以將其刪除。 您也不使用 TrueNumber,因此可以將其取出。
  • 要訪問幻燈片母版,您需要使用帶有數字參數的 Designs,而不是 SlideMaster。 以下應該做你想做的:
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
```


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM