Home > Software engineering >  Headache is dead, VBA how to open notepad text in copying to Excel, then the notepad close
Headache is dead, VBA how to open notepad text in copying to Excel, then the notepad close

Time:09-26

Notepad is another program automatically Open, I do not know the name and path of the text, so can not use the Open... for... Statement to the operation,

I want to by VBA is found after system opens up a notepad, copies the content inside to Excel, and then close the notepad,

Ask you master how to write code, thank you!


CodePudding user response:

http://www.autohotkey.com

CodePudding user response:

Don't need to use "open" the action, only need to "read",
Read all the contents of the file, will do

CodePudding user response:

Vb and vba?

Excel, word of vba external programs cannot be shut down

Vb, provide the ideas

Search notepad forms regularly, then getwindowstext, then open the excel input data, then close notepad.

CodePudding user response:

Search all the current window, distinguish the notepad, so, take a child window, to send message to retrieve text,

CodePudding user response:

 Option Explicit 

Private Const WM_DESTROY As Long=& amp; The H2
Private Const WM_NCDESTROY As Long=& amp; H82

Private Declare Function DestroyWindow Lib "user32. DLL" (ByVal HWND) As Long As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam) As Long As Boolean
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal HWND As Long, ByVal lpString As String, ByVal CCH) As Long As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal HWND) As Long As Long
Private Declare Function PostMessage Lib "user32. DLL" Alias "PostMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam) As Long As Long

The Public Function EnumWindowsProc (ByVal HWND As Long, ByVal lParam) As Long As Boolean
Dim lLength As Long
Dim sText As String
Dim hEdit As Long

'access to form the title, determine whether to notepad'
LLength=GetWindowTextLength (HWND)
If lLength=0 Then GoTo NextWindow

SText=Space (lLength)
SText GetWindowText HWND, lLength + 1

If Not (sText Like "* - notepad") Then GoTo NextWindow

The Debug. Print Hex (HWND) & amp; ":" & amp; SText

'get the edit box the text'
HEdit=FindWindowEx (HWND, 0 & amp; , "Edit", vbNullString)

LLength=GetWindowTextLength (hEdit)
If lLength=0 Then
SText=vbNullString
The Else
SText=Space (lLength)
GetWindowText hEdit sText, lLength + 1
End the If

The Debug. Print Hex (hEdit) & amp; ":" & amp; SText

'close notepad'
PostMessage HWND, WM_DESTROY, 0 & amp; , 0 & amp;
PostMessage HWND, WM_NCDESTROY, 0 & amp; , 0 & amp;

StopEnum:
EnumWindowsProc=False
The Exit Function
NextWindow:
EnumWindowsProc=True
End the Function

Public Sub f ()
The EnumWindows AddressOf EnumWindowsProc, ByVal 0 & amp;
End Sub

Running the above macro f,
EnumWindowsProc the notepad contents (sText), you can direct the output to Excel,

CodePudding user response:

Can use a few years ago I wrote a class clswindow, very convenient,
Clswindow introduced: http://blog.csdn.net/sysdzw/article/details/9083313

The following code tests pass,
 Sub button 1 _click () 
Dim w As New clsWindow
Range (" A1 ")=w.G etWindowByTitleEx (" notepad ", 1). GetElementTextByClassName (" edit ")
W.C loseWindow 'close notepad window
End Sub

If a program has multiple Windows closed with w.C loseApp
Can also use a window is closed out the special effects, w.F adeOut, True 'disappearing finally close the window

CodePudding user response:

The above code test files uploaded, download address: http://download.csdn.net/detail/sysdzw/9401598
  •  Tags:  
  • VBA
  • Related