2013-07-18 13 views
1

我試圖創建一個VBA形式和我使用下面的代碼:不能在VBA創建形式爲另一個活動顯示

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 

它運作良好,如果我運行的代碼演示文稿是我創建的宏,但如果我想在另一個執行它,它會給我「所需的對象」錯誤。我嘗試使用ActivePresentation.VBA女巫甚至沒有編譯。

編輯: 我創建了一個ppam文件並將其添加到powerpoint中,但即使在演示文稿創建它時它也給我帶來同樣的問題。所以如果我在演示文稿中執行代碼,我創建它,它就可以工作。但是,如果我執行ppam代碼(我添加一個按鈕來執行它)它給我的對象所需的錯誤。

+0

什麼是完整的錯誤信息,它是指什麼行。 –

+1

錯誤是在行:VBA.UserForms.Add(FormName).Show和完整的錯誤是:「運行時錯誤'424':所需的對象」 –

+0

交叉發佈在http://social.msdn.microsoft .com/Forums/office/en-US/4b60ced0-b2f8-4b27-847e-24e8b7b02dee/runtime-error-424-object-required-while-doing-vbauserformsaddformnameshow –

回答

1

好的,我有一段時間來測試一下。

我添加了一些代碼,這些代碼將在運行時在ActivePresentation中創建一個標準代碼模塊,其中一個子例程名爲ShowMe。該子程序然後可以從名爲:

Application.Run ActivePresentation.Name & "!ShowMe"

下面是示例代碼。我已經在PPAM文件中對它進行了測試,併成功創建了&,顯示了ActivePresentation中的UserForm。

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。你的代碼有未聲明的變量,所以這會引發一些警告,你的代碼將不會執行,直到你清理它。

0

我沒有詳細研究代碼,但選擇文件菜單(Backstage視圖2010年)PowerPoint選項,信任中心,單擊信任中心設置,檢查「信任對VBA項目對象模型」。

+0

感謝您的答案,但我之前檢查過,所以這不是問題。 –

相關問題