Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macros - Pasting into a hyperlink 1

Status
Not open for further replies.

NiceButDim

Programmer
Feb 12, 2002
154
GB
Hopefully I’m in the right forum for this question; I need a macro the will convert a line of text into a hyperlink.
I thought it would be a simple matter of recording a macro. However when I run my macro it literally pastes the text of the line that I used when recording the macro.

I put the cursor at the start of ‘Line One’ and recorded my macro. The macro looks as follows;
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"Line%20One", SubAddress:="", ScreenTip:="", TextToDisplay:="Line One"

Anyone know how I could change the ‘Address’ & ‘TextToDisplay’ elements to display whats now in the clipboard rather than the literals ‘Line One’ every time?

Thanks.


john.
 
Hi PencilBiter,

All you need to do is use the Selection instead of the literals. You might also like to set the style, something like this. With the cursor on the line to change ..

[blue]
Code:
Selection.Expand wdLine
Selection.Style = ActiveDocument.Styles("Hyperlink")
Selection.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        Selection, SubAddress:="", ScreenTip:="", TextToDisplay:=Selection
[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
I am confused.

"I put the cursor at the start of ‘Line One’ [/i}.

What does that mean? Do you mean a line number of 1, or is there text that states "Line One"?

When you make a hyperlink, you must select a target. This becomes the Address. The "text to display" is part of the hyperlink dialog. You can accept the original text (IF you have selected text - which of course you do NOT have to), OR enter new text. In the latter case, if you selected text in the first place, that text is REPLACED by the "text to display".

You can make a hyperlink on a blank character space, and insert new text as the "text to display", or not. If you choose nothing as text to display, then Word uses the target text itself as the text to display.

Could you run this by again, step by step exactly what you want to happen?

Gerry
 
Belated thanks for the replies.

fumei; you're right, it was a bit confused. I didn't explain what I was after very well.

Tony; Thanks a lot, just what i was looking for. Looks so obvious, but I spent an hour trying to work it out without success.
Thanks again.

john.
 
What Tony is doing is:

selecting the line.
making the line contents the Hyperlink style
making the selection contents the target address
making the selection contents = the text to display

I do not really see this as much different from what you asked for in your firsd post.
I need a macro the will convert a line of text into a hyperlink{/quote]

So Tony's answer is good. It code does that, in the steps stated above.

Again, if this is not doing what you want, please be clear, Walk us through, step-by-step if you have to (not a bad idea), and let us know PRECISELY what you want to do. You state that you worked with Tony's suggestion for an hour without success. OK, then it seems likely that you want to do something not stated in your posts. Please post some clear detail.


Gerry
 
I think that you’re getting a little confused here.

After re-reading the posts, I think the difference if fairly obvious, my version;

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"Line%20One", SubAddress:="", ScreenTip:="", TextToDisplay:="Line One"

will set the new hyperlink’s address and the text displayed values to the literals “Line One” every time the macro is run. Whereas Tony’s version;

Selection.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
Selection, SubAddress:="", ScreenTip:="", TextToDisplay:=Selection

Sets those same 2 values to whatever is currently in the clipboard.

I was polite and grateful in my response. In your final post you appear to be somewhat irritated. I can’t understand why, it was unwarranted as I had ALREADY APOLOGISED. Whilst the number of posts I have made over the years in only a fraction of what you have made, I have nevertheless posted enough to be fully aware of what it required. If you care to take the time to look at some of my previous posts I am sure that this will be confirmed to you.

Please move on


john.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top