PDA

View Full Version : Solved: Find Text and Copy Paste



Krishna Kumar
04-04-2011, 12:55 AM
I have some data in my word file. The data looks like the following

Some Text [Hyperlink]
some text goes here

Source
some text goes here
some text goes here
Author(s)
some text goes here
some text goes here
Author Affiliation
some text goes here
some text goes here
Title
some text goes here
some text goes here
Language
some text goes here
Publication Year
Year
(Unique Word)

Here it repeats the above


Now I want to find the Hyperlink word and paste after Source. Now after the macro run the format would be..

Some Text
some text goes here

Source [Hyperlink]
some text goes here
some text goes here
Author(s)
some text goes here
some text goes here
Author Affiliation
some text goes here
some text goes here
Title
some text goes here
some text goes here
Language
some text goes here
Publication Year
Year
(Unique Word)

Any help on this would be greatly appreciated

TIA

Kris

macropod
04-04-2011, 04:03 PM
Hi Kris,

Is the 'Hyperlink' a true hyperlink (ie blue & underlined and clicking causes Word to follow it) or just a text string? Does it have a particular format? Do the square brackets exist as shown?

For the literal example you've posted, a wildcard Find/Replace would do the job, where:
Find = ( \[Hyperlink\])(*Source)
Replace = \2\1
However, I think you need something more spohisticated than that.

Krishna Kumar
04-04-2011, 07:53 PM
Hi Paul,

Thanks for the reply. To answer the questions

1. Is the 'Hyperlink' a true hyperlink (ie blue & underlined and clicking causes Word to follow it? Yes

2. Do the square brackets exist as shown? No

Kris

macropod
04-05-2011, 06:23 AM
Hi Kris,

Try:
Sub HyperlinkMover()
Dim fRng As Range, hRng As Range, cRng As Range
Set fRng = ActiveDocument.Range
ActiveWindow.View.ShowFieldCodes = False
With fRng
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = ""
.Replacement.Text = ""
.Style = "Hyperlink"
Do While .Execute
With fRng.Duplicate
Set hRng = fRng.Duplicate
hRng.MoveStart wdCharacter, -1
hRng.Collapse (wdCollapseStart)
Set cRng = fRng.Duplicate
With cRng
.Collapse (wdCollapseEnd)
.Move wdCharacter, 1
While InStr(.Text, "Source") = 0
.MoveEnd 1
Wend
fRng.Duplicate.End = cRng.End
.Cut
End With
hRng.Paste
.Collapse (wdCollapseEnd)
End With
Loop
End With
End With
End Sub

Krishna Kumar
04-05-2011, 08:25 PM
Hi Paul,

Thanks for the code. Somehow it does not find the style Hyperlink. So executed the find by adding the text 'Text to Display' of hyperlink which is a unique word. Now it works fine.

:beerchug:

macropod
04-05-2011, 08:35 PM
Hi Kris,

Hyperlinks in Word ordinarily have the hyperlink Style applied, which is why I used that. Besides, I don't recall seeing any mention of the 'Text to Display' being a unique word in your previous posts. Still, I'm glad you got it working.

Krishna Kumar
04-06-2011, 01:27 AM
Hi Paul,

What I meant was the address for each hyperlink is different, but the text displayed for each hyperlink is same.

Kris

macropod
04-06-2011, 02:00 AM
Hi Kris,

Yes, I understood that, but you didn't mention it until after I'd done the coding.

Krishna Kumar
04-06-2011, 07:36 AM
Hi Paul,

That's true :( . I should have been mentioned that.

Kris