Your Active Document application might need to behave differently depending on
the type of container in which it runs.
For example, some containers (such as Internet Explorer) will be aware of the
Internet and will handle Internet addresses properly while other containers such
as Office Binder (at least as of Office 97) won't know URL from Earl. An Active
Document that finds itself sited in an Internet-aware application can use its
Hyperlink object to navigate to other documents. If the container is not aware
of the Internet, then the Active Document must use other techniques specific to
that container. (See the section "Navigating Between Documents
in the Container Application.")
This difference in behavior will, of course, translate into different program
code. Because you can't know ahead of time what type of container application will
use your Active Document, you must programmatically detect your container's type and then
write different code for each possible container type.
How to detect and react to the type of your UserDocument's container
is discussed in the following section.
Detecting the Type of
Container with the TypeName Function and UserDocument.Parent
The UserDocument's Parent property is an object that points to the container
where the current instance of the Active Document is sited.
You can use this fact to get information about the container, including its application
type. Recall that the TypeName function takes the name of a variable as its argument and returns
a string telling the data type of the variable. Therefore, the following line of code in a UserDocument
will display the container type for all to see:
MsgBox "Container is "
The example in Listing 14.1 will detect the type of container, store the container
type in a String variable, and then take different actions depending on the contents of the String:
DETECTING THE TYPE OF CONTAINER
Dim strContainerType as String
strContainerType = Ucase$(UserDocument.Parent)
'If container is Internet Explorer:
If Instr(strContainerType, "WEBBROWSER")
<> 0 Then
'behave one way
'or if it's Office
ElseIf strContainerType = "SECTION"
'or if it's VB:
ElseIf strContainerType = "WINDOW"
'behave a third way
'or if the container
type is unknown
'behave in a very generic
Note that the string for the container type (as specified in the Document's Parent
object) is not necessarily an intuitively obvious name for the container application.
For further discussion of this topic, see the section in this chapter titled
"Writing an Application to Handle Different Containers'
<< Previous | Contents
| Next >>