Skip to content

Instantly share code, notes, and snippets.

@simply-coded
Last active July 19, 2024 08:32
Show Gist options
  • Save simply-coded/2a31cbd5a7000cb825907052edbe35fe to your computer and use it in GitHub Desktop.
Save simply-coded/2a31cbd5a7000cb825907052edbe35fe to your computer and use it in GitHub Desktop.
Set, get, append, and clear your clipboard text.
Class ClipBoardText
REM @author: Jeremy England ( SimplyCoded )
REM @info: Set, get, append, and clear your clipboard text.
REM @mini: Class ClipBoardText:Property Get Text:Text=CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text"):If IsNull(Text)Then Text="":End If:End Property:Property Let Text(s):CreateObject("WScript.Shell").Run "mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','"&Replace(Replace(s,"'","\\u0027"),"""","\\u0022")&"');close()"")",0,True:End Property:Sub ClearText:Text="":End Sub:Sub AddText(s):Text=Text&s:End Sub:End Class
Property Get Text
Text = CreateObject( "HTMLFile" ).parentWindow.clipboardData.getData( "Text" )
If IsNull( Text )Then Text = ""
End Property
Property Let Text( str )
Call CreateObject( "WScript.Shell" ).Run( _
"mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" & _
Replace( Replace(str, "'", "\\u0027"), """", "\\u0022" ) & "');close()"")", 0, True _
)
End Property
Sub ClearText
Text = ""
End Sub
Sub AddText( str )
Text = Text & str
End Sub
End Class
'You can create an environment variable and store the mini class in there if desired.
'WindowsKey + r > rundll32 sysdm.cpl,EditEnvironmentVariables
'Create a user environment variable with the mini class as the value
'Load it into your script like so:
'Execute(CreateObject("WScript.Shell").Environment("User")("YOUR_VAR_NAME"))
'Otherwise just copy the mini or full version into your scripts, and use.
Class ClipBoardText:Property Get Text:Text=CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text"):If IsNull(Text)Then Text="":End If:End Property:Property Let Text(s):CreateObject("WScript.Shell").Run "mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','"&Replace(Replace(s,"'","\\u0027"),"""","\\u0022")&"');close()"")",0,True:End Property:Sub ClearText:Text="":End Sub:Sub AddText(s):Text=Text&s:End Sub:End Class
'Create an instance of the ClipBoardText Class
Set clip = New ClipBoardText
'Clear the clipboard text
clip.ClearText()
'Set the clipboard text
clip.Text = "Hello, World!"
'Append to the clipboard text
clip.AddText( " How are you?" )
'Get the clipboard text
result = clip.Text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment