我使用以下命令将数据从 Excel 复制到记事本SendKeys
Excel VBA 函数。
我希望避免使用 sendkeys。
我有这个代码:
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
这只是将数据从Excel复制到记事本,但在Excel文件中做了一些更正后,我希望将记事本中的数据从C2开始复制到Excel。
这是一个替代过程SendKeys
:
从工作表上的一系列单元格获取值
复制到剪贴板
将剪贴板内容获取到字符串中
将该字符串保存到临时文件中
使用临时文件的内容打开 Notepad.exe
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)