-2

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
abousetta
  • 111
  • 7
  • possible duplicate of [GetSaveFileName() how to update the file extension in the "File name:" control?](http://stackoverflow.com/questions/14449280/getsavefilename-how-to-update-the-file-extension-in-the-file-name-control) – Remy Lebeau Sep 19 '14 at 05:51
  • Thanks Raptor and Remy. New example with code have been added to the question. I don't think it's a duplicate question to the one you had worked on before Remy, but I may be mistaken. – abousetta Sep 19 '14 at 17:33

1 Answers1

2

Well, you certainly don't do it by synthesizing keystrokes. The correct way of pre-filling the file name field in the Save (or Open) dialog is to put the desired string in the lpstrFile member of the OPENFILENAME structure that you pass to the GetSaveFileName function.

When the dialog is closed by the user, that field will be updated with the file name and path that was selected.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks Cody. I apologize for not being more clear in the first example. I have created a new example that will hopefully be more clear as what I am doing. – abousetta Sep 19 '14 at 17:32