
Create a hyperlink in Word from a drop down listHow can I create a hyperlink in Word based off of a Dropdown field? 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.
Category is Microsoft Office
(Article #7)
Comments
This site is incredible!
This site is incredible! It's amazing the questions you get asked and you always come up
with the answer. I've just got to help you stay cool by buying you a
Snoball!
I do have a comment, now that you mention it!
|
|