Run Macro when file window is opened

steve case

Well-known Member
Joined
Apr 10, 2002
Messages
823
I have a "gazillion" screens from another application I want to:

Select All, Copy, Open Excel, Find the next blank line, paste, return to the application, page down and repeat.

This application won't accept [SendKeys "^a^c", True] but at least I would like Excel to automatically run some code when I reopen it with [Alt+Tab]

So far this code:


Columns("A:A").Select
Selection.Find(What:=Empty, After:=ActiveCell).Select
ActiveSheet.Paste
If Err Then MsgBox "OK": Err.Clear
AppActivate "MyApplication"

doesn't run as a workbook macro on [Alt+Tab]

Any ideas?

Other suggestions on how to this are welcome (-:

.
.
.
.
.
.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I'm not stumbling across any events that fire when Excel receives focus. That's what you'd need. About the best I can think of would be to use the SelectionChange event handler, so when you flip back to Excel all you need to is hit any arrow or other movement key and it would fire it for you.
 
Upvote 0
This is not OVER Greg's reply. But.........
will the
Workbook_WindowActivate or
Workbook_Activate events help you.
Or might be these things are well over my head.
 
Upvote 0
shajueasow said:
This is not OVER Greg's reply. But.........
will the
Workbook_WindowActivate or
Workbook_Activate events help you.
Or might be these things are well over my head.

How did you test that? :unsure: Because when I drop the following code into a workbook's code module...

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Activate()
    ActiveCell = Now
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_WindowActivate(<SPAN style="color:#00007F">ByVal</SPAN> Wn <SPAN style="color:#00007F">As</SPAN> Window)
    ActiveCell.Offset(, 1) = "today is " & <SPAN style="color:#00007F">Date</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

...and then use Alt+Tab to flip between applications and then flip focus back to Excel, nothing happens, i.e. the Activate and WindowActivate events do not fire unless I am using Ctrl+Tab to toggle through windows within Excel. If I'm doing that then yes. But that's not what StACase described.
 
Upvote 0
Hi there,

This is a scenario where Subclassing XL can be useful.

Note that subclassing can be dangerous so please save your work before you try the code !

Place this in a Standard Module :

Code:
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Const GWL_WNDPROC  As Long = (-4)
Const WM_ACTIVATEAPP   As Long = &H1C

Dim OldWindowProc As Long
Dim hwnd As Long
Dim blnXLIsSubclassed As Boolean


Sub SubClass()

    'Subclass The Excel Application Window
    If Not blnXLIsSubclassed Then
        hwnd = Application.hwnd
        OldWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
        blnXLIsSubclassed = True
    End If
End Sub


Sub UnSubclass()

    'UnSubclass The Excel Application Window
    SetWindowLong hwnd, GWL_WNDPROC, OldWindowProc
    blnXLIsSubclassed = False
    
End Sub


Function NewWindowProc(ByVal hwnd As Long, ByVal Msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
    
    Dim blnXLActivated As Boolean
    If Msg = WM_ACTIVATEAPP Then
        blnXLActivated = wParam
        If blnXLActivated = True Then
            ActiveSheet.Paste Range("b4") '\\ Do other stuff too here
        End If
        Exit Function
    End If
    NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
   
End Function


Now, just run the SubClass Procedure, deactivate XL , Copy something from a different Application\Window, go back to excel and you will find that the contents of the ClipBoard are copied on to Cell B4 of the activesheet.

Don't forget to run the UnSubclass Procedure when you are done !!

Regards.
 
Upvote 0
Hot diggity - something new to learn! :p

Hi Jaafar!

I googled my way into this. Was there a specific reason why you did not include the test for WM_NCDESTROY in your NewWindowProc code? Is it not needed here? (Would the message be WM_NCDESTROY if the user decided to close Excel in our context here? Or would this be caused by something else?)

Thanks so much for stopping by with this tip! (y) I don't know beans about API calls, so this kind of stuff is always fun for me to read.
 
Upvote 0
Hi Greg,

As its name implies, the WM_NCDESTROY message is sent to a Window when it is being destryoed.We didn't need that in the OP's scenario.Instead, we wanted to catch the WM_ACTIVATEAPP message which is sent when an Appliation Window is Activated/Deactivated and whose wParam value cahanges accordingly.

Subclassing is a very powerful technic but can be difficult to implement in XL.It also cariies a potential for crashing the system.

Regards.
 
Upvote 0
rafaaj2000 said:
...We didn't need that in the OP's scenario...Subclassing is a very powerful technic...It also carries a potential for crashing the system...
I can see how powerful this could be. And yes, the link I cited had a long warning on how this can crash the VBE very nicely.

Given it's potential for wreaking havoc, I initially thought that putting the WM_NCDESTROY test in there would be prudent in case the user forgot to run UNSUBCLASS before trying to close Excel.

But then I begin to wonder if that matter with VBA? If you are closing Excel, then by default the VBE for Excel closes simultaneously; so unless there were a danger of corrupting a file or something if the VBE crashes to the ground, would it be anything other than a possible nuisance?

[RAMBLEMODE=OFF]

Thanks again for the info!

Regards,
 
Upvote 0
For some reason, my post from earlier today didn't "take"

Well I'm home now and I don't have the code I came up with, but it involved an " Onkey " command for the PageDown key that ran a macro that did the paste and ended with and Appactivate statement back to my application.

I wound up with the routine I described in my initial post except that I had to press "PageDown" twice instead of once.

I pasted all "gazillion and one" pages of data to Excel before noon, and the user is happy (-:

.
.
.
.
.
 
Upvote 0

Forum statistics

Threads
1,216,377
Messages
6,130,265
Members
449,569
Latest member
sukming

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top