SendKeys() requires UAC to be disabled. Avoid that method if possible.
Did you really need Notepad open? You are really just making a text file.
See if these give you some ideas. Run T from a blank workbook.
Sub t()
Dim rc As Variant
Dim s As String, s2 As String
s = ActiveWorkbook.Path & "\Fruits.txt"
[A1] = "Fruit"
[A2] = "Apple"
[A3] = "Grape"
[A4] = "Orange"
[B1] = "Color"
[B2] = "Red/Golden"
[B3] = "Red/Green"
[B4] = "Orange"
Range("A1:B4").Copy
's2 = Replace(getClipboard(), vbTab, ",")
s2 = getClipboard()
Application.CutCopyMode = False
MakeTXTFile s, s2
rc = Shell("notepad " & s, vbNormalFocus)
'Kill s
End Sub
Sub InsertTxtFileContents()
Dim s As String, a() As String, b() As String
Dim i As Long, i2 As Long, r As Range
s = ActiveWorkbook.Path & "\Fruits.txt"
Set r = Range("C5")
FileLoadToArray a(), s
i2 = 0
For i = LBound(a) To UBound(a)
b() = Split(a(i), ",")
If UBound(b) > 0 Then r.Offset(i2, 0).Resize(1, UBound(b) + 1).Value = b()
i2 = i2 + 1
Next i
'Kill s
End Sub
Sub MakeTXTFile(filePath As String, str As String)
Dim hFile As Integer
If Dir(FolderPart(filePath), vbDirectory) = "" Then
MsgBox filePath, vbCritical, "Missing Folder"
Exit Sub
End If
hFile = FreeFile
Open filePath For Output As #hFile
If str <> "" Then Print #hFile, str
Close hFile
End Sub
Function FolderPart(sPath As String) As String
FolderPart = Left(sPath, InStrRev(sPath, "\"))
End Function
Function getClipboard()
'Add Reference: 'Reference: Microsoft Forms xx Object
Dim MyData As DataObject
On Error Resume Next
Set MyData = New DataObject
MyData.GetFromClipboard
getClipboard = MyData.GetText
End Function
'http://www.visualbasic.happycodings.com/Files_Directories_Drives/code54.html
'Purpose : Reads a file into a string array.
'Inputs : asLines() A string array (see Outputs)
' sFileName The path and file name of the file to open and read
'Outputs : Returns an empty string on success, else returns the error decription
' asLines(1 to NumLines) String array containing the file
'Notes : Usually used for text files, but will load any file type.
Function FileLoadToArray(ByRef asLines() As String, ByVal sFileName As String) As String
Dim iFileNum As Long, lFileLen As Long
Dim sBuffer As String
'Initialise Variables
On Error GoTo ErrFailed
'Open File
iFileNum = FreeFile
Open sFileName For Binary Access Read As #iFileNum
'Get the size of the file
lFileLen = LOF(iFileNum)
If lFileLen Then
'Create output buffer
sBuffer = String(lFileLen, " ")
'Read contents of file
Get iFileNum, 1, sBuffer
'Split the file contents
asLines = Split(sBuffer, vbNewLine)
End If
Close #iFileNum
'Return success
FileLoadToArray = ""
Exit Function
ErrFailed:
Debug.Assert False
Debug.Print Err.Description
FileLoadToArray = Err.Description
'Close file
If iFileNum Then
Close #iFileNum
End If
End Function