PDA

View Full Version : Solved: Using “ThisDocument” versus “ActiveDocument”



Ice-Tea-Jan
12-05-2010, 08:49 PM
I’ve successfully crafted and invoked numerous global templates against open documents via:
. . . recording a macro, editing it manually, assigning it to a toolbar, saving it as a template, and then loading it as a global template (add-in).

I know these global templates have the words “activedocument” in them as they are automatically recorded.

However, another poster thoughtfully advised me to be careful about using ““ThisDocument” versus “ActiveDocument” when dealing with templates.

I’m sure he explained it fully, but I’m still having a brain freeze on this concept. :dunno

Can somebody please “dumb it down” for me, or give me a Mickey Mouse example?

Thanks!
Janet

lucas
12-06-2010, 10:00 AM
It's really not that complicated. If you have code in a template and it says to do something to the active document......then that would be the clone or open document. If it says thisdocument it would act on itself which is not what you want.....

thisdocument refers to just what it says.....this document.

fumei
12-06-2010, 10:01 AM
Most of the time they are equivalent.

The only time I really use ThisDocument is if I am am making a reference to ActiveX objects in the document. They natively are called from ThisDocument.

Technically speaking ThisDocument is a code module. It is the code module that specifically, and explicitly refers to, well, THIS document.

ActiveDocument refers to the document that currently has focus; it is Active.

The reason I rarely use ThisDocument is because when I need to refer to specific document I always use document objects.

Recording macros always uses ActiveDocument. You can not record a macro actioning any document that is not Active. The reason for this is that recording macros ONLY uses Selection (the cursor). And the cursor is only "alive" in the active document.

"However, another poster thoughtfully advised me to be careful about using ““ThisDocument” versus “ActiveDocument” when dealing with templates. "

Perhaps if you posted what they stated we can clarify things more. It would be unusual for a template to refer to ThisDocument.

gmaxey
12-06-2010, 10:53 AM
I don't really know if this will clear the fog or further muddy the water, but I will reply anyway.

ThisDocument typically refers to the document (or more frequently) the template that contains the executing code. ActiveDocument refers to the document that has the active focus. If you open a new instance of Word and create a new document based on one of your templates then that document is at that instance the "ActiveDocument." It may or may not be "ThisDocument."

Maybe this will help. Create a new template (call it say Demo Template.dot). Add the following code to the "ThisDocument" module:


Private Sub Document_Open()
MsgBox "ThisDocument refers to the template: " & ThisDocument.Name & vbCr & "ActiveDocument refers to the document: " & ActiveDocument.Name
End Sub

Create a new document based on this template. Save it (call it say Demo Document.doc). Close and reopen the file.

As you should see, ActiveDocument will refer to "Demo Document" while "ThisDocument" will refer to Demo Template."




I’ve successfully crafted and invoked numerous global templates against open documents via:
. . . recording a macro, editing it manually, assigning it to a toolbar, saving it as a template, and then loading it as a global template (add-in).


I know these global templates have the words “activedocument” in them as they are automatically recorded.

However, another poster thoughtfully advised me to be careful about using ““ThisDocument” versus “ActiveDocument” when dealing with templates.

I’m sure he explained it fully, but I’m still having a brain freeze on this concept. :dunno

Can somebody please “dumb it down” for me, or give me a Mickey Mouse example?

Thanks!
Janet

fumei
12-06-2010, 11:42 AM
This is not quite accurate. Take a look at the attached demo. The code (click Who What_Am I on toolbar), is very similar. Identical in fact, with the additional explicit statement on what is the attached template:
Option Explicit

Sub WhoWhat_AM_I()
MsgBox "ThisDocument refers to the template: " & _
ThisDocument.Name & vbCr & _
"ActiveDocument refers to the document: " & _
ActiveDocument.Name & _
vbCrLf & vbCrLf & _
"Attached Template: " & ActiveDocument.AttachedTemplate
End Sub
As you can see, ThisDocument is NOT the template.

fumei
12-06-2010, 11:42 AM
Ummm, you will have to download to really see it. Otherwise it just sees the PHP.

fumei
12-06-2010, 11:47 AM
The key thing here is:

"ThisDocument typically refers to the document (or more frequently) the template that contains the executing code. "

More accurately it is the code module that holds the executing code.

The reason Greg's code returns the template as ThisDocument is because the executing code module is in the template. Document_Open is the template module. Therefore ThisDocument is the template.

In my attached, the same messagebox code is executing from the document code module, therefore ThisDocument.Name is the document.

So ThisDocument does not "frequently" refers to the document/template that contains the executing code - it always refers to the project that contains the executing code.

gmaxey
12-06-2010, 12:13 PM
Gerry,

Yes including "typically" in my statement was misleading. Given another chance I would have said:

"ThisDocument refers to the document (or more frequently the template) that contains the executing code."

I think that both our examples together illustrates that.

I can live with "ThisDocument always refers to the project (Document or Template) that contains the executing code."

I would striked "code module" from the discussion ;-).

fumei
12-06-2010, 12:17 PM
Agreed.

fumei
12-06-2010, 12:20 PM
Anyway, Janet, for the most part you can ignore Greg and I having an arcane discussion, as well as the difference between ThisDocument and ActiveDocument.

In 99.5% of the time, any code you are dealing with will be quite happy using ActiveDocument. And as I stated, if you need to refer to another document, use document objects.

Properly coded, procedures (macros) in a template (global or otherwise) should execute correctly without any need for using ThisDocument.

Paul_Hossler
12-07-2010, 06:30 AM
Janet --



(From the other thread) --I might not be explaining this clearly, Gerry's a much better teacher than I
Paul


See, I told you he was :thumb

Paul

Paul_Hossler
12-07-2010, 06:47 AM
Properly coded, procedures (macros) in a template (global or otherwise) should execute correctly without any need for using ThisDocument.

Agreed, but I think that sometimes there might be objects in the macro executing document/template where you would want explicitly identify which parent source you are refering to (to which you are referring???)

For example, if my Global template (.DOTX) contains the macro and has a style defined that I want to apply to a document (.DOCX)



Sub MacroInTheTemplate()
ActiveDocument.Styles("heading 1").Font = ThisDocument.Styles("normal").Font

End Sub


Correct?

Paul

fumei
12-08-2010, 10:27 AM
True. Yes, that is correct. Global templates do not use styles (they are code containers). That is, you can not use a style directly from a global. However, a global template itself can of course have styles. And yes, to be able to reference them, you have to use ThisDocument (thus pointing to their source).

But...this is not a preferred way to deal with Styles. Why would you do this?

However, yes that is technically correct.

Ice-Tea-Jan
12-09-2010, 06:27 PM
Many thanks to Steve, Gerry, Greg & Paul for the prompt replies and examples.

Your input and expertise is tremendously valuable to newbies (like myself). :thumb

Your pinpoint answers and "lessons" could not easily be received in a college, nor in books. And best of all, I can post and learn at my own pace, and at convenient times. Extraordinary!!!:cloud9:

I think I can scamper away keeping ActiveDocument in my codes, and if they do not execute properly; then reviewing the use of ThisDocument might be a consideration.

I'm marking this thread solved.

Thanks again,
Janet