Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2025, 01:15 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default A simple macro to copy a returned string to clipboard


Can someone point to a simple macro to copy a returned string to clipboard? Thanks!
Reply With Quote
  #2  
Old 04-12-2025, 01:35 AM
macropod's Avatar
macropod macropod is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example:
Code:
Sub Demo()
' Note: A VBA reference to the MS Forms Object Library is required. See Tools|References in the VBE.
Dim MyData As DataObject: Set MyData = New DataObject
MyData.SetText "My Text String": MyData.PutInClipboard
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-12-2025, 03:28 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Many thanks Paul! It works perfectly. But how do I recover in a macro the string I copied to the Clipboard? Cheers, RobiNew
Reply With Quote
  #4  
Old 04-12-2025, 04:13 AM
macropod's Avatar
macropod macropod is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You would normally only be putting it into the clipboard to paste into a document or other application's file. What are you trying to achieve?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-12-2025, 06:46 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

This is my macro:


Code:
Sub ColorRGBText()
OPT = InputBox("Get Color[1], Apply Color[2]", "Color Management", "1")
If OPT = 1 Then
'MsgBox Selection.Font.TextColor.RGB
MyColor = Selection.Font.TextColor.RGB 'gets the color
Dim MyData As DataObject: Set MyData = New DataObject
MyData.SetText MyColor: MyData.PutInClipboard 'copies color code into ClBrd
End If
'When the macro is restarted and option 2 selected, the color code should be applied to a different text selection
If OPT = 2 Then Selection.Font.TextColor.RGB = MyColor 
End Sub
Reply With Quote
  #6  
Old 04-12-2025, 07:32 AM
gmayor's Avatar
gmayor gmayor is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Surely the format painter would be more appropriate for this task? However, the following should work.
Code:
Sub ColorRGBText()
Dim myColor As String
Dim OPT As Long
Dim MyData As DataObject
    Set MyData = New DataObject
    OPT = InputBox("Get Color[1], Apply Color[2]", "Color Management", "1")
    If OPT = 1 Then
        'MsgBox Selection.Font.TextColor.RGB
        myColor = Selection.Font.TextColor 'gets the color`
        MyData.SetText myColor
        MyData.PutInClipboard 'copies color code into ClBrd
    End If
    'When the macro is restarted and option 2 selected, the color code should be applied to a different text selection
    If OPT = 2 Then
        MyData.GetFromClipboard
        myColor = MyData.GetText(1)
        If myColor <> "" Then
            Selection.Font.TextColor = myColor
        Else
            MsgBox "Color has not been selected"
        End If
    End If
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 04-12-2025, 07:55 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 11 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Unfortunately 'If OPT = 2 ...' does not apply any color, though the correct code appears in a MsgBox. Any idea? Thanks!
Reply With Quote
  #8  
Old 04-12-2025, 09:41 AM
gmayor's Avatar
gmayor gmayor is offline A simple macro to copy a returned string to clipboard Windows 10 A simple macro to copy a returned string to clipboard Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It works here - did you select the text you want to re-colour before running the macro again?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 04-12-2025, 10:09 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 11 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

I did, but it doesn't work (Windows 11). Thanks!
Reply With Quote
  #10  
Old 04-12-2025, 10:15 AM
RobiNew RobiNew is offline A simple macro to copy a returned string to clipboard Windows 11 A simple macro to copy a returned string to clipboard Office 2016
Competent Performer
A simple macro to copy a returned string to clipboard
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Now it works! Thanks a lot. All the best, Robi
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
A simple macro to copy a returned string to clipboard Paste from Clipboard, Find and Replace All, Then Copy Results Back to The Clipboard robnun Word VBA 7 01-03-2025 12:52 PM
A simple macro to copy a returned string to clipboard Copy Specific Text String To Clipboard At End Of Macro et_33 Excel Programming 8 10-11-2022 06:24 AM
WORD MACRO COPY/PAST from clipboard rachidlea Word VBA 0 11-16-2021 09:38 AM
A simple macro to copy a returned string to clipboard Simple macro to create a copy eduzs Word VBA 3 05-17-2017 05:34 PM
A simple macro to copy a returned string to clipboard copy without using clipboard? g48dd Excel 3 07-16-2011 10:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft