PDA

View Full Version : [SOLVED:] Help please - userform not coming up



c0lin
08-04-2018, 12:44 AM
Hi,

I'm verturing into my first attempts at some simple coding with userforms and have been followinf various examples and tutorials online. However my 'test' just refuses to display the userform I have created.

I have this in the userform (named MyForm) part of the project

Option Explicit

Private Sub cmdSubmit_Click()
FillBM "Test", txttest.Text
Unload Me
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
Dim orng As Range
With ActiveDocument
Application.ScreenUpdating = False
On Error GoTo lbl_Exit
Set orng = .Bookmarks(strBMName).Range
orng.Text = strValue
orng.Bookmarks.Add strBMName
End With
Application.ScreenUpdating = True
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

Private Sub cmdCancel_Click()
ActiveDocument.Close SaveChanges:=False
MyForm.Hide

End Sub

And this in the Module (named modMain)

Sub AutoNew()
Create_Reset_Variables
CallUF
lbl_Exit:
Exit Sub
End Sub

And this in the Thus Document part (although I also tried sticking it in the ModMain as well)

Option Explicit

Sub CallUF()
Dim oFrm As MyForm
Set oFrm = New MyForm
oFrm.Show
Unload oFrm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub


The AutoNew works fine and creates a new document and the macro CallUf is there (and I can run in manually) but why won't it start automatically when I create a new doc from the template?

Any help would be appreciated

Thanks

Oh - here is the whole thing if it helps




22670

gmayor
08-04-2018, 02:32 AM
For a start this isn't a template but a macro enabled document. It would be better saved as a macro enabled template if you plan to create documents from it.
The Sub CallUF() should be in the ModMain module. It contains code that is not referenced anywhere i.e. Create_Reset_Variables so that line should be removed.
The userform code will crash on the cancel button

Change the Cancel code as follows

Private Sub cmdCancel_Click()
Me.Hide
ActiveDocument.Close SaveChanges:=False
End Sub

It will then work when you create a new document from the template - File > New
As your document contains some of my code see http://www.vbaexpress.com/forum/showthread.php?63294-Userform-bookmarks-problem-!st-try-not-going-well
and the code in the Help template I posted there.

c0lin
08-04-2018, 03:29 AM
Fantastic - thanks Graham,

Now, being new to all this, how do I mark the thread as solved?

c0lin
08-04-2018, 03:30 AM
Don't worry - found it :)