Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-10-2018, 09:26 PM
cheech1981 cheech1981 is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 73
cheech1981 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 09-10-2018, 10:00 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

If you recorded the copying, your macro would have a line like:
Selection.Copy
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 11-29-2018, 04:12 PM
cheech1981 cheech1981 is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 73
cheech1981 is on a distinguished road
Default

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
So it looks like I need to change the "address" field to be the string that the user currently has copied to the clipboard.
Reply With Quote
  #4  
Old 11-29-2018, 09:25 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,755
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

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)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 11-29-2018, 09:25 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,468
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
Reply

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


All times are GMT -7. The time now is 01:50 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft