Gruppe
Extern
Bereich
NotePad
Thema
Text in NotePad schreiben, speichern, in Excel aufrufen
Problem
Wie kann ich aus Excel heraus NotePad aufrufen, einen Text schreiben, diesen speichern, Notepad schliessen und die Textdatei in Excel laden?
Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.
StandardModule: basMain
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const WAIT_TIMEOUT = &H102&
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Sub Aufruf()
Dim sFile As String
sFile = Application.Path & "\testtext.txt"
If Dir(sFile) <> "" Then Kill sFile
SendKeys "Hallo Hubert!%du" & sFile & "%s", False
SendKeys "%de"
WartenBisFertig "NotePad"
Workbooks.OpenText sFile
MsgBox "Weiter"
ActiveWorkbook.Close
Kill sFile
End Sub
Sub WartenBisFertig(strEXE As String)
Dim ProcessID As Long
Dim hProcess As Long
Dim RetVal As Long
ProcessID = Shell(strEXE, vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
Do
DoEvents
RetVal = WaitForSingleObject(hProcess, 50)
Loop Until RetVal <> WAIT_TIMEOUT
End Sub