Writing text to the clipboard with VBScript 
Author Message
 Writing text to the clipboard with VBScript

Just for grins, I was trying to write a little snippet of script that would fix
broken URL's copied out of an email or newsgroup message, i.e., clean them up
and unwrap them. The following code will get text from the clipboard and do the
clean up, but I can't seem to write the new text back into the clipboard. Is
there a way to do this without using an ActiveX component (of which I found
several when trying to Google an answer to this question)? I'm just doing this
for my own personal enjoyment, so if you have any other comments (ways to
streamline the regexp processing, etc.) I'm all ears. TIA

'
' Take a chunk of text from the clipboard containing a wrapped URL, and output
' the "fixed" URL, i.e., unwrapped and with comment and whitespace characters
' removed. Trim off anything preceding the http as well.
'
 Set oHtml = CreateObject("htmlfile")
 sClipText = oHtml.ParentWindow.ClipboardData.GetData("text")

 With New RegExp
   .IgnoreCase  = True
   .MultiLine   = True
   .Global      = True        ' Find all matches
   .Pattern     = "\s|>| "
   URL          = .Replace(sClipText,"")
   .Global      = False       ' Only find first match
   .Pattern     = "(http[s]?\:.*)"
   Set patMatch = .Execute(URL)
   URL          = trim(patMatch(0).SubMatches(0))
 End With

 Set patMatch = Nothing
 set RegExp   = Nothing

 ClipStatus = oHtml.ParentWindow.ClipboardData.SetData("text",URL)
 set oHtml  = Nothing

 WScript.Echo "Original text was:"&vblf&sClipText&vblf&vblf&_
                      "URL is:"&vblf&vblf&URL&vblf&ClipStatus



Sat, 13 Aug 2005 07:06:06 GMT  
 Writing text to the clipboard with VBScript

Quote:

> Just for grins, I was trying to write a little snippet of script that would fix
> broken URL's copied out of an email or newsgroup message, i.e., clean them up
> and unwrap them. The following code will get text from the clipboard and do the
> clean up, but I can't seem to write the new text back into the clipboard. Is
> there a way to do this without using an ActiveX component (of which I found
> several when trying to Google an answer to this question)? I'm just doing this
> for my own personal enjoyment, so if you have any other comments (ways to
> streamline the regexp processing, etc.) I'm all ears. TIA

Hi

htmlfile is not able to put to the clipboard, but the IE object is:

sText = "some text to clipboard"

Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate "about:blank"
Do Until oIE.ReadyState=4: WScript.Sleep 1: Loop
oIE.Document.ParentWindow.ClipboardData.SetData "Text", sText
oIE.Quit

Just be aware that depending on your Internet Explorer security settings, you can
get this message when running the script above (it will say 'from' even if it is
'to' ;-)

---------------------------
Internet Explorer
---------------------------

Do you want to allow this page to paste information from
your clipboard

---------------------------
    Yes    No
---------------------------

More about this in this thread:

Subject: Retrieve text from clipboard
Newsgroups: microsoft.public.scripting.VBScript
http://groups.google.com/groups?th=5c6f16e6f4ff1885

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of the 1328 page Scripting
Guide: http://www.microsoft.com/technet/scriptcenter



Sat, 13 Aug 2005 07:39:31 GMT  
 Writing text to the clipboard with VBScript

Quote:

> htmlfile is not able to put to the clipboard, but the IE object is:

> sText = "some text to clipboard"

> Set oIE = CreateObject("InternetExplorer.Application")
> oIE.Navigate "about:blank"
> Do Until oIE.ReadyState=4: WScript.Sleep 1: Loop
> oIE.Document.ParentWindow.ClipboardData.SetData "Text", sText
> oIE.Quit

