Object Required error

J

Jason Logue

Hi, I am trying to run the following code and get an
object required error at the noted line. The following
code allows the user to enter a bookmark number into an
input box and the corresponding bookmark will be inserted
wherever the cursor is located.

Would anyone be able to tell me why I keep getting this
error?
TIA,
Jason


Public Sub PutBookmarkTextInDocument()
Dim strUserResponse As String
Dim strBookmarkName As String



strUserResponse = InputBox("Enter Exception
Number.", "Exception Number")

If VerifyUserInput(strUserResponse) Then

'We know we have a valid bookmark number. So put
it in the document
strBookmarkName = "Chapter" & Replace
(strUserResponse, ".", "_")

<<<Below is the line location where I get the error.>>>

'Put the bookmark text at the cursor
Selection.TypeText (source.Bookmarks
(strBookmarkName).Range)

End If

End Sub
 
J

Jay Freedman

Hi, Jason,

What is "source"? I suspect it's a Document object that you declared and
initialized in some other subroutine. Unless it's a global variable (that
is, one declared in the Declarations section of the module, outside any
subroutine), it's "out of scope" in the PutBookmarkTextInDocument
subroutine -- meaning that as far as VBA is concerned, it doesn't exist in
this routine.

To fix this, you can make "source" a global variable by moving its
declaration, or you can pass it as a parameter to PutBookmarkTextInDocument.
Global variables aren't too bad in very small projects, but they're
generally bad programming practice as the project size increases because
they're too easily modified by rogue subroutines.
 
J

Jason Logue

Hey, thanks, Jay. I got it figured out. I forgot to set
the source for that module. There is a global source
(it's a small program), but I hadn't declared it in that
subroutine.
 

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