Word Not Activating - Sometimes (1 Viewer)

bsiemsen

Registered User.
Local time
Today, 06:55
Joined
Jul 23, 2014
Messages
14
Hey guys I have an INTERMITTENT problem. I am calling MS Word from a form command button and sometimes Word opens correctly and opens on top of Access, other times it activates on the taskbar but stays behind the Access form, and I have to click on the Taskbar to open the doc. I have no idea what is the trigger that makes it work - sometimes. Here is the code:


LWordDoc = "C:\MyForm.doc"
Set WApp = CreateObject(Class:="Word.Application")
WApp.Visible = True
WApp.Documents.Open filename:=LWordDoc
Wapp.Activate

I am using MS Access 2000 and MS Word 2000 running under Windows 7 Pro. I have tried all variations and scoured the net and tried all forms of code and the only thing I don’t understand is how the Running Object Table (ROT) may or may not be a factor.
Thoughts??
Thanx
Bill
BTW, Happy Holidays
 

sneuberg

AWF VIP
Local time
Today, 06:55
Joined
Oct 17, 2014
Messages
3,506
I vaguely remember that happening to us. Anyway I think that's the reason I added

Code:
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

at the beginning of the form module and

Code:
SetForegroundWindow WApp..ActiveWindow.hwnd

after
Code:
Wapp.Activate

Note that if you use 64 bit Office the Declare statement will probably have to be modified.
 

bsiemsen

Registered User.
Local time
Today, 06:55
Joined
Jul 23, 2014
Messages
14
Hey Steve,
No Joy.
This is my modified code:
Option Compare Database
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
.
.
(the command button - btnTest).
Private Sub btnTest_Click()
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
oApp.Documents.Open filename:=LWordDoc
oApp.Activate
SetForegroundWindow oApp.ActiveWindow.hwnd

And this is the error msg - "object does not support this property or method"

The MS Word doc still is activated in the Taskbar, it just does not present in front of the Access form.
:-(
BTW, I’m jes down the street in Yuma - its cold here too :)
 

sneuberg

AWF VIP
Local time
Today, 06:55
Joined
Oct 17, 2014
Messages
3,506
I might be a version thing. I have Access 2013. The following code runs without error on my system and displays the Word document on top, albeit at the moment it displays it on top even without SetForegroundWindow oApp.ActiveWindow.hwnd

Code:
Option Compare Database
Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Sub w()

Dim LWordDoc As String
Dim oApp As Word.Application
LWordDoc = "C:\Users\sneuberg\Desktop\MyForm.doc"
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
oApp.Documents.Open FileName:=LWordDoc
oApp.Activate
SetForegroundWindow oApp.ActiveWindow.hwnd

End Sub

When it stops with the error "object does not support this property or method" is the debugger pointing to SetForegroundWindow oApp.ActiveWindow.hwnd? How do you have oApp declared? The code above was compiled with early binding, i.e., I added the Microsoft Word Object Library reference to the database.
 

bsiemsen

Registered User.
Local time
Today, 06:55
Joined
Jul 23, 2014
Messages
14
Rats!
It still doesn’t like me. I did a cut/paste and still no luck. The debugger stops on the "SetForegroundWindow oApp.ActiveWindow.hwnd", and this time the program stops at the dim "Dim oApp As Word.Application" which, like you said, could be a version thing.

The Microsoft Word Object Library is set for Version 9 which is what I am running, so no issues there. The original code works after I leave the program, then send an email, or print a picture, or anything else where the OS has to get away from Access. I just have a feeling that it is not an Access problem but the way Win 7 handles applications. Any thoughts??
 

bsiemsen

Registered User.
Local time
Today, 06:55
Joined
Jul 23, 2014
Messages
14
Me Again.
I wrote another button with the code "msgboxxx ‘here’" which of course blows up. But, after it blows up and I close the debugger, the original code calling MS Word works fine. Got me beat.
 

bsiemsen

Registered User.
Local time
Today, 06:55
Joined
Jul 23, 2014
Messages
14
Hey Steve after a lot of digging I found a solution (don't remember who to thank) but thought I’d share the answer. Added "AppActivate" at the end.


Set oapp = CreateObject(Class:="Word.Application")
oapp.Visible = True
oapp.Documents.Open FileName:=LWordDoc
AppActivate "Microsoft Word"

I looks like "AppActivate" sets the focus on the Word program which brings it in front of the calling Access form. It may work for Excel, PowerPoint, and maybe even WordPerfect or other vendors. Don't know. Anyway now we both know. Thanx for the effort. Bill :)
 

Users who are viewing this thread

Top Bottom