From: electric on
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!
From: Jonathan West on

"electric" <electric(a)discussions.microsoft.com> wrote in message
news:D3BBCDCA-43D1-4F63-BF69-CB4A6882BF3E(a)microsoft.com...
>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.

--
Regards
Jonathan West

From: Fumei2 via OfficeKB.com on
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 West wrote:
>>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.
>

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1