ActiveDocument or ThisDocument?

K

kill_beast

When using the Document_Close and Document_Open sub procedures, should I use
the ActiveDocument object or the ThisDocument object to reference the
document?
 
J

Jay Freedman

When using the Document_Close and Document_Open sub procedures, should I use
the ActiveDocument object or the ThisDocument object to reference the
document?

Generally, use ActiveDocument.

The ThisDocument object refers to the document or, more often, the template that
contains the executing code. That's occasionally useful, but often not what's
intended.

To see the difference, save this code in a template; create a document based on
the template and save it; close and then reopen the document to see what the
message box displays.

Private Sub Document_Open()
Dim msg As String
msg = "ThisDocument is " & ThisDocument.Name
msg = msg & vbCr & "ActiveDocument is "
msg = msg & ActiveDocument.Name
MsgBox msg
End Sub
 

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