vba in document vs module

E

ebct

VBA using the editor lets you choose to put your macro either in the
document itself or in a module linked to the document. Why would one
pick one over the other?
 
H

Helmut Weber

Hi,

I am no good at this, but it seems,
module vs. document isn't the right concept.
A macro would be in a module.
A sub can be in a form, too.
A sub not in a form, maybe called a macro.
In any way, sub or macro, both are in a project.

A project can be linked to another project.
But not to a module there in a straightforward way.

I wish someone would write an article on all that.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Charles Kenyon

A macro can be in the "This_document" object or in a module. Is this what
you are asking about? Macros should seldom be in documents.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
E

ebct

Hi. Yes, that is exactly what I was wondering. Since the VBA editor
allows you to place macros in the "This Document" object, how is that
different than putting it into a module and what are the pros and cons
for each.

A prior response suggesting putting the macro in a template. That can
be an issue because the template is not always available if a document
is moved around.

Thank you,

Irwin
 
C

Charles Kenyon

I'm not sure, but suspect that you cannot use the Organizer to move
procedures in This_Document. Also, they may not show up in the macro list in
Word; again, haven't checked this. You are welcome to check on both and
report back.

Macros in documents are problematic because of macro security. Many
environments have security set to high, in which case the macros are as good
as not there. If set to medium, you still get worried questions from users.
You can digitally sign macros in documents. However, you can't clear macro
security simply by putting a document in a trusted location; you can with
templates.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
K

Karl E. Peterson

VBA using the editor lets you choose to put your macro either in the
document itself or in a module linked to the document. Why would one
pick one over the other?

I compartmentalize pretty strictly. If it's a coded solution, it goes in a
module (standard or class). If it's related to native UI events, whether
from document itself or objects embedded within it, it goes in ThisDocument.
I know "related to" is of the weasel-word variety, but I do see some leeway
there. Generic event handlers should likely be moved out of specific object
modules, while highly specific ones can stay. Clear as mud?
 
E

ebct

Thanks. Can you give me an example of what you mean by a native UI
event, so I understand just what you mean?

Thanks,
Irwin
 
K

Karl E. Peterson

Thanks. Can you give me an example of what you mean by a native UI
event, so I understand just what you mean?

Well, if you open up the ThisDocument module, you'll see in the upper-left a
dropdown that contains "(General)" and "Document" -- select the latter. Now
the upper-right dropdown offers you three native events: Close, New, and
Open. These events allow you to react to when that document is closed,
created (obviously, this would be template-based code, in this case), or
opened. If you were to add controls directly to the document, events for
those would also be fired within this module.

The use of "native" implies non-native as well, doesn't it? By that, I
really meant events generated by things you add to the project, such as
userforms or controls hosted by userforms. The code for those should
typically be contained within the respective userform module. Again,
generic event handlers (such as one that would select all the text within a
textbox) can be moved into more generic/standard code modules.

Then there are events raised by objects other than ThisDocument, such as
Application. Those would most typically be coded up within class modules,
though I suppose it could be arguable in some rare cases that they more
appropriately belonged within the document.
 

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