Computer geek, and self-appointed know-it-all, Westley Annis answers all those hard 
questions about anything related to computers and technology, as well as business and 
political questions.

Create a hyperlink in Word from a drop down list

Question asked on July 30, 2005 12:40 AM :: :: Comments (0) :: TrackBacks (1)

Creating a hyperlink in Word through Visual Basic for Applications (VBA), the programming language for Word, involves two three steps. Inserting the text to be dislayed as the hyperlink, selecting that text, attaching a URL to the text. Sounds complicated, but it's not.

The extra step that you are looking, creating the hyperlink based on a selection from a drop down list is just as easy.

First thing to do, enter the code into the VBA editor. Click Tools->Macro->Macros. Type URLLink into the Macro Name field. Select Document1 or whatever the name of your current document is to store the macro inside the document itself. Click Create.

Word will open the VBA editor and enter the beginning and ending statements of the macro along with comments reflecting your name and the current date. Enter the following text, minus the Sub and End Sub commands. See below for an explanation of how the code works.

Sub UpdateURL()
'
' UpdateURL Macro
' Macro created 7/28/2005 by Westley Annis
' http://www.AskWestley.com
'
Dim strField As String, strURL As String
Dim bmrkURL As Bookmark
    strField = ActiveDocument.FormFields("Dropdown1").Result
    strURL = "https://www.companyurl.com/" & strField & "/Metrics/Forms/AllItems.aspx"
    ActiveDocument.Unprotect
    ActiveDocument.Bookmarks("URLLink").Range.Select
    Selection.Expand unit:=wdSentence
    Selection.TypeText Text:=strURL
    Selection.Expand unit:=wdSentence
    ActiveDocument.Hyperlinks.Add Address:=strURL, Anchor:=Selection.Range
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Exit out of the VBA editor and back into Word. Add the following elements to your document.

A dropdown FormField (right-click on any toolbar to show the list of toolbars available and select Forms if not already checked - click the dropdown button then click inside the document to insert it). Right-click on the dropdown formfield and select Properties. Add the text of each option you would like displayed inside the dropdown box and enter Dropdown1 in the Bookmark field.

Create a MacroButton to execute the URLLink macro when clicked by clicking Insert->Field, select MacroButton from the Field names list, enter "Click here to create hyperlink" in the Display text field, and select URLLink from the Macro name field. Click Ok.

Create an empty paragraph to hold the URL and insert a bookmark by clicking Insert->Bookmark, type URLLink in the Bookmark name field, click the Add button.

Protect the document for forms by clicking the Protect Form button on the Forms toolbar. You're all set.

Select an element from the drop down list and click on your macro button, your hyperlink will be entered.

For those that don't understand VBA code, here's a play-by-play account of what's happening.

The two lines that begin with Dim are creating storage locations to hold the element that was selected, the URL that gets built, and a reference to the bookmark where the URL will be inserted. The next line gets the actual element that was select while the following line builds the URL by adding the necessary information in front and behind the selected element.

Next the document is unprotected so that text can be entered. The cursor is moved to the URLLink bookmark and the text entered. The just entered text is then selected and the hyperlink is attached to it. Finally the document is reprotected for forms with the NoReset flag so that any entered information is not lost.

Categories

1 TrackBacks

Listed below are links to blogs that reference this entry: Create a hyperlink in Word from a drop down list.

TrackBack URL for this entry: http://www.askwestley.com/cgi-sys/cgiwrap/wannis/managed-mt/mt-tb.cgi/7

» AdultFriendFinder from AdultFriendFinder

I really enjoyed your topic and plan to share your blog with my friends! thanks for taking the time out to write your blog! If you want to learn how to get 100% free access to Adult Friend Finder check out my blog! Read More

Leave a comment




Ask Westley your question on technology, business, or politics!
RDF XML
Add to My Yahoo!
Subscribe in 
NewsGator Online
Feedburner
GeoURL
Search



All Categories
Powered by
Movable Type 4.1
© 2005-2009 by Westley Annis. All Rights Reserved.

Valid XHTML 1.0!