Problem with UserForm in Template

W

WembleyBear

I have created a template based on the suggestion at
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

It all goes to plan except when it is executing the code for the button to
update the bookmarks on the template. My bookmarks are named as shown in the
example but the VB debugger gives an error on .Bookmarks of 'invalid use of
property'
Code is as follows:

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

Any suggestions would be appreciated, in fairly newbie language please.

Martyn
Word 2000
Windows 2003 over Citrix XPe
 
J

Jay Freedman

You have split two of the statements into two lines each, where they must be
one line in order to work.

Change to this:

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

Greg Maxey

Martyn,

I hate to say that the obvious problem is that you didn't do a very
good job of following the instructions.

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

Is missing the essential ".Range" method and the line continuation
characters "_" that are shown in the example.

See:
http://word.mvps.org/FAQs/MacrosVBA/_AtEndOfLine.htm

and "Writing Visual Basic Statements" In VBA Help.
 
G

Greg Maxey

Jay,

Adding .Range will help too ;-)


Jay said:
You have split two of the statements into two lines each, where they must be
one line in order to work.

Change to this:

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

WembleyBear

Doh! Thanks Jay, that part works perfectly now. However the Autonew macro
doesn't start automatically when I open the template - it only starts if I
actually run the macro in the template. Again, I have put in the code as
shown in the example....

Sub autonew()
'
' autonew Macro
' Macro created 15/11/2006 by mgr060
'

UserForm1.Show

End Sub
 
W

WembleyBear

Greg

Thanks but as Jay pointed out, the Range part of the statement is not needed
in this instance (and I don't need it for my purposes either); and the
example on the site is set out particularly poorly. Fortunately, I'm not a
total idiot....

Martyn :)
 
G

Greg Maxey

Martyn,

I note your smile, but still want to say that I didn't mean to imply
you where an idiot at all and apologize for coming across that way to
you or anyone else reading this thread. I'm sorry.

Hmm...I get an compile error "Method or data member not found" if I
exclude the .Range part. I sense I am about to be schooled ;-)
 
J

Jay Freedman

Yes, you're right. I'm working today on a machine without Office, so maybe I
should just keep my head down...
 
G

Greg Maxey

Jay,

Now I am really baffled. Martyn stated that "Thanks but as Jay pointed
out, the Range part of the statement is not needed in this instance
(and I don't need it for my purposes either);..."

That would lead me to believe that there must be more to Martyn's code
than he has posted or his concept of not needed is different than mine.
Based on the code that he has shown, I don't see how it could possible
work.
 
J

Jay Freedman

Agreed. If Martyn has tried to run his code, he'll find that it stops with a
compile error.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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