I copy data from Excel to Notepad using the SendKeys
Excel VBA function.
I'm looking to avoid using the sendkeys.
I have this code:
sub test()
dim wb as Workbook
set wb = "C:\Documents\test.xlsx"
wb.Sheets(2).Range("C2:C" & lRow).Copy
myApp = Shell("Notepad.exe", vbNormalFocus)
SendKeys "^v"
Application.CutCopyMode = False
wb.Sheets(2).Range("C2:C" & lRow).NumberFormat = "@"
end sub
This only copies the data from Excel to Notepad, but after doing some corrections in the Excel file, I want the data in Notepad to be copied to Excel starting from C2.
This is an alternative process to SendKeys
:
gets values from a range of cells on a worksheet
copies to clipboard
gets the clipboard content into a string
saves that string to a temp file
opens Notepad.exe with the content of the temp file
Code:
Option Explicit
Sub OpenNotepadWithTempFileWithClipboardContent()
Dim rngData As Range
Dim strData As String
Dim strTempFile As String
' copy some range values
Set rngData = Sheet3.Range("B1:B5")
rngData.Copy
' get the clipboard data
' magic code for is for early binding to MSForms.DataObject
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.GetFromClipBoard
strData = .GetText
End With
' write to temp file
strTempFile = "D:\temp.txt"
With CreateObject("Scripting.FileSystemObject")
' true to overwrite existing temp file
.CreateTextFile(strTempFile, True).Write strData
End With
' open notepad with tempfile
Shell "cmd /c ""notepad.exe """ & strTempFile & """", vbHide
End Sub