I need to paste data from clipboard (Ctrl+V) into a text file and save it.
I already did some research and it seems that a text file
doesn't have a method like SaveAs
.
With code below, I could create a new text file and paste data into it, but I can't Save
it:
Set WShshell = CreateObject("WScript.Shell")
WShshell.run "c:\WINDOWS\system32\notepad.exe",1
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V"
I understand there is a method called CreateTextFile
, but seems I can't perform paste
with it.
I also tried to combine those two:
Set WShshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "c:\1.txt",true
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V"
But nothing happened...
UFT has its own clipboard access methods you could use. Here I've created an instance of the clipboard, and extracted its content into sText
, then created a text file in C:\\temp and written the data from the clipboard directly into it. oFile.Close
closes the file and saves it at the same time.
Dim oClipboard : Set oClipboard = CreateObject("Mercury.Clipboard")
sText = oClipboard.GetText ' gets the current content of the clipboard
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.CreateTextFile("C:\temp\myTextFile.Txt", True)
oFile.Write sText
oFile.Close
I took a stab at it... This was my successful result.
I also noted that this script got really angry when another notepad was already opened.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set WShshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'Gets user profile variable with CMD
UserProfile = CreateObject("WScript.Shell").Exec("cmd /c echo.%USERPROFILE%").Stdout.ReadAll
'Creates temporary files in appdata\local\temp
'--------------------------------------------------------------------------------------
tmpfilename = fso.GetTempName
tmpfile = Replace(UserProfile & "\AppData\Local\Temp\" & tmpfilename, vbcrlf, "")
Set CreateTempFile = FSO.OpenTextFile(tmpfile, forwriting, true) : CreateTempFile.Close
'Creates a new file for the pasted content
'--------------------------------------------------------------------------------------
MyFile = ThisFolder & "ClipBoard_Extract_" & _
Replace(FormatDateTime(Cdate(now),2), "/", "-") & "_" & _
Hour(now) & Minute(Now) & Second(now) & ".txt"
'Execute's the Main Sub but you could get along without it.
' I usually build my scripts to scale and do logging and other magical things.
'--------------------------------------------------------------------------------------
MainScript
Sub MainScript()
CaptureClipboardText
ExtractTempFile
End Sub
Sub CaptureClipboardText
WShshell.run "c:\WINDOWS\system32\notepad.exe " & tmpfile, 1
WshShell.AppActivate "Notepad"
wscript.sleep 1000
WShshell.SendKeys "^V"
WShshell.SendKeys "^S"
Wshshell.SendKeys "%F"
Wshshell.SendKeys "x"
Wshshell.SendKeys "s"
wscript.sleep 1000
End Sub
Sub ExtractTempFile
Set Extract = fso.OpenTextFile(tmpfile, ForReading)
Set Output = fso.OpenTextFile(MyFile, ForWriting, True) 'True on this syntax means - Create the file if it doesn't exist.
Do Until Extract.AtEndofSTream
line = Extract.Readline
Output.Writeline Line
Loop
Extract.Close : Set Extract = Nothing
fso.DeleteFile tmpfile, true
End Sub
Function ThisFolder
ThisFolder = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName))
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.