[英]Opening form from other project, VB6
您如何從表單中打開另一個項目中的表單?
可以說我有這樣一個項目結構:
在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.