About Naming MSForm in VBE.

F

fujing1003

The following code could only run correctly once after the workbook's
open. I could not understand why we couldn't name a form as the early
one we've deleted before. IF we change vbext_ct_MSForm to
vbext_ct_StdModule, all goes well.
What's the reason of it? Is it caused by form's designer object?
Thanks in advance.
Sub AddAndRenameVBComponent()
Dim oProjForm As VBComponent
Dim oProj As VBProject

Const sFormName As String = "FormTest"

Set oProj = ThisWorkbook.VBProject

On Error Resume Next
oProj.VBComponents.Remove oProj.VBComponents(sFormName)
On Error GoTo 0

Set oProjForm = oProj.VBComponents.Add(vbext_ct_MSForm)

With oProjForm
.Name = "Test"
End With

End Sub
 
F

fujing1003

The following code could only run correctly once after the workbook's
open. I could not understand why we couldn't name a form as the early
one we've deleted before. IF we change vbext_ct_MSForm to
vbext_ct_StdModule, all goes well.
What's the reason of it? Is it caused by form's designer object?
Thanks in advance.
Sub AddAndRenameVBComponent()
Dim oProjForm As VBComponent
Dim oProj As VBProject

Const sFormName As String = "FormTest"

Set oProj = ThisWorkbook.VBProject

On Error Resume Next
oProj.VBComponents.Remove oProj.VBComponents(sFormName)
On Error GoTo 0

Set oProjForm = oProj.VBComponents.Add(vbext_ct_MSForm)

With oProjForm
.Name = "Test"
End With

End Sub
Somebody replays as follows:
'-------------------------------------------------------------------------------------------------------------------
Once you run your code, VBE constructor has allocated used userform
names in
cache during a VBE session.

If you want to create a new userform (eventhough it is deleted
physically
from the VBcomponents collection) using a previously used name in the
same
VBE session, you'll see this error.
'-------------------------------------------------------------------------------------------------------------------
It's reasonable,but how to clear userform in this vbe session?
 
P

Peter T

I get the same problem even if doing all manually. However, some while ago I
noticed if you drag form-name from another wb all seems to work (assuming of
course form-name no longer exists in the main wb).

Based on same idea try the following:

Sub AddAndRenameVBComponent2()
Dim oProjForm As VBComponent
Dim oProj As VBProject
Dim wb As Workbook
Set wb = Workbooks.Add

Const sFormName As String = "FormTest"

Set oProj = ThisWorkbook.VBProject

On Error Resume Next
oProj.VBComponents.Remove oProj.VBComponents(sFormName)
On Error GoTo 0

Set oProjForm = wb.VBProject.VBComponents.Add(vbext_ct_MSForm)
oProjForm.Name = sFormName

GoSub KillForm
oProjForm.Export "c:\temp\FormTest.frm"
wb.Close False

' can't 'step through this line, run/F5
Set oProjForm = oProj.VBComponents.Import("c:\temp\FormTest.frm")

GoSub KillForm
Exit Sub
KillForm:
On Error Resume Next
Kill "c:\temp\FormTest.frm"
Kill "c:\temp\FormTest.frx"
On Error GoTo 0
Return
End Sub

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top