Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

How to automatically start UserForm on document open

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Electech - 14 Nov 2006 18:24 GMT
How can I code my user form to pop-up (initialize) when someone opens a
document or template in Word 2003?
Signature

Electech

Jean-Guy Marcil - 14 Nov 2006 20:43 GMT
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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.