PDA

View Full Version : Hyperlinks copy-paste in Word Macros



okvia
09-12-2013, 09:50 AM
Hello,

I'm trying to find a way for a macro to copy hyperlinks from one name to another in a Word document. What I have is a list of names:

Name1
LongName1
Name2
LongLongName2
...

Only short names have hyperlinks and I need to copy these hyperlinks and add them to long names. So Name1 hyperlink needs to be added to LongName1, Name2 hyperlink needs to be added to LongLongName2 etc.

I'm new to Word VBA, so I tried to record a macro that will do this. Unfortunately, it copies the exact name of one hyperlink and keeps pasting it. How do I make it to copy the hyperlink of a word I want (of Name1), add it to words below (LongName1), and then copy a new link (of Name2) and so on? Would "Text ToDisplay" also cause it posting the same text?

'
Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
Selection.Range.Hyperlinks(1).Delete
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"Name1Hyperlink" _
, SubAddress:="", ScreenTip:="Name1", TextToDisplay:="", Target:= _
"_blank"
Selection.Collapse Direction:=wdCollapseEnd

Selection.MoveDown Unit:=wdLine, Count:=1

Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend

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



Also, is there a way for a macro to recognize the end of a line when highlighing the text?
Right now, it will only count the exact number of words in the name that I indicate (3). But the number of words in Long Names vary and I need all words of a particular name to be highlighted to add a hyperlink. This is the line:

Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend


Thank you!

mrojas
09-12-2013, 01:53 PM
You may need to create a couple of variables (Dim strLinkShort strLinkLong) and store in strLinkShort the value in Name1 and LongName into strLinkLong, then concatenate variables and shove this into new word.
NewWord=strLinkShort & strLinkLong (this is pseudo code)

okvia
09-16-2013, 08:06 AM
You may need to create a couple of variables (Dim strLinkShort strLinkLong) and store in strLinkShort the value in Name1 and LongName into strLinkLong, then concatenate variables and shove this into new word.
NewWord=strLinkShort & strLinkLong (this is pseudo code)


Could you be more specific please?
So I'd Dim strLinkShort as String, but how would I make strLinkShort to become the hyperlink under Name1? And then add this hyperlink to the NewWord?

mrojas
09-16-2013, 09:40 AM
After creating the variable, simply use the equal sign to store a value in it. It would look something simiilar to this:
strLinkShort=Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
This statement should be right after you've shifted focus to your hyperlink.

okvia
09-16-2013, 01:15 PM
After creating the variable, simply use the equal sign to store a value in it. It would look something simiilar to this:
strLinkShort=Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
This statement should be right after you've shifted focus to your hyperlink.

The line (strLinkShort=Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select) gives "Compile Error: expected function or variable".

fumei
09-16-2013, 01:43 PM
Remove the Select, like this:

strLinkShort=Selection.Range.Hyperlinks(1).Range.Fields(1).Result

okvia
09-17-2013, 07:17 AM
Remove the Select, like this:

strLinkShort=Selection.Range.Hyperlinks(1).Range.Fields(1).Result

That worked, thank you!

The problem is now that it copied the Name, not the hyperlink associated with it. I did made the program to paste it nicely as a hyperlink later :)

Is there a way to indicate that I need a hyperlink before assigning "strLinkShort"?
Because all the previous lines or their variations that I used either don't help like "Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select" or give errors like "Bad parameter" for this one "ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range"..

fumei
09-17-2013, 07:55 PM
Please post your entire code as it stands now. And restate what you are trying to do, as I am a little confused. When you say "not the hyperlink associated with it", do you mean the Address? I am also not sure what it is you are actually selecting.

okvia
09-18-2013, 06:33 AM
Sure. I'm trying to copy hyperlinks (I mean Web Addresses) from one name to another in a Word document. What I have is a list of names:

Name1
LongName1
Name2
LongLongName2
...

Only short names have hyperlinks (with Web Addresses) and I need to copy these hyperlinks' Addresses and add them to long names. So Name1 hyperlink Address needs to be added to LongName1, Name2 hyperlink Address needs to be added to LongLongName2 etc.

The code I have so far for copy-pasting part:

Sub Macro()

Dim strLinkShort As String

Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend

'(the line above is just for highlighting, with or without it, the macro copies "Name1", not the hyperlynk address)

Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select

strLinkShort = Selection.Range.Hyperlinks(1).Range.Fields(1).Result

Selection.Collapse Direction:=wdCollapseEnd

Selection.MoveRight Unit:=wdCharacter, Count:=1

Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=strLinkShort, SubAddress:="", ScreenTip:=""

End Sub

fumei
09-18-2013, 04:36 PM
Again, please describe exactly what you are doing. WHAT are you selecting. You are hard coding movements of Selection. Does that mean each time you want to run this code you are moving the cursor to some new location? Your use of Selection is confusing.

In any case, if you want to get the ADDRESS of the hyperlink in the Selection, do NOT select, nor get the Result. Get the Address.

strLinkShort = Selection.Range.Hyperlinks(1).Address

Sub Macro()
Dim strLinkShort As String

Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend

strLinkShort = Selection.Range.Hyperlinks(1).Address
With Selection
.Collapse Direction:=wdCollapseEnd
.MoveRight Unit:=wdCharacter, Count:=1
.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend
End With
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=strLinkShort, SubAddress:="", ScreenTip:=""
End Sub

okvia
09-19-2013, 08:24 AM
Thank you! I need to copy-paste Addresses, so this works perfectly!

I didn't think it would be confusing when I talked about hyperlinks I meant addresses associated with those hyperlinks. I'm new to Word macros so I tried to automatize at least copy-pasting step.

I need to use cursor because each LongName (to which hyperlinks' Addresses are pasted) has different number of words. All of these words need to get highlighted before pasting so that the hyperlink will work for all of words in this LongName. I couldn't find a way for a macro to recognize the end of a line when highlighing the text so I use several macros. In the macro above, it will only highlight exactly 3 words in the name:
Selection.MoveRight Unit:=wdWord, Count:=3, Extend:=wdExtend

fumei
09-19-2013, 04:55 PM
All of these words need to get highlighted before pasting so that the hyperlink will work for all of words in this LongName.

Huh? I fail to see why there is any need for highlighting at all. How does highlighting make any difference?


each LongName (to which hyperlinks' Addresses are pasted) has different number of words If there is different number of words, how does hard coding a specific number of words help?

okvia
09-20-2013, 06:30 AM
Huh? I fail to see why there is any need for highlighting at all. How does highlighting make any difference?

? When you create a hyperlink & paste an Address into it, and you cursor is not highlighting all the words, the hyperlink&Address will only get pasted to the word where your cursor is. All the words next to it will be left without hyperlink/Address. I need ALL of the words in the LongName to have hyperlink/address.


If there is different number of words, how does hard coding a specific number of words help?

- I have a couple of types of LongNames so have a couple of macros assigned to keyboard. Definitely easier and less prone to errors than doing everything manually.

fumei
09-20-2013, 02:54 PM
Oh, we have basic confusion of tems. Highlighting to me means exactly that, the text is highlighted. You mean SELECTING.

As for the number of words, are you saying that you have different subroutines (macros) doing the same thing with only ONE thing different...the number of words selected? This seems very inefficient.

Let me see if I have this:

1. you manually move the cursor into NameX, then execute macro_whatever (the # of words to select)
2. manually move to the next NameX and then execute the appropriate macro (the # of words to select)
etc.