1

I am trying to create a form with vba and for that I am using the following code:

Private Sub createButton_Click()
    Dim cSlide As Slide
    Dim survey As Shape
    Dim text As String
    Dim top As Integer

    'Dim TempForm As Object ' VBComponent
    Dim FormName As String
    Dim NewButton As MSForms.CommandButton
    Dim TextLocation As Integer
'   ** Additional variable
    Dim X As Integer
    If singleOption.value Then
        typ = "radio"
        Else
            If multipleOption.value Then
                typ = "checkBox"
            Else
                If dropdown.value Then
                    typ = "dropdown"
                Else
                    MsgBox "Please, select survey type before continue"
                    Exit Sub
                End If
            End If
        End If
        If tagBox = "" Then
            MsgBox "Please, write a title before continue"
            Exit Sub
        End If
        If choiceNum = "" Then
            MsgBox "Please, set the options number"
            Exit Sub
    End If
    'Locks Excel spreadsheet and speeds up form processing
    Application.VBE.MainWindow.Visible = False
    'Application.ScreenUpdating = False
    choNum = choiceNum
'   Create the UserForm
    Set TempForm = ActivePresentation.VBProject.VBComponents.Add(vbext_ct_MSForm)
    'TempForm.Activate
    'Set Properties for TempForm
    With TempForm
        .Properties("Caption") = "Possible answers"
        .Properties("Width") = 300
        .Properties("Height") = 10 + 34 * choiceNum + 50
    End With
    FormName = TempForm.Name

    For i = 1 To choiceNum

        Set newTab = TempForm.Designer.Controls.Add("Forms.Label.1", "label" & i, True)
        With newTab
            .Caption = "Answer" & i
            .width = 40
            .height = 15
            .top = 10 + 30 * (i - 1)
            .left = 10
        End With

        Set cCntrl = TempForm.Designer.Controls.Add("Forms.TextBox.1", "textBox" & i, True)
        With cCntrl
            .width = 150
            .height = 15
            .top = 10 + 30 * (i - 1)
            .left = 60
            .ZOrder (0)
        End With
    Next i

    Set NewButton = TempForm.Designer.Controls.Add("forms.CommandButton.1", "answerButton", True)
    With NewButton
        .Caption = "Create survey"
        .left = 60
        .top = 30 * choiceNum + 10
    End With

    ActiveWindow.Selection.Unselect

    Set cSlide = Application.ActiveWindow.View.Slide

    Set survey = cSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 20, 400, 20)
    survey.TextFrame.TextRange.Font.Size = 25
    survey.TextFrame.TextRange.text = tagBox
    height = survey.height
    survey.Select
    'X = ActivePresentation.VBProject.VBComponents(FormName).CodeModule.CountOfLines
    With TempForm.CodeModule
      X = .CountOfLines + 1
        .InsertLines X + 1, "Sub answerButton_Click()"
        .InsertLines X + 2, "   Dim cSlide As Slide"
        .InsertLines X + 3, "  Dim survey As Shape"
        .InsertLines X + 4, "  Dim top As Integer"
        .InsertLines X + 5, "   Set cSlide = Application.ActiveWindow.View.Slide"
        .InsertLines X + 6, "   top = 30 + surveyCreation.height - 20"
        .InsertLines X + 7, "   "
        .InsertLines X + 8, "   For i = 1 To surveyCreation.choNum"
        .InsertLines X + 9, "   "
        .InsertLines X + 10, "   top = top + 15"
        .InsertLines X + 11, "   Set survey = cSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 30, top, 400, 10)"
        .InsertLines X + 12, "   survey.TextFrame.TextRange.text = Me.Controls(i * 2 - 1).Text"
        .InsertLines X + 13, "   "
        .InsertLines X + 14, "   survey.TextFrame.TextRange.ParagraphFormat.Bullet = True"
        .InsertLines X + 15, "   survey.TextFrame.TextRange.ParagraphFormat.Bullet.Type = ppBulletUnnumbered"
        .InsertLines X + 16, "  survey.Select Replace:=False"
        .InsertLines X + 17, "  Next i"
        .InsertLines X + 18, "  With ActiveWindow.Selection.ShapeRange"
        .InsertLines X + 19, "  .Group.title = ""Dink survey creation"" & surveyCreation.typ"
        .InsertLines X + 20, "End With"
        .InsertLines X + 21, "Application.VBE.ActiveVBProject.VBComponents.Remove Application.VBE.ActiveVBProject.VBComponents(Application.VBE.ActiveVBProject.VBComponents.Count)"
        .InsertLines X + 22, "End Sub"
    End With


    'TempForm.Activate
    tagBox.text = ""
    choiceNum = ""
    'ActivePresentation.VBProject.VBComponents.Add vbext_ct_MSForm
    surveyCreation.Hide
    'TempForm.Show

    VBA.UserForms.Add(FormName).Show
    ActivePresentation.VBProject.UserForms
End Sub

It is working well if I run the code in the presentation were I create the macro but if I want to exec it in another other it gives me the "object required" error. I try with ActivePresentation.VBA witch is not even compiling.

EDIT: I create a ppam file and add it to powerpoint but it is giving me the same problem even in the presentation were I create it. So if I exec the code in the presentation were I create it, it works. But if I exec the ppam code (I add a button to exec it) it is giving me object required error.

4

2 回答 2

1

好的,我有一些时间来测试一下。

我添加了一些代码,这些代码将在运行时创建一个标准代码模块ActivePresentation,其中一个子例程称为ShowMe. 然后可以从以下位置调用此子例程:

Application.Run ActivePresentation.Name & "!ShowMe"

这是示例代码。我已经在 PPAM 文件中对其进行了测试,它成功地在ActivePresentation.

Option Explicit
Sub Test()
Dim TempForm As Object 'VBComponent / Late Binding
Dim showModule As Object 'VBComponent
Dim FormName As String
Dim choiceNum As Long: choiceNum = 3
Dim vbComps As Long
Dim X As Long

    Set TempForm = ActivePresentation.VBProject.VBComponents.Add(3) 'vbext_ct_MSForm
    With TempForm
        .Properties("Caption") = "Possible answers"
        .Properties("Width") = 300
        .Properties("Height") = 10 + 34 * choiceNum + 50
        FormName = .Properties("Name")
    End With

    '## Insert a standard code module which will contain a subroutine to show the TempForm
    Set showModule = ActivePresentation.VBProject.VBComponents.Add(1) 'vbext_ct_StdModule
    With showModule.CodeModule
        X = .CountOfLines + 1
        .InsertLines X + 1, "Sub ShowMe()"
        .InsertLines X + 2, "    " & FormName & ".Show"
        .InsertLines X + 3, "End Sub"
    End With

    Application.Run ActivePresentation.Name & "!ShowMe"

    '## Remove the module & user form created, above
    ActivePresentation.VBProject.VBComponents.Remove TempForm
    ActivePresentation.VBProject.VBComponents.Remove showModule

    '## Clean up
    Set TempForm = Nothing
    Set showModule = Nothing
End Sub

虽然我认为可能有另一种方法可以实现这一点(类似于您尝试使用的.Show方法),但我无法做到这一点。上述方法似乎是可靠的。

注意我正在使用Option Explicit. 您的代码有未声明的变量,因此这会引发一些警告,并且您的代码在清理之前不会执行。

于 2013-07-28T21:49:16.287 回答
0

我没有详细研究过代码,但是选择文件菜单(2010年后台查看)PowerPoint选项,信任中心,点击信任中心设置,勾选“信任访问vba项目对象模型”。

于 2013-07-18T21:58:52.963 回答