Create Hyperlink - Paste Address

B

Bod

I want to make a macro that turns text into a hyperlink:
First, select the text which is to become the hyperlink (e.g. a doc number
like "602-fm03"). Then run the macro.

The macro should:
Copy selected text.
Create hyperlink.
Address: "http://twnzms/?document=" (this is our server's search function)
and the copied text pasted thereafter.
So the Text to display is the original doc number;
The Address is http://twnzms/?document=602-fm03.

I can get it to put the h-link in, but I can't get it to paste the copied
doc number after the Address stem. If it just stopped with the dialogue box
open, I could paste the copied text, but it would be better if it could type
the first bit AND paste the copied text.
 
J

Jay Freedman

I want to make a macro that turns text into a hyperlink:
First, select the text which is to become the hyperlink (e.g. a doc number
like "602-fm03"). Then run the macro.

The macro should:
Copy selected text.
Create hyperlink.
Address: "http://twnzms/?document=" (this is our server's search function)
and the copied text pasted thereafter.
So the Text to display is the original doc number;
The Address is http://twnzms/?document=602-fm03.

I can get it to put the h-link in, but I can't get it to paste the copied
doc number after the Address stem. If it just stopped with the dialogue box
open, I could paste the copied text, but it would be better if it could type
the first bit AND paste the copied text.

Here's how I would do it:

Sub HLinkDoc()
Dim sDocNm As String
Const sURL = "http://twnzms/?document="

If Selection.Type = wdSelectionNormal Then
sDocNm = Trim(Selection.Text)
ActiveDocument.Hyperlinks.Add _
Anchor:=Selection.Range, _
Address:=sURL & sDocNm
End If
End Sub

The If statement checks to be sure some text is selected (and it isn't a picture
or a table or some other non-text item). By using Selection.Range as the anchor
(the location in the document) of the hyperlink, the display text automatically
becomes the same as the original text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top