HOWTO: Hyperlink in UserDocuments

ID: Q177238


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 6.0
  • Microsoft Visual Basic Control Creation, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Internet Explorer (Programming) versions 3.02, 4.0


SUMMARY

This article illustrates how to use the hyperlink object from a UserDocument (Visual Basic Active Document) to navigate to another document. It demonstrates the following:

  1. How to use the Hyperlink.NavigateTo.


  2. How to use Parent.LocationURL.


  3. How to construct an absolute URL.


  4. Why Parent.LocationName and App.Path are not suitable for this purpose.



MORE INFORMATION

From your UserDocument, you can jump to other UserDocuments that are in your application (Internal Jumping) or you can jump to other Web locations or documents (External Jumping). In both cases, you use the Hyperlink object's NavigateTo method and supply an absolute URL to where you want to jump. For example, you can do the following from your UserDocument code:


   Hyperlink.NavigateTo "http://www.microsoft.com" 

This will cause Internet Explorer to navigate to the specified location. You can also use this method for jumping to other UserDocuments in your application.

Because you have to specify an absolute URL, using this approach for other UserDocuments on a Web site requires extra work. You need to know the full URL from which the document (.vbd) was launched. You can get this information using Parent.LocationURL from your UserDocument code. The location can be either a Web URL or a UNC file location name.

NOTE: You should use the Parent.LocationURL property for retrieving the location of the UserDocument. Parent.LocationURL, unlike Parent.LocationName, works identically in both Internet Explorer 3 and Internet Explorer 4. In Internet Explorer 3, LocationName returns the complete path just as LocationURL does. In Internet Explorer 4, LocationName does not always return a complete path.

You should not use App.Path because it returns the location where your EXE or DLL exists locally, not the location from where the VBD was downloaded.

Constructing an Absolute URL

As mentioned before, you need an absolute URL for Hyperlink.NavigateTo method. This can be tricky for jumping to other UserDocuments in your application because the UserDocuments can reside on a local (or network) file or on a Web server. The following function takes the value returned by Parent.LocationURL, strips off the filename, and returns a path string. You can then append the name of any UserDocument that resides at the same base URL as the current document to the path string, and use the final result to call NavigateTo:

   Function PathFromURL(iURL As String) As String
   'Returns the path of the given URL. Works for both files and Web URLs
   'the final / or \ is also returned so you can append your own filename
   '
   'http://www.microsoft.com/default.asp -> http://www.microsoft.com/ 
   'c:\demos\doc1.vbd -> c:\demos\ 
       Dim I As Integer
       Dim Last As Integer
       Dim C As String * 1
       For I = Len(iURL) To 1 Step -1
           C = Mid$(iURL, I, 1)
           If C = "\" Or C = "/" Then
               Last = I
               Exit For
           End If
       Next
       If Last > 0 Then
           PathFromURL = Left$(iURL, Last)
       Else
           PathFromURL = ""
       End If
   End Function 

As an example, suppose you have a Doc1.VBD and a Doc2.VBD stored on a server at http://demo/hyperlink, but you do not want to hardcode the server name in your code so that these VBD files can be moved easily from one server location to another. To jump from Doc1.vbd to Doc2.vbd, you can use the above function as follows:

   Hyperlink.NavigateTo PathFromURL(Parent.LocationURL) & "doc2.vbd" 

Because PathFromURL can work on both URLs and UNC, this technique will work in both cases without changing code. Also, note that PathFromURL returns the final / or \, so you simply need to append your .vbd file name.

Additional query words: LocationName LocationURL Hyperlink kbInternet kbVBp500 kbVBp600 kbdsi
kbDSupport kbVBp kbIE302 kbIE400

Keywords :
Version : WINDOWS:3.02,4.0,5.0,6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: September 22, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.