在新的记事本中写入文本

任何一个人可以帮我写单元格值的记事本的新实例?

这是我试过的代码:

Sub a() Dim nt As String nt = Shell("notepad.exe", vbNormalFocus) Print #1, ActiveSheet.Cells(1, 1).Value Close #1 End Sub 

很多年前,我在vbforums.com上已经回答过类似的问题,但是找不到,所以我很快就为你写了。 我已经评论了代码,所以你不应该有理解它的问题。

就像你我一样,我们都有名字,类似的窗口有“句柄”(hWnd),Class等等。一旦你知道了hWnd是什么,就更容易和窗口交互了。 Findwindow API通过使用类名来查找特定窗口的hWnd。 在这里阅读其余的API

 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ ByVal lpsz2 As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const WM_SETTEXT = &HC Private Sub Command1_Click() Dim Ret As Long, ChildRet As Long Dim sString As String '~~> This is the value from the cell which '~~> you want to send to notepad sString = Range("A1").Value '~~> Start Notepad Ret = Shell("notepad.exe", vbNormalFocus) '~~> Wait for it to load DoEvents '~~> Find notepad Ret = FindWindow(vbNullString, "Untitled - Notepad") '~~> Check if found If Ret = 0 Then MsgBox "Cannot find Notepad Window" Exit Sub End If '~~> Find the "Edit Window" which is a child window of Notepad window ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString) '~~> Send the message SendMessage ChildRet, WM_SETTEXT, 0, ByVal sString End Sub 

要获取窗口的类名,我通常使用Spy ++或uuSpy。 看到这是我如何得到记事本的类名“编辑”使用Spy ++

在这里输入图像描述