Public Func in DocObj Code

S

Sabaka

A call to a Public function in the code for a userform object results in an
error message if the "Public" function is in the code for a document object,
but not if the Public function is in a standard module. The error message is
that the function is not defined.

This is puzzling because I thought that declaring a function or variable as
Public made it visible everywhere in a project. Apparently, this is true if
the function or variable is declared as Public in a standard module, but it
is not necessarily true in other contexts.

My question is do I understand this correctly and, if so why is this so? Is
there something special about the code for a document? I'm guessing that the
code for a userform has the same property.
 
S

Sabaka

Thank you Jonathan.

I read the link you suggested and also one I found at
http://msdn.microsoft.com/library/d...s/vbcon98/html/vbconworkingwithprocedures.asp

I quote from the article at the link:

"Public procedures in other modules can be called from anywhere in the
project. You might need to specify the module that contains the procedure
you're calling. The techniques for doing this vary, depending on whether the
procedure is located in a form, class, or standard module.

Procedures in Forms
All calls from outside the form module must point to the form module
containing the procedure. If a procedure named SomeSub is in a form module
called Form1, then you can call the procedure in Form1 by using this
statement:

Call Form1.SomeSub(arguments)"

In my code, I am not calling a procedure in a form. I am calling a
procedure in a document (from a form). Previously, I wrote:

Call Procedure(arguments)

This gave me an error message. But when I changed it to this:

Call ThisDocment.Procedure(arguments)

it works fine.

So sometimes you need to both declare a function as Public and name the
module where it resides when you call it.
 
J

Jonathan West

Hi Sabaka,

ThisDocument is a class, but with a default instance in much the same way
that userforms have default instances. Therefore, public procedures in the
ThisDocument class need to be called in the same way as public procedures in
UserForm modules
 

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