Many thanks - that certainly did the trick, although it's not as fast as one
might hope. =(

Quote:
> Just be aware that depending on your Internet Explorer security
> settings, you can get this message when running the script above (it
> will say 'from' even if it is 'to' ;-)

> ---------------------------
> Internet Explorer
> ---------------------------

> Do you want to allow this page to paste information from
> your clipboard

> ---------------------------
>     Yes    No
> ---------------------------

> More about this in this thread:

> Subject: Retrieve text from clipboard
> Newsgroups: microsoft.public.scripting.vbscript
> http://groups.google.com/groups?th=5c6f16e6f4ff1885

That wasn't a problem for me on this machine, but I'll know what the problem is
(and a potential workaround) if I see it elsewhere. Now I know where that
htmlfile clipboard trick came from as well.

The script below now works as intended - next job is to get it to set focus to
my browser, bring up the "Open link" dialog, paste in the URL, and send a CR,
triggering all events with a single hotkey. The things we do in the name of
laziness... =)

'-----
' Take a chunk of text from the clipboard containing a wrapped URL, and output
' the "fixed" URL, i.e., unwrapped and with comment and whitespace characters
' removed. Trim off anything preceding the http as well.
'-----

 Set oIE = CreateObject("InternetExplorer.Application")
 oIE.Navigate "about:blank"
 sClipText = oIE.Document.ParentWindow.ClipboardData.GetData("Text")

 With New RegExp
   .IgnoreCase  = True
   .MultiLine   = True
   .Global      = True        ' Find all matches
   .Pattern     = "\s|>| "
   URL          = .Replace(sClipText,"")
   .Global      = False       ' Only find first match
   .Pattern     = "(http[s]?\:.*)"
   If (.test(URL)) Then
     Set patMatch = .Execute(URL)
     URL = trim(patMatch(0).SubMatches(0))
   Else
     URL = "about:blank"
   End If
 End With
 Set patMatch = Nothing : set RegExp = Nothing

 Do Until oIE.ReadyState=4 : WScript.Sleep 1 : Loop
 oIE.Document.ParentWindow.ClipboardData.SetData "Text", URL
 oIE.Quit : set oIE = Nothing
 WScript.Quit



Sat, 13 Aug 2005 08:25:40 GMT  
 Writing text to the clipboard with VBScript

Quote:

> That wasn't a problem for me on this machine, but I'll know what the problem is
> (and a potential workaround) if I see it elsewhere. Now I know where that
> htmlfile clipboard trick came from as well.

> The script below now works as intended - next job is to get it to set focus to
> my browser, bring up the "Open link" dialog, paste in the URL, and send a CR,
> triggering all events with a single hotkey. The things we do in the name of
> laziness... =)

Why not just do like this then (and then you dont need to put anything to the
clipboard):

 ' your fixed URL
 URL = "http://groups.google.com"

 Set oIE = CreateObject("InternetExplorer.Application")
 oIE.Navigate URL
 oIE.Visible = True
 WScript.Quit

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of the 1328 page Scripting
Guide: http://www.microsoft.com/technet/scriptcenter



Sat, 13 Aug 2005 08:54:22 GMT  
 Writing text to the clipboard with VBScript

Quote:

> Why not just do like this then (and then you dont need to put
> anything to the clipboard):

>  ' your fixed URL
>  URL = "http://groups.google.com"

>  Set oIE = CreateObject("InternetExplorer.Application")
>  oIE.Navigate URL
>  oIE.Visible = True
>  WScript.Quit

Well, mainly because I'm not using IE. =)


Sat, 13 Aug 2005 12:56:34 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Text to Clipboard using VBScript?

2. Needed text box for text written from write to left

3. how do i write the clipboard from jscript?

4. Clipboard Read/Write

5. reading/writing from/to clipboard

6. Clipboard Read/Write

7. Read and write text files in vbScript (without a DSN)

8. Clipboard Read/Write

9. Building data entry screens in vbscript and writing results to text files

10. Pasting text from the clipboard, finding that text, and applying a style

11. VB: Copy Text From DOS Window To Clipboard As Text

12. passing variables from a text box to a document.write text box

 

 
Powered by phpBB® Forum Software