Below is a an updated example where through Excel (vba), the sub opens Notepad, adds text and then prompts for a save as file name. It works except the passing of the file name from the vba code to Windows Save File dialog.
Option Explicit
Private Declare Function AllowSetForegroundWindow Lib "user32.dll" (ByVal dwProcessId As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal lngHWnd As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpszClass As String, ByVal lpszTitle As String) As Long
Private Declare Function LockSetForegroundWindow Lib "user32.dll" (ByVal uLockCode As Long) As Long
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const WM_SETTEXT As Long = &HC
Private Const LSFW_LOCK = 1
Private Const VK_CONTROL = &H11 '0x11
Private Const VK_S = &H53 '0x53
Sub WriteToNotepad()
Dim hwndNotepad&, hwndTextbox&, hwndSaveAs&, hwndSaveLocation, hwndFileName&, Retval
ResumeHere:
' Start "Notepad"
Retval = Shell("C:\Windows\System32\NotePad.exe", 4)
' Identify handle for "Notepad" window
hwndNotepad = FindWindowEx(0, 0, "Notepad", vbNullString)
hwndTextbox = FindWindowEx(hwndSaveAs, 0, "Edit", vbNullString)
' Write message
SendMessageString hwndTextbox, WM_SETTEXT, 0, "My message goes here"
' Lock the window for futher input
BringWindowToTop (hwndNotepad)
AllowSetForegroundWindow (hwndNotepad)
SetForegroundWindow (hwndNotepad)
LockSetForegroundWindow (LSFW_LOCK)
' Show Save As dialog box
'Press Ctrl key down, but don't release
keybd_event VK_CONTROL, 0, 0, 0
'Press the letter "S" then release
keybd_event VK_S, 0, 0, 0
keybd_event VK_S, 0, 2, 0
'Release the Alt key
keybd_event VK_CONTROL, 0, 2, 0
' Find SaveAs window before continuing
hwndSaveAs = FindWindowEx(0, 0, "#32770", vbNullString)
hwndFileName = FindWindowEx(hwndSaveAs, 0, "Edit", vbNullString)
' Write file name
SendMessageString hwndFileName, WM_SETTEXT, 0, "Testing file.txt"
End Sub