I am working on a VBA macro to open the Save As screen in Excel using an API call to F12 (the shortcut for the screen), and then populating the screen with the file name and location. The screen is opened successfully but the code I have following the call to F12 never executes properly regardless of everything I have tried.
It should be as simple as:
CopyToClipboard fileName
PressF12
PressControlV
CopyToClipboard saveDirectory
PressAltD
PressControlV
The Alt+D shortcut selects to the address bar.
The API calls:
Sub PressControlV()
keybd_event VK_CONTROL, 0, 0, 0 'Press Ctrl
keybd_event VK_V, 0, 0, 0 'Press V
keybd_event VK_V, 0, 2, 0 'Release V
keybd_event VK_CONTROL, 0, 2, 0 'Release Ctrl
End Sub
Sub PressF12()
keybd_event VK_F12, 0, 0, 0
keybd_event VK_F12, 0, 2, 0 'Release V
End Sub
Sub PressAltD()
keybd_event VK_MENU, 0, 0, 0 'Press alt
keybd_event VK_D, 0, 0, 0 'Press D
keybd_event VK_D, 0, 2, 0 'Release D
keybd_event VK_MENU, 0, 2, 0 'Release alt
End Sub
Currently, all this does is open the Save As window, type "v" into the filename, and sometimes correctly paste the save address.
If I comment out everything but CopyToClipboard fileName, PressF12, PressControl
, it will sometimes correctly paste the filename but more often then not just types "v".
If I comment out everything but CopyToClipboard fileName, PressF12, PressControlV, CopyToClipboard saveDirectory
it will correctly paste the save directory where the name is.
why??
I know doing it this way through the UI is stupid and annoying even if it were working, but I have done far more complicated things through the UI as I do not have permission to .SaveAs
(or much) since the location is on a company drive. I'm not an admin on this computer to make things worse, but I don't know why that would contribute to this issue?
Every method to generate and populate a save as dialogue box has not worked either.
The many things I have tried
First thing I tried was just adding some
Sleep
lines right after F12 is called (so the window has time to open before things are pasted right?).NO MATTER WHAT I try, the Save As window is always opened last and all the following code executes horribly.
I have tried adding in
DoEvents
after the F12 call, nothing happens at all. I have tried adding inDoEvents
before and after the API calls, along with addingSleep
between keystrokes because maybe then it would stop typing "v" and do Ctrl+V instead but that was wishful thinking.I have also attempted to create a separate Sub for everything executed after F12 is called, and scheduling that with
Application.OnTime
, but the clock for this does not start until after the Save As window opens.I have also tried adding in a
Do-While
loop immediately after the F12 call where itSleep
s until an Excel child window is detected usingFindWindowEx
. I would post this code for more detail but the window still did not open until after the loop timed out.
There is no end to this unless I am missing something very stupid. I may just set the computer on fire. Anything I might be doing wrong? Anyone having similar problems? Solutions?