Get Word to the foreground

T

TTD

Hello,

I need to close Word from Excel VBA. I have the code. But sometimes a user
has an open file in Word and during closing Word asks to save it. But when
Word is on the background and another application is in front of it, the
user doesn't see the questiondialog and Excel can't close Word.

I've found some code to get an application to the foreground, but It doesn't
seem to work.

' **************** Declare API Function *********************************
Declare Function SetForegroundWindow Lib "user32" Alias
"SetForegroundWindow" _
(ByVal hwnd as Long) as Long

' *************** Command to bring our Window to the foreground
**************
Public Sub SetForeground(hWnd as Long)
SetForegroundWindow hWnd
End Sub


Does anyone have any ideas?

Thanks for your time and attention.
 
T

Tom Ogilvy

why not loop through the documents and close them (savechanges:=false or
true) or have your code put up a prompt to ask the user.
 
T

TTD

Problem is that I don't know if the user wants to save it or not. Sometimes
it's important and they want to save it ... sometimes it's something totally
irrelevant and then the don't want to save it.

When the user has changed the document, the dialogbox automatically appears
to ask the user if (s)he wants to save it. But the problem is that Word is
not on the forground.

The user opens my Excel macro from the mail I've sent. So Outlook is in
front of Word.
 
T

Tom Ogilvy

Perhaps you should use createobject to create a new instance of word and
avoid the problem. If the user already has word open, then you probably
shouldn't try to close it anyway (so another alternative would be to test if
it is already open in your code. See the help example for getobject in
Excel VBA help for sample code - then it it was, don't try to close it -
just close the document you created).
 
T

TTD

Wish I could, but the reason I want to close Word (and Outlook, for some
users use Word as email editor) is that I want to overwrite a .dot file in
the word startup directory. I am updating a housestylewizard, but when Word
is open, I can't overwrite it, for the .dot file is in use.

That's why I use Excel to do the copying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top