View Single Post
 
Old 11-04-2022, 01:14 PM
Testeris Testeris is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2022
Posts: 8
Testeris is on a distinguished road
Default Make hyperlink in macro using Clipboard

Hi all,
I am trying to create a word file to use with another macro. I need to select some text and make it hyperlink that I copy/cut from a line below (it is automatically generated in Excel and linked/updated at the time of opening the doc). The result is cut and saved into separate Word files, as part of a big batch file.



So far I scroll, select and cut the hyperlink text I need. Then I go back several numbers to select the reference and run a subroutine to paste in the hyperlink text onto my selection.


What I encounter is error "DataObject:getfromclipboard openclipboard failed". I debug, yellow arrow at "MyData.GetFromClipboard" and when clicked the green arrow (F5) it goes through as expected.



I can't get it done in one go. What I am doing wrong?

Sorry, I am "copypaster" only. My suspicion is that CUT does not put into Clipboard, so I tried to COPY and then cut, and both...

P.S. An example of the text in Bulgarian (I am not)


... several lines...
Текстовете, приети на 4 май 2022 г. относно освобождаването от отговорност във връзка с изпълнението на Бюджет 2020 г., са публикувани в [Here goes the hyperlink]-->ОВ L 258, 5.10.2022.
https://eur-lex.europa.eu/legal-content/BG/TXT/?uri=OJ%3AL%3A2022%3A258%3AFULL
... several lines...


My "macro":
Sub aMacro1()
'
' aMacro1 Macro
'
Dim MyData As DataObject
Dim strAddr As String
Dim strTitle As String
Set MyData = New DataObject

Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdParagraph, Count:=9
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
' cut the link text
' Selection.Copy
Selection.Cut
MyData.GetFromClipboard
strAddr = MyData.GetText

' selecting the location where to overlay
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=10, Extend:=wdExtend
' sub to put in the hyperlink -->
' Commented FOR REUSING IF IT FAILS
' Dim MyData As DataObject
' Dim strAddr As String
' Dim strTitle As String

' On Error Resume Next - skipped the error but did not do the job

' MyData.GetFromClipboard
' strAddr = MyData.GetText

' Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
' Selection.Copy
' MyData.GetFromClipboard

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

' End of the insert

' return to the beginning of the page and/or continue to other macto
Selection.HomeKey Unit:=wdStory
End Sub


Thanks
Reply With Quote