ACC97: How to Create Shortcuts from a Field of Hyperlinks
ID: Q181231
|
The information in this article applies to:
SUMMARY
Advanced: Requires expert coding, interoperability, and multi-user skills.
This article shows you how to create sample Visual Basic for Applications
code that creates Internet shortcuts from a Microsoft Access table that
contains a Hyperlink field with one or many records. The shortcuts are then
placed in a folder on your hard disk.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
To create a folder of Internet shortcuts from a field of hyperlinks,
follow these steps:
- In Windows Explorer, create the following folder:
C:\MyShortcuts
- Start Microsoft Access 97, open a database and create the following
table:
Table: tblHyperlinks
-----------------------
Field Name: fldLink
Data Type: Hyperlink
- Save the table as tblHyperlinks. When prompted to create a primary
key, click Yes.
- Switch to Datasheet View and type a valid Internet address into the
fldLink field of the first record.
- Right-click the address, point to Hyperlink, and type a descriptive
name in the Display Text box. Although the displaytext portion of the
Hyperlink data type is optional, you can hide the URL address and
display a more user-friendly name for the hyperlink if you specify
display text.
- Repeat steps 4 and 5 until you have added several records with
different Internet addresses.
- Create a new module and type or paste the following code:
Sub ProcessHyperlinkTable(strTableName As String, _
strFieldName As String)
' Declare variables.
Dim DBS As Database
Dim RST As Recordset
Dim strSQL As String
Dim strAddress As String
Dim strDisplayText As String
Dim strDirectoryPath As String
Dim varLink As Variant
' Set error trapping.
On Error GoTo Error_handler
' Assign the current database to the database variable.
Set DBS = CurrentDb
' Set the path to the existing directory
' where the shortcuts will be created.
' Note: the path should end with a backslash ("\").
strDirectoryPath = "c:\MyShortcuts\"
' Set the SQL string to return the field
' containing the hyperlinks from the
' table you have specified.
strSQL = "SELECT " & strFieldName _
& " FROM " & strTableName
' Open the Recordset.
Set RST = DBS.OpenRecordset(strSQL)
' Move to the first record.
RST.MoveFirst
' Loop until reaching the end of the table.
Do Until RST.EOF
' Check to see if the HomePage field contains a link.
If Not IsNull(RST!fldLink) Then
' Set varLink equal to the current Hypertext link field.
varLink = RST!fldLink
' Pull the Address from varLink.
strAddress = HyperlinkPart(varLink, acAddress)
' Pull the display text, if any, from varLink.
strDisplayText = HyperlinkPart(varLink, acDisplayText)
' If there is no display text, set strDisplayText to
' be the same as the URL.
If strDisplayText = "" Then
' If strAddress starts with "Http://",
' or "Ftp://", leave that out of strDisplayText.
If Left(strAddress, 7) = "Http://" Or _
Left(strAddress, 6) = "Ftp://" Then
strDisplayText = Right(strAddress, _
Len(strAddress) _
- (InStr(1, strAddress, "/") + 1))
Else
' Otherwise, simply set strDisplayText
' equal to strAddress.
strDisplayText = strAddress
End If
End If
' Pass the URL, the link name, and the directory path to
' the function.
Create_Hyperlink_Shortcut strAddress, strDisplayText, _
strDirectoryPath
End If
' Move to the next record.
RST.MoveNext
Loop
' Error handling section
ProcessHyperlinkTable_Exit:
DBS.Close
' Exit the Procedure.
Exit Sub
Error_handler:
MsgBox Err.Description
Resume ProcessHyperlinkTable_Exit
End Sub
Function Create_Hyperlink_Shortcut(strAddress As String, _
LinkFileName As String, LinkFilePath As String)
' Declare output file variable.
Dim fileNum As Integer
' Declare counter variable.
Dim i As Integer
' Set error trapping.
On Error GoTo Error_handler
' Set fileNum to the next available file number.
fileNum = FreeFile
' Create the new shortcut file.
Open LinkFilePath & LinkFileName & ".url" For Output _
As #fileNum
Print #fileNum, "[InternetShortcut]"
Print #fileNum, "URL=" & strAddress
Close #fileNum
' Exit the function when done.
Exit Function
' Error handling section
Error_handler:
' Trap for errors caused by a bad file name
' and prompt the user to correct it.
Select Case Err
' "Bad file name or number" error caused by
' invalid characters in file name.
Case 52
LinkFileName = InputBox("Cannot create file for " _
& strAddress & Chr(13) _
& "The Internet Shortcut Name shown below is invalid." _
& Chr(13) & Chr(13) _
& "It contains one or more of the following: " _
& "\ / : * ? " & """" & " < > |" & Chr(13) & Chr(13) _
& Chr(13) & "Please enter a valid Shortcut name.", _
"Invalid Shortcut Name", LinkFileName)
' "Path not found" error caused by slashes
' in the file name.
Case 76
LinkFileName = InputBox("Cannot create file for " _
& strAddress & Chr(13) & Chr(13) _
& "The shortcut name shown below is invalid. " _
& "It may contain one or more slashes." _
& Chr(13) & Chr(13) _
& "Please remove any slashes from the Shortcut name.", _
"Path not found", LinkFileName)
' If it is any other error, exit the function
' without creating the shortcut file.
Case Else
MsgBox "Error Number: " & Err & " while attempting to " _
& "create" & "shortcut for " & strAddress
Exit Function
End Select
' If the user entered a blank shortcut name in the error dialog,
' prompt for a correct one, allowing for three tries.
For i = 1 To 3
If Len(LinkFileName) = 0 Then
LinkFileName = InputBox("Cannot create file for " _
& strAddress & Chr(13) _
& "The Internet Shortcut Name is blank" _
& Chr(13) & Chr(13) _
& "Please enter a valid Shortcut name.", _
"Invalid Shortcut Name", LinkFileName)
End If
Next i
' If the user has still left the name blank, let the user know
' the shortcut will not be created and then exit the function.
If Len(LinkFileName) = 0 Then
MsgBox ("A shortcut will not be created for " _
& strAddress & ".")
Exit Function
End If
' Resume at the line that generated the error.
Resume
End Function
- On the Debug menu, click "Compile and Save All Modules".
- Save the Module as HyperlinkCode.
- To test the code, type the following line in the Debug window,
and then press ENTER:
ProcessHyperLinkTable "tblHyperlinks", "fldLink"
- Check the C:\MyShortcuts folder and notice the new Internet shortcuts.
- Double-click any Internet shortcut to run your browser and move
to the respective URL.
REFERENCES
For more information about the parts of a Hyperlink field, search the Help
Index for "hyperlinks, returning hyperlink information" or "hyperlinks,
addresses" and then choose the "About hyperlink addresses in hyperlink
fields and controls."
Additional query words:
inf favorites browser
Keywords : kbdta PgmHowto IntLink
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto
|