"Argument not optional" error

G

Guest

I have a very simple user box with the following code:
Private Sub CmdOK_Click()
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("ClientNumber").Range
rng.InsertAfter.txtClient

Set rng = ActiveDocument.Bookmarks("MatterNumber").Range
rng.InsertAfter.txtMatter
End Sub

The user simply has to enter a client and matter number
which the macro should place after the appropriate
bookmarks. But when the macro runs I get:
"Argument not optional" and it highlights
the "InsertAfter" command. Not sure what to do. Thanks
for your help.
 
J

Jay Freedman

Change the two InsertAfter lines to these:

rng.InsertAfter txtClient.Text
rng.InsertAfter txtMatter.Text

There should not be a period between InsertAfter and txt... Instead there
must be a space, which tells VBA that the txt... expression is the
"argument" -- that is, the string that should be inserted. I've assumed here
that txtClient and txtMatter are the names of textboxes on the userform, so
I added the .Text part to the end -- that represents the content of each
textbox.
 

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