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.