从 excel VBA 打开并写入记事本

Open and Write to Notepad from excel VBA

我需要打开记事本并将宏的结果写入其中。我想打开记事本并写入,然后让用户查看,如果他或她想保存它,则将其保存在他们喜欢的任何地方。而不是全部写入代码并保存在计算机上。

谢谢

使用以下代码打开记事本并在其中键入:

Dim myApp As String
myApp = Shell("Notepad",    vbNormalFocus)
SendKeys "test", True

与 Tarik 的回答类似,我可能会使用如下内容:

In a class module called NotepadManager:

    Option Explicit

Private Const CAPTION$ = "Notepad"

Private MHwnd As Long

'based on code lifted from:
'http://www.pbdr.com/vbtips/api/FindCloseAPI.htm


    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
      (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
      (ByVal hWnd As Long, ByVal lpString As String, _
      ByVal aint As Long) As Long
    Private Declare Function GetWindow Lib "user32" _
      (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function EnumWindows Lib "user32" _
      (ByVal wndenmprc As Long, ByVal lParam As Long) 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 Declare Function SetForegroundWindow Lib "user32" ( _
     ByVal hWnd As Long) As Long

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

   Private Const WM_CLOSE = &H10
   Private Const GW_HWNDFIRST = 0
   Private Const GW_HWNDLAST = 1
   Private Const GW_HWNDNEXT = 2
   Private Const GW_HWNDPREV = 3
   Private Const GW_OWNER = 4
   Private Const GW_CHILD = 5
   Private Const GW_MAX = 5

   Private mstrTarget As String
   Private mblnSuccess As Boolean


Public Sub writeMessageToNotepad(ByRef message As String)

focusNotepad

Sleep 2000

SendKeys (message)

End Sub

Public Sub startNotepad()

Dim ret

ret = Shell("notepad", vbNormalFocus)

End Sub


Public Sub focusNotepad()

        If MHwnd = 0 Then
            MHwnd = hwndFindWindow(CAPTION$)
        End If

        If MHwnd = 0 Then MsgBox "Error: Cannot find notepad."

        Debug.Print SetForegroundWindow(MHwnd)

End Sub



Private Function hwndFindWindow(strApplicationTitle As String) As Long

   Dim hWndTmp As Long
   Dim nRet As Integer
   Dim TitleTmp As String
   Dim TitlePart As String
   Dim MyWholeTitle As String
   Dim mCounter As Long
   Dim hWndOver As Integer
   Dim sClassName As String * 100

   hwndFindWindow = False

   TitlePart = UCase$(strApplicationTitle)

   'loop through all the open windows
   hWndTmp = FindWindow(0&, 0&)

   Do Until hWndTmp = 0

      TitleTmp = Space$(256)
      nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))

      If nRet Then
         'retrieve window title
         TitleTmp = UCase$(VBA.Left$(TitleTmp, nRet))
         'compare window title & strApplicationTitle
         If InStr(TitleTmp, TitlePart) Then
            hwndFindWindow = FindWindow(vbEmpty, TitleTmp)
            Exit Do
         End If
      End If

      hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
      mCounter = mCounter + 1

   Loop

   End Function


Private Function EnumCallback(ByVal app_hWnd As Long, _
  ByVal param As Long) As Long

   Dim buf As String * 256
   Dim title As String
   Dim length As Long

   ' Checks a returned task to determine if App should be closed

   ' get window's title.
   length = GetWindowText(app_hWnd, buf, Len(buf))
   title = Left$(buf, length)

   ' determine if target window.
   If InStr(UCase(title), UCase(mstrTarget)) <> 0 Then
      ' Kill window.
      SendMessage app_hWnd, WM_CLOSE, 0, 0
      mblnSuccess = True
   End If

   ' continue searching.
   EnumCallback = 1

End Function

在普通模块中:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub test()


Dim m As New NotepadManager

m.startNotepad

m.focusNotepad

Sleep 2000

m.writeMessageToNotepad "My message"

Debug.Print "done"

End Sub

但是,我经常发现当用户当时与机器交互时,确保您有 window 焦点是最困难的事情。