Non-modal forms and multiple documents

E

electric

I have a non-modal form over my documents for convenience tools while I am
working with it. Essentially a constantly-running macro until the form is
closed. It is set to automatically run upon opening the file.

The macro/form is based off a template file so that if I send the document
to someone else, they won't get the macros, since they don't need it.

The problem I am coming across is that if I have documentA loaded, with the
macro running, and open up documentB for reference.

Instead of running a separate process of the macro, it takes over the macro
running with documentA and alters the form to the format of documentB (the
form is setup to recognize bookmarks in the document so it displays the
content of the document correctly).

What I am wondering, is if it is possible to either A) determine that the
macro is already running on another document and not allow it to AutoOpen, or
B) run the same macro from the same template in a separate instance (most
convenient option)?

Thanks for any input!
 
J

Jonathan West

electric said:
I have a non-modal form over my documents for convenience tools while I am
working with it. Essentially a constantly-running macro until the form is
closed. It is set to automatically run upon opening the file.

The macro/form is based off a template file so that if I send the document
to someone else, they won't get the macros, since they don't need it.

The problem I am coming across is that if I have documentA loaded, with
the
macro running, and open up documentB for reference.

Instead of running a separate process of the macro, it takes over the
macro
running with documentA and alters the form to the format of documentB (the
form is setup to recognize bookmarks in the document so it displays the
content of the document correctly).

What I am wondering, is if it is possible to either A) determine that the
macro is already running on another document and not allow it to AutoOpen,
or
B) run the same macro from the same template in a separate instance (most
convenient option)?

Thanks for any input!

You can ensure that separate instances of the form are opened by this
technique, like this

Dim oForm as myForm
Set oForm = New myForm

where myForm is the name of the form in your project.

make sure that for code in the form, references to controls within the form
are prefixed with the keyword Me, as in:

Me.Textbox1.Text = "Initial text"

The Me keyword means "The current instance of the form". (You can use Me in
Class modules as well, where it means just the same thing.)

For external code which references the form, you have to make sure that you
reference the object variable you have defined, like this

Dim oForm as myForm
Set oForm = New myForm
With oForm
.Caption = ActiveDocument.Name
.Show
End With

So in summary

- Within the form, use the Me keyword
- Outside the form, create an object variable to hold an instance of the
form, and always refer to that.
 
F

Fumei2 via OfficeKB.com

As an alternative:

have the userform execute (open) from neither DocumentA or DocumentB. This
makes it independent of either document.

Then make DocumentA or B document objects. That way the userform can execute
instructions, again, independently.

Jonathan said:
I have a non-modal form over my documents for convenience tools while I am
working with it. Essentially a constantly-running macro until the form is
[quoted text clipped - 20 lines]
Thanks for any input!

You can ensure that separate instances of the form are opened by this
technique, like this

Dim oForm as myForm
Set oForm = New myForm

where myForm is the name of the form in your project.

make sure that for code in the form, references to controls within the form
are prefixed with the keyword Me, as in:

Me.Textbox1.Text = "Initial text"

The Me keyword means "The current instance of the form". (You can use Me in
Class modules as well, where it means just the same thing.)

For external code which references the form, you have to make sure that you
reference the object variable you have defined, like this

Dim oForm as myForm
Set oForm = New myForm
With oForm
.Caption = ActiveDocument.Name
.Show
End With

So in summary

- Within the form, use the Me keyword
- Outside the form, create an object variable to hold an instance of the
form, and always refer to that.
 

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