从其他项目打开表单,VB6

时间:2014-02-06 14:05:47

标签: vb6

您如何从表单中打开另一个项目中的表单..

假设我有这样的项目结构:

  • P1
    • Frm1中
    • FRM2
  • P2
    • frmxyz

在VB6中从frmxyz打开frm1 ......?类似的东西:

p1.frm1.show() 'Maybe?

你是怎么做到的?

2 个答案:

答案 0 :(得分:2)

有两种不同的方法可以解决此问题:

1)。 您可以使用project - >将frm1添加到项目p2。添加表单 - >标签“现有”。之后,您可以使用frm1.show()轻松访问。

2)。 使用函数创建一个接口对象来访问表单将p1编译为active-x dll。在下一步中,您可以在p2中添加active-x dll作为引用,并在接口对象中调用该函数时显示该表单。

答案 1 :(得分:0)

这很复杂。我想到的最简单的方法如下:

Sub UseExternalUserForm()

    'Set the File name, File path, and Form name
    Dim myForm As String, myDirectory As String, myFile As String
    myDirectory = "C:\FilePath\"
    myFile = "MyFile.xls"
    myForm = "UserForm1"

    'Start dealing with workbooks and their objects
    Dim vbc As Object
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Open(myDirectory & myFile)

    If UserFormExists(2, myForm) Then
        'Export the form if it exists
        wb2.VBProject.VBComponents(myForm).Export (myForm & ".frm")
    Else
        'Display an error message if it doesn't
        MsgBox myForm & " doesn't exist in " & wb2.Name
    End If

    wb2.Close False

    If UserFormExists(1, myForm) Then
        'Display an error message if it already exists
        MsgBox myForm & " already exists in " & wb1.Name
    Else
        'Import the form if it doesn't
        Set vbc = Application.VBE.vbProjects(1).VBComponents.Import(myForm & ".frm")
        VBA.UserForms.Add(myForm).Show

        'Remove the imported form
        Application.VBE.vbProjects(1).VBComponents.Remove vbc
    End If

End Sub


'How to figure out if a form already exists in a project (numbered with an integer representing the index
'           of the project in the VBAProject Explorer)
Public Function UserFormExists(projectIndex As Integer, formName As String) As Boolean

    On Error Resume Next
        UserFormExists = (Application.VBE.vbProjects(projectIndex).VBComponents(formName).Name = formName)
    On Error GoTo 0

End Function

如果您需要任何有关如何让此代码为您工作的额外说明,请与我们联系。