繁体   English   中英

从其他项目打开表单,VB6

[英]Opening form from other project, VB6

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

可以说我有这样一个项目结构:

  • P1
    • 的frm1
    • FRM2
  • P2
    • frmxyz

在VB6中从frmxyz打开frm1 ......? 就像是:

p1.frm1.show() 'Maybe?

你怎么做呢?

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

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

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

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

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

如果您需要有关如何使此代码适合您的任何额外说明,请与我们联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM