Partager via


Visual Basic Concepts

Using the HyperLink Object with ActiveX Documents

If you target a Web browser as your container application, and you create more than one ActiveX document, you must use the HyperLink object to navigate between the different documents.

The Hyperlink object gives your documents access to ActiveX hyperlinking functionality. Using the properties and methods of the Hyperlink object, your ActiveX document can request a hyperlink-aware container, such as Microsoft Internet Explorer, to jump to a given URL.

The NavigateTo Method

The NavigateTo method executes a hyperlink jump to the target specified in the URL argument. The URL can be set to an HTML, Word, or Excel document as well as a .vbd file. For example, the following code navigates to the www.microsoft.com Web page:

Private Sub cmdGoTo_Click()
   UserDocument.Hyperlink.NavigateTo _
      "https://www.microsoft.com"
End Sub

To jump from one ActiveX document to another, you can use the same method, as shown.

Private GoNextDoc_Click()
   ' Assuming the next ActiveX document is named
   ' MyDoc2.vbd
      UserDocument.Hyperlink.NavigateTo _
      "file://c:\ActXDocs\MyDoc2.vbd"

Dynamically Constructing an Absolute Path

When you compile an ActiveX project with multiple ActiveX documents, Visual Basic creates the .vbd files in the same directory as the ActiveX .dll or .exe. However, if you move the .vbd files into another directory, you must give the NavigateTo method a fully qualified path for every .vbd file. However, because you cannot determine where a user will place the .vbd files, you must be able to dynamically create the absolute path.

The following code dynamically constructs the path of a .vbd file by parsing the LocationURL property of Internet Explorer which returns the absolute path of the document currently displayed by Internet Explorer. The code parses this path, and discards the name of the current .vbd file. The code then appends the name of a second ActiveX document to the remainder of the path.

Dim strPath As String    ' String to be parsed
Dim strAbsPath As String    ' Result of parsing
Dim intI As Integer      ' Character position counter

' Return the path of the current ActiveX document.
strPath = Trim$(UserDocument.Parent.LocationURL)

' Find the position of the last separator character.
For intI = Len(strPath) To 1 Step -1
   If Mid$(StrPath, intI, 1) = "/" Or _
      Mid$(StrPath, intI, 1) = "\" Then Exit For
Next intI

' Strip the name of the current .vbd file.
strAbsPath = Left$(StrPath, intI)

' Navigate to the second ActiveX document.
UserDocument.Hyperlink.NavigateTo _
   strAbsPath & "MyDoc2.vbd"

Automatically Starting a Browser

If the NavigateTo method is invoked from an ActiveX document contained in an application that supports the Hyperlink object (such as Internet Explorer), the same application instance will be used to "go" to the target document. If the application does not support hyperlinking (Microsoft Binder, for example), then an application that does (determined by the registry) will be started to handle the request. In other words, if the method is invoked from within an application that doesn't support hyperlinking, another that does will be started.

The NavigateTo method can be used to jump from one ActiveX document to another; in fact, it is the only way to start another document in a browser. Thus, if you create a suite of ActiveX documents, and your target container application is a browser that supports the Hyperlink object, you must use the NavigateTo method to open the next document. For example, the following code will cause the second (in a suite) document to appear in the Internet Explorer when the user clicks the cmdGoNext button on the first document:

Private Sub cmdGoNext_Click()
   ' The second ActiveX document's file is named _
   ' "ActiveDoc2.vbd"
   UserDocument.HyperLink.NavigateTo _
   "file://c:\ActiveX\ActiveDoc2.vbd"
End Sub

The NavigateTo method also includes a second argument, the FrameName argument, that specifies a particular frame in the document to jump to.

The GoBack and GoForward Methods

The GoBack and GoForward methods execute a jump forward or backward to the next document in the browser's history list. These methods only work with hosts that are Hyperlink aware (such as Internet Explorer 3.0 and later).

When implementing the GoForward or GoBack method, be sure to use error checking in case there is no document in the history list to jump to. An example is shown:

Private Sub cmdGoForward_Click()
   On Error GoTo noDocInHistory
   UserDocument.Hyperlink.GoForward
   Exit Sub
noDocInHistory:
   Resume Next
End Sub