SOLVED
Home

Activating or shifting focus from Access to Word

%3CLINGO-SUB%20id%3D%22lingo-sub-1031861%22%20slang%3D%22en-US%22%3EActivating%20or%20shifting%20focus%20from%20Access%20to%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1031861%22%20slang%3D%22en-US%22%3EActivating%20or%20shifting%20focus%20from%20Access%20to%20Word%3CBR%20%2F%3E%3CBR%20%2F%3EDear%20Sir%3CBR%20%2F%3EI%20have%20an%20Access%20DB%20in%20which%20I%20have%20stored%20Addresses%20of%20clients%2C%20I%20have%20concatenated%20all%20address%20fields%20in%20a%20Memo%20field.%20I%20have%20placed%20a%20button%20on%20the%20form%20which%20when%20I%20click%20copies%20the%20address%20from%20Memo%20field%20onto%20clipboard%2C%20this%20is%20working%20fine.%20I%20have%20another%20button%20which%20when%20I%20click%20shifts%20the%20focus%20to%20an%20already%20opened%20word%20document.%20The%20problem%2Fs%20is%2Fare%20sometimes%20this%20works%20fine%20i.e%20when%20I%20click%20button%20focus%20from%20Access%20shifts%20to%20Word%2C%20but%20sometimes%20it%20doesn't%20work%20at%20all.%20The%20other%20scenario%20when%20it%20doesn't%20work%20is%20when%20word%20document%20is%20minimised.%20I%20can%20see%20toggle%20happening%20in%20taskbar%20but%20focus%20doesn't%20shifts.%20I%20am%20using%20following%20code%20attached%20with%20click%20event%20of%20button%3A%3CBR%20%2F%3E%3CBR%20%2F%3EApplication.activate%20%22Microsoft%20Word%22%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20when%20word%20document%20is%20not%20open%20it%20gives%20error.%3CBR%20%2F%3E%3CBR%20%2F%3EAny%20help%20would%20be%20much%20appreciated.%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you.%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1031861%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E2010%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3E2016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVBA%20CODE%20to%20switch%20focus%20from%20Access%20form%20to%20Word%20Document%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1034079%22%20slang%3D%22en-US%22%3ERe%3A%20Activating%20or%20shifting%20focus%20from%20Access%20to%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1034079%22%20slang%3D%22en-US%22%3EDear%20George%20Hepworth%2C%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%20for%20your%20reply%20and%20suggestions.%3CBR%20%2F%3ELet%20me%20paint%20you%20whole%20picture.%3CBR%20%2F%3E%3CBR%20%2F%3E1%20The%20document%20(it%20is%20an%20appointment%20letter)%20which%20needs%20address%20pasting%20is%20downloaded%20from%20an%20in%20house%20web%20based%20software%2C%20the%20document%20is%20not%20allowed%20to%20be%20saved%20on%20local%20machine%2C%20it%20is%20temporarily%20loaded%20for%20the%20purpose%20of%20printing%20and%20editing%20and%20ineeds%20to%20be%20uploaded%20back%20to%20the%20same%20software.%3CBR%20%2F%3E2.%20Because%20of%20changing%20nature%20of%20addresses%20they%20could%20not%20be%20attached%20with%20the%20online%20database.%3CBR%20%2F%3E3.%20It%20is%20a%20long%20list%20of%20addresses%2C%20large%20number%20of%20them%20have%20similar%20sort%20of%20words%20in%20them%20and%20these%20lists%20are%20stored%20in%20Word%20table%20and%20one%20address%20is%20in%20multiple%20rows.%20Searching%20and%20copy%20pasting%20is%20difficult.%3CBR%20%2F%3E4.%20What%20I%20have%20done%20is%20imported%20all%20address%20in%20Access%20Table%20and%20then%20concatenated%20them%20in%20a%20memo%20field.%20Then%20with%20a%20search%20form%20which%20has%20combo%20box%20containing%20code%20names%20I%20display%20the%20whole%20address%20in%20memo%20field%20on%20a%20resultant%20form%2C%20the%20address%20is%20then%20copied%20on%20clipboard%20by%20clicking%20a%20button%2C%20then%20by%20clicking%20another%20button%20the%20focus%20should%20be%20shifted%20to%20already%20downloaded%20(opened)%20document%20and%20then%20ultimately%20copied%20address%20is%20pasted%20by%20the%20user%20at%20a%20specific%20place%20in%20document.%3CBR%20%2F%3E%3CBR%20%2F%3E5.%20Everything%20is%20working%20except%20shifting%20the%20focus%20to%20already%20opened%20and%20displayed%20document.%3CBR%20%2F%3E6.%20We%20can't%20do%20the%20mail%20merge%20as%20document%20is%20individually%20downloaded%20from%20web-based%20software%2C%20it%20cannot%20be%20saved%20locally%2C%20and%20are%20individually%20uploaded%20back%20after%20printing%20and%20editing.%20Please%20keep%20in%20mind%20these%20are%20individual%20appointment%20letters.%3CBR%20%2F%3E6.%20My%20Software's%20sole%20purpose%20is%20to%20search%20address%20and%20copy%20paste%20in%20the%20opened%20document.%3CBR%20%2F%3E%3CBR%20%2F%3EHope%20above%20would%20help%20you%20to%20understand%20my%20predicament.%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%3CBR%20%2F%3ERegards.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1034453%22%20slang%3D%22en-US%22%3ERe%3A%20Activating%20or%20shifting%20focus%20from%20Access%20to%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1034453%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F468421%22%20target%3D%22_blank%22%3E%40Asad_khan1971%3C%2FA%3E%26nbsp%3BWhat%20you%20need%20is%20Word%20Automation...essentially%20you%20instruct%20Word%20where%20to%20place%20the%20text%20you%20want...and%20it%20produces%20the%20final%20document%20with%20the%20info%20from%20Access%20but%20with%20the%20Formatting%20from%20Word.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1034666%22%20slang%3D%22en-US%22%3ERe%3A%20Activating%20or%20shifting%20focus%20from%20Access%20to%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1034666%22%20slang%3D%22en-US%22%3EWith%20the%20greatest%20of%20respect%2C%20I%20don't%20need%20more%20than%20shifting%20the%20focus%20from%20Access%20to%20Word%20after%20I%20have%20copied%20the%20address.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20think%20either%20I%20am%20not%20able%20to%20convey%20my%20problem%20or%20may%20be%20it%20is%20not%20possible%20to%20do%20what%20I%20want%20I%20my%20situation%20i.e.%20shifting%20the%20focus%20from%20Access%20to%20Word.%3CBR%20%2F%3EAnyway%20thank%20you%20very%20much%20for%20your%20suggestions%20which%20may%20be%20helpful%20in%20another%20situation.%3CBR%20%2F%3ERegards%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1036370%22%20slang%3D%22en-US%22%3ERe%3A%20Activating%20or%20shifting%20focus%20from%20Access%20to%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1036370%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F468421%22%20target%3D%22_blank%22%3E%40Asad_khan1971%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20is%20that%20%22shifting%20focus%22%20from%20one%20running%20application%20to%20another%2C%20in%20this%20case%2C%20from%20Access%20to%20Word%2C%20is%20not%20a%20reliable%20method%2C%20so%20a%20different%20approach%20is%20likely%20to%20be%20safer%20and%20more%20stable.%20That%20said%2C%20your%20specific%20circumstances%20do%20restrict%20your%20options%20as%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20you'll%20need%20to%20do%20is%20obtain%20the%20Windows%20Handle%20for%20the%20open%20Word%20document%20(there%20should%20be%20%3CA%20href%3D%22http%3A%2F%2Faccess.mvps.org%2Faccess%2Fapi%2Findex.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ea%20Windows%20API%20to%20do%20that%3C%2FA%3E)%20and%20use%20that%20information%20to%20select%20and%20activate%20it.%20Not%20something%20I've%20done%2C%20but%20I%20suspect%20it%20is%20possible.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Asad_khan1971
Occasional Contributor
Activating or shifting focus from Access to Word

Dear Sir
I have an Access DB in which I have stored Addresses of clients, I have concatenated all address fields in a Memo field. I have placed a button on the form which when I click copies the address from Memo field onto clipboard, this is working fine. I have another button which when I click shifts the focus to an already opened word document. The problem/s is/are sometimes this works fine i.e when I click button focus from Access shifts to Word, but sometimes it doesn't work at all. The other scenario when it doesn't work is when word document is minimised. I can see toggle happening in taskbar but focus doesn't shifts. I am using following code attached with click event of button:

Application.activate "Microsoft Word"

And when word document is not open it gives error.

Any help would be much appreciated.

Thank you.
5 Replies

@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?

Dear 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.

@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.

With the greatest of respect, I don't need more than shifting the focus from Access to Word after I have copied the address.

I think either I am not able to convey my problem or may be it is not possible to do what I want I my situation i.e. shifting the focus from Access to Word.
Anyway thank you very much for your suggestions which may be helpful in another situation.
Regards
Solution

@Asad_khan1971 

 

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.

 

 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies