where to put code

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

where to put code

Post by dasadler »

This may belong in the Word forum... if so, pls move it.

When I open the VBA editor in Word 2007, I see Modules and This Document - either of which allows me to enter code and seems to work fine. The only difference I see is that if I enter code in the Module container, all the macros seems to show up twice (in the list of macros) when I try to add them to QAT bar.


So what is the difference between these two containers and which should be used... and why?

See below for the area of which I ask...
You do not have the required permissions to view the files attached to this post.
Don

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: where to put code

Post by HansV »

ThisDocument is a special type of module. It is not really intended for 'standard' macros, although you can create them there.
ThisDocument is intended for event procedures of the Document object. If you select Document from the Object dropdown, you can select various events associated with the document from the Procedure dropdown.
x1374.png
The Document_Close event occurs each time this document, or a document based on this template (if it is a template) is closed.
The Document_New event occurs each time a new document is created based on this document or template.
The Document_Open event occurs each time this document, or a document based on this template (if it is a template) is opened.
There are other events, but these are the main ones. They will only work in ThisDocument, not in a standard module.

So: create 'standard' macros in a module under Modules, and event procedures for the document in ThisDocument.

Are you sure that macros in a 'standard' module are listed twice? Aren't they from different documents/templates?
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: where to put code

Post by dasadler »

well, actually, I am only talking about two macros... one being some code I got from the lounge that pastes unformatted text from the clipboard and the other a new doc macro. here they are:

Code: Select all

Sub PasteUnformatted()
  Selection.PasteSpecial DataType:=wdPasteText
End Sub

Sub FRQuote()
Documents.Add Template:="C:\Users\Don\Documents\DON TEMPLATES\xxxxx.dotx"
End Sub
This is looking at the code editor with no document open. Oh, previously, the paste macro was in this Document and the other was in the Module container. One macro in each.
You do not have the required permissions to view the files attached to this post.
Don

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: where to put code

Post by HansV »

I'd move both macros from ThisDocument to Module1, as mentioned in my previous reply.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: where to put code

Post by dasadler »

Thanks, I did move them. Is this the same in Excel where code and macros should be in module 1 instead of this workbook... or this worksheet?
Don

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: where to put code

Post by HansV »

The situation is similar in Excel:

ThisWorkbook is intended for workbook-level event procedures. For example, the Workbook_Open event occurs each time the workbook is opened. You can use this for example to make sure that a specific worksheet is active when the workbook is open.

Each worksheet has its own code module. This is intended for worksheet-level event procedures. For example, the Worksheet_Change event procedure occurs each time the user edits one or more cells. This can be used - among other things - to change the value of other cells without using a formula in those cells.

Standard modules are intended for ordinary macros and custom functions.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: where to put code

Post by dasadler »

Good to know. Thank you.
Don

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: where to put code

Post by HansV »

There are two other types of objects you can create in the Insert menu in the Visual Basic Editor:

A UserForm can be used to display and/or collect information in a user-friendly way, using text boxes, combo boxes, command buttons etc. Each userform has its own code module for code specifically associated with that userform, for example On Click event procedures for command buttons. Here is a picture of a very simple userform:
x1375.png
And here is what it looks like in the Visual Basic Editor, where you can design it:
x1376.png
A class module is a code module of a special type. One can do very powerful things with class modules, but they are rather technical, and most casual programmers don't use (or need) them. Technically speaking, ThisDocument, ThisWorkbook, worksheet modules and userform modules are all class modules, but again, that doesn't concern casual programmers.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans