Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2021, 10:01 AM
daneyuleb daneyuleb is offline Run Code when document is Activated? Or clicked anywhere in it? Windows 10 Run Code when document is Activated? Or clicked anywhere in it? Office 2016
Novice
Run Code when document is Activated? Or clicked anywhere in it?
 
Join Date: Dec 2021
Posts: 3
daneyuleb is on a distinguished road
Default Run Code when document is Activated? Or clicked anywhere in it?

Is there a way to have a word document run a vba function whenever it's activated--and by activated, I mean--the document is already open, but the user has switched to another application then switched back to the document.



Alternatively, any method to run the vba code when, in switching back to word, the document is clicked on (anywhere in the document--not a specific button or text)?

The usage case here is -- user has opened word doc, switched to another application and possibly put some text from it in the Windows clipboard. As soon as they switch back to word (and the already open doc) I have code that will check the clipboard for specific strings and do some stuff if it sees those strings. The user could be anywhere in the document and this needs to be transparent to the user and require no extra clicks--meaning they won't be able to go find a macro button and then click it to activate this clipboard check.

I know I could run it on open, but is there any way to do it on "activate" like Excel has when a sheet is made active?
Reply With Quote
  #2  
Old 12-17-2021, 10:25 AM
daneyuleb daneyuleb is offline Run Code when document is Activated? Or clicked anywhere in it? Windows 10 Run Code when document is Activated? Or clicked anywhere in it? Office 2016
Novice
Run Code when document is Activated? Or clicked anywhere in it?
 
Join Date: Dec 2021
Posts: 3
daneyuleb is on a distinguished road
Default

OK, figured it out. Followed MS's instructions on creating an event-handler object:

Using events with the Application object (Word) | Microsoft Docs

In case anyone else ever needs something like this, it was simple:

Create a class module:

Insert -> Class Module

In the View -> Properties window, give your new module a name. (I just used MS's example: EventClassModule )

In the class module, put this at the top to declare an object with events:

Code:
Public WithEvents App As Word.Application
Click your new object ("App") in the Object dropdown, and you'll see all the interesting events you can use with your new object in the Procedures drop-down to its right -- there's lots! I picked the WindowActivate one for what I needed:

Code:
Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)

MsgBox "Active!"

End Sub
The last thing is to connect your new class object ("App") to the Word.Application object. Which is just this code in any module.
Code:
Dim X As New EventClassModule

Sub Register_Event_Handler()

Set X.App = Word.Application

End Sub
Run this Register_Event_Handler sub, and the Event becomes active. Each time I come back to the word doc from another app, it triggers! Perfect!
Reply With Quote
  #3  
Old 10-15-2022, 08:57 AM
PatrickOfLondon PatrickOfLondon is offline Run Code when document is Activated? Or clicked anywhere in it? Windows 11 Run Code when document is Activated? Or clicked anywhere in it? Office 2016
Novice
 
Join Date: Oct 2022
Posts: 3
PatrickOfLondon is on a distinguished road
Default Run Word VBA macro whan a Word document is activated or re-activated...?

This was really useful, thank you.

I would like to know if there is a similar method for running a macro when a Document is activated or re-activated (rather than when the Word Application window itself is activated). Is there any way of doing that?

What I'm hoping for is something that works similarly to
Private Sub Worksheet_Activate()
for a sheet in an Excel workbook, so that the macro fires when a Word document is activated or re-activated while the Word Application remains running.

With thanks in anticipation...
Reply With Quote
  #4  
Old 10-15-2022, 09:42 AM
daneyuleb daneyuleb is offline Run Code when document is Activated? Or clicked anywhere in it? Windows 10 Run Code when document is Activated? Or clicked anywhere in it? Office 2016
Novice
Run Code when document is Activated? Or clicked anywhere in it?
 
Join Date: Dec 2021
Posts: 3
daneyuleb is on a distinguished road
Default

If I'm understanding you correctly, it seems it would work if you use the "DocumentChange" procedure (in that second step) rather than WindowActivate. So the sub (in the class module) looks like this:



Private Sub App_DocumentChange()
MsgBox "changed!"
End Sub

Everything else remains the same. This fires the sub every time you switch from one doc to another without leaving Word.
Reply With Quote
  #5  
Old 10-21-2022, 10:18 AM
PatrickOfLondon PatrickOfLondon is offline Run Code when document is Activated? Or clicked anywhere in it? Windows 11 Run Code when document is Activated? Or clicked anywhere in it? Office 2016
Novice
 
Join Date: Oct 2022
Posts: 3
PatrickOfLondon is on a distinguished road
Default

Thank you again, it does work as you suggested, with the DocumentChange event procedure.

Extremely useful, and seemingly undocumented anywhere else, at least in any helpful way, as far as I have found. Greatly appreciated.
Reply With Quote
Reply

Tags
activate



Similar Threads
Thread Thread Starter Forum Replies Last Post
This copy of MS Office not activated chzuck Office 1 10-10-2021 03:10 PM
I clicked "this document" in VBA thinking it was a menu button, it said "saving", how can I reverse messgchr Word VBA 2 07-20-2020 06:52 PM
Is my Office 'activated' or not? mabel Office 8 08-01-2019 04:18 AM
Run Code when document is Activated? Or clicked anywhere in it? Retrieve the last activated document name. eduzs Word VBA 1 08-22-2017 03:11 PM
Hyperlink not activated by footpedal Donone PowerPoint 2 05-05-2014 04:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft