Nov 25 2019 10:59 AM - edited Nov 26 2019 05:40 AM
Nov 26 2019 08:22 AM
@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?
Nov 26 2019 09:06 AM
Nov 26 2019 12:22 PM
@Asad_khan1971 What you need is Word Automation...essentially you instruct Word where to place the text you want...and it produces the final document with the info from Access but with the Formatting from Word.
Nov 26 2019 02:15 PM
Nov 27 2019 07:53 AM - edited Nov 27 2019 07:55 AM
Solution
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.
Nov 27 2019 07:53 AM - edited Nov 27 2019 07:55 AM
Solution
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.