Electech was telling us:
Electech nous racontait que :
How can I code my user form to pop-up (initialize) when someone opens
a document or template in Word 2003?
Normally, a userform is stored in a template, so documents created from the
template will have access to the userform.
Next question is: Do you need the userform to pop up *every* time a document
based on that template is opened (What if it has been edited/saved a few
times already)?
Normally, I do it this way:
Let's say that the userform is called "frmUser", then in a Standard Module
(Called "MainCode"), I create a Sub, let's say "GetUserInfo", like this:
'_______________________________________
Sub GetUserInfo()
Dim frmInfo As frmUser
Set frmInfo = New frmUser
With frmInfo
'Code to initialize some the controls if necessary
.Show
'Do stuff with it, e.g. if we have a textbox control on the userform:
'But if user cancelled, do nothing
If Not .boolCancel Then
ActiveDocument.Bookmark("Test").range.Text = .txtUserName.Value
End If
End With
Unload frmInfo
Set frmInfo = Nothing
End Sub
'_______________________________________
In the userform code, the code behind the OK or Cancel button are:
'_______________________________________
Public boolCancel As Boolean
'_______________________________________
Private Sub cmdOK_Click()
boolCancel = False
Me.Hide
End Sub
'_______________________________________
'_______________________________________
Private Sub cmdCancel_Click()
boolCancel = True
Me.Hide
End Sub
'_______________________________________
Finally, in the ThisDocument Module:
'_______________________________________
Private Sub Document_New()
MainCode.GetUserInfo
End Sub
'_______________________________________
And if necessary every time a user opens the document:
'_______________________________________
Private Sub Document_New()
MainCode.GetUserInfo
End Sub
'_______________________________________
The advantages of doing it this way are many, all the manipulation of the
info gathered in the userform is done in a standard module where it is easy
to access other sub or modules, the code is centralized, the userform code
is kept to a minimum, so it can be easily reused elsewhere, since the code
is centralized, you can easily call the userform from anywhere as often as
needed (you could have a toolbar button that calls up "MainCode.GetUserInfo"
to display the userform to the user whenever the user wants to, etc.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org