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 / March 2005

Tip: Looking for answers? Try searching our database.

Runtime error 91 (simple userform)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob McCormick - 18 Mar 2005 07:08 GMT
I read Doug Robbins article
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

Decided to try creating the document which I called UserForm Practice.doc
(not a template).
I inserted the two bookmarks in the document, inserted a userform in the VBA
window with two textboxes and a command button and wrote the following code:

Option Explicit

Private Sub CommandButton1_Click()
   With ActiveDocument
       .Bookmarks("Text1").Range.InsertBefore TextBox1
       .Bookmarks("Text2").Range.InsertBefore TextBox2
   End With

   UserForm1.Hide

End Sub

Private Sub UserForm_Click()

End Sub

I then returned to the document view and created an autoopen macro as
follows:

Option Explicit
Dim UserForm1 As Object

Sub autoopen()
'
' autoopen Macro
' Macro created 3/17/2005 by Bob
'
   UserForm1.Show
End Sub

When I open the document I get the following:

Run-time error '91':
   Object variable or with block variable not set

And the debug highlight points to:

   UserForm1.Show

in the autoopen macro.

Where did I go wrong?  Would it work differently in a template with an
autonew macro?

Thanks,

Bob McCormick
Word Heretic - 18 Mar 2005 13:25 GMT
G'day "Bob McCormick" <rmc1357@twcny.rr.com>,

In order to show a userform at startup, like you are, you need to
delay the execution of the code with an ONTIME command

Eg

Sub autoopen()
Application.OnTime When:=Now + TimeValue("00:00:15"), _
   Name:="ShowForm"
end sub

sub showform()
UserForm1.Show
end sub

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice

Bob McCormick reckoned:

>I read Doug Robbins article
>http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm
[quoted text clipped - 51 lines]
>
>Bob McCormick
Bob McCormick - 18 Mar 2005 22:27 GMT
Thanks for your response, Heritic

I tried your suggestion but still get the same Runtime error.
It's like VBA runs into the userform object reference in the autoopen macro
and can't find it in the document.
How do I make certain the autoopen macro has the information it needs to
find UserForm1.

I'm new to VBA in Word, but it seems like there must be something fairly
obvious missing.  I just don't have enough experience to know what it is.

Thanks,

Bob McCormick

> G'day "Bob McCormick" <rmc1357@twcny.rr.com>,
>
[quoted text clipped - 16 lines]
> steve from wordheretic.com (Email replies require payment)
> Without prejudice
Word Heretic - 25 Mar 2005 08:32 GMT
G'day Word Heretic <myfullname@tpg.com.au>,

Ok, I see now - you are all mucked up

UserForm1 is the actual module name of the form right?

So

Dim MyForm as UserForm1

Set MyForm = New UserForm1

Myform.show

Unload Myform  'better than set = nothing for forms as ensures
                           'special events, win handlers etc dropped

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice

Word Heretic reckoned:

>G'day "Bob McCormick" <rmc1357@twcny.rr.com>,
>
[quoted text clipped - 74 lines]
>>
>>Bob McCormick
Bob McCormick - 18 Mar 2005 22:38 GMT
I forgot to mention I'm using Word 2K

>I read Doug Robbins article
>http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm
[quoted text clipped - 52 lines]
>
> Bob McCormick
 
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.