Forum Discussion
Activating or shifting focus from Access to Word
- Nov 27, 2019
The problem is that "shifting focus" from one running application to another, in this case, from Access to Word, is not a reliable method, so a different approach is likely to be safer and more stable. That said, your specific circumstances do restrict your options as well.
What you'll need to do is obtain the Windows Handle for the open Word document (there should be a Windows API to do that) and use that information to select and activate it. Not something I've done, but I suspect it is possible.
Asad_khan1971This is a pretty high risk strategy, as you're learning.
It will be much more effective to fully automate this entire process from Access VBA.
In other words, instead of relying on application Windows (which may or may not get focus as you expect), directly open the proper Word document from the Access VBA module and set a reference to that document. Then copy the appropriate text directly into that open Word document.
I can think of at least two ways to implement this, depending in part on what the end goal is and what the content of the Word doc is.
Have you considered using the Mail Merge feature of Word to retrieve the data from your Access accdb?
- Asad_khan1971Nov 26, 2019Copper ContributorDear George Hepworth,
Thank you for your reply and suggestions.
Let me paint you whole picture.
1 The document (it is an appointment letter) which needs address pasting is downloaded from an in house web based software, the document is not allowed to be saved on local machine, it is temporarily loaded for the purpose of printing and editing and ineeds to be uploaded back to the same software.
2. Because of changing nature of addresses they could not be attached with the online database.
3. It is a long list of addresses, large number of them have similar sort of words in them and these lists are stored in Word table and one address is in multiple rows. Searching and copy pasting is difficult.
4. What I have done is imported all address in Access Table and then concatenated them in a memo field. Then with a search form which has combo box containing code names I display the whole address in memo field on a resultant form, the address is then copied on clipboard by clicking a button, then by clicking another button the focus should be shifted to already downloaded (opened) document and then ultimately copied address is pasted by the user at a specific place in document.
5. Everything is working except shifting the focus to already opened and displayed document.
6. We can't do the mail merge as document is individually downloaded from web-based software, it cannot be saved locally, and are individually uploaded back after printing and editing. Please keep in mind these are individual appointment letters.
6. My Software's sole purpose is to search address and copy paste in the opened document.
Hope above would help you to understand my predicament.
Thank you
Regards.