#1
|
|||
|
|||
copying text with Word's macro recorder
Hi everyone,
I am trying to set up a macro that, among other things, involves copying text to the clipboard, then later pasting that text into the address field of the Insert Hyperlink pop-up box. However, it seems that Word is unable to recognize the copy-text function (at least through the recorder). What happens is that the pasted text reflects whatever was on the clipboard before running the macro (rather than the text that was supposed to be copied during the macro). Does anyone have experience with a workaround for this? I have read that even the VBA is buggy for simple copy and paste, but I do not know much VBA at all, hence the recorder. Thank you in advance for any suggestions. |
#2
|
||||
|
||||
If you recorded the copying, your macro would have a line like:
Selection.Copy
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
Actually I am not recording the copying because the copied text comes from outside the Word program (info from web). So I should have worded that question slightly differently. Here is a video to show what I'm trying to do and the problem I am running into: https://youtu.be/Dx88UOHFsH8 How would I go about tweaking the vba to "paste from clipboard" both at the beginning of the macro (into the Word doc) and then at the end (to create a hyperlink address)? I am also posting the recorded vba below that I created when recording the video. Code:
Sub Macro133() ' ' Macro133 Macro ' ' Selection.Paste Selection.HomeKey Unit:=wdLine Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "https://doi.org/" .Replacement.Text = "doi:" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.TypeText Text:="doi:" Selection.MoveLeft Unit:=wdCharacter, Count:=4 Selection.EndKey Unit:=wdLine, Extend:=wdExtend Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ "https://doi.org/10.1177%2F1077800407301175", SubAddress:="", ScreenTip:= _ "", TextToDisplay:="doi:10.1177%2F1077800407301175" End Sub |
#4
|
||||
|
||||
If the aim of the game is to modify a hyperlink (or links) in a pasted text then based on your code.
Code:
Sub Macro134() Dim oRng As Range Dim oLink As Hyperlink Set oRng = Selection.Range oRng.Paste Options.AutoFormatReplaceHyperlinks = True oRng.AutoFormat If oRng.Hyperlinks.Count > 0 Then For Each oLink In oRng.Hyperlinks oLink.TextToDisplay = Replace(oLink.Address, "https://doi.org/", "doi:") Next oLink End If Set oLink = Nothing Set oRng = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
||||
|
||||
It's still not clear what you want. Perhaps:
Code:
Sub Demo() With Selection .Paste With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "https://doi.org/" .Replacement.Text = "doi:^c" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceOne End With If .Find.Found = True Then .Start = .Start + 4 .Hyperlinks.Add Anchor:=.Range, Address:="https://" & .Text, TextToDisplay:=.Text End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
copy and paste, macro, recorder |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro for copying text between two Words. | Demonic | Word VBA | 3 | 07-18-2017 09:08 PM |
Copying Certain Text From Numerous Cells using Macro | Sean_Needs_Help | Excel | 3 | 11-08-2016 04:39 PM |
Macro recorder changing reference style for columns in pivot source | andylaw31 | Excel Programming | 0 | 06-15-2016 08:45 AM |
Macro recorder and ShowFieldCodes | Jennifer Murphy | Word VBA | 1 | 11-04-2012 01:55 AM |
Picture properties bug in Word 2010 Macro Recorder | ralphpickering | Word VBA | 5 | 10-24-2012 10:15 AM |