Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to know if a word document is opened??

Status
Not open for further replies.

mciing7912

Programmer
Jul 14, 2006
16
MX
Hi,

I created a VB application but I need to close all previous WORD documents before to run my application.

for example, if you opened a MEMORANDUM or something like that, I need to validate it in my program and say:

"This application cannot run until you close all your active Word documents"

any idea??

thanks a lot!
 

I have a reference to Microsoft Word 11.0 Object Library, so I use this:

Module Code:
Code:
Option Explicit

[green]'API declarations to check if MSWord is open[/green]
Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
     ByVal lpClassName As Any, _
     ByVal lpWindowName As Any) As Long

Public Function IsMSWordUp() As Boolean
    [green]'Function checks if MSWord is running[/green]
    IsMSWordUp = (FindWindow("OpusApp", vbNullString) <> 0)
End Function

Code in Form:
Code:
Private Sub Command1_Click()
    MsgBox IsMSWordUp
End Sub

There may be a better way. Anybody.....? :)

Have fun.

---- Andy
 
I remember I posted similar code long time back in thread222-503791.

I think the OP wants to know if any active documents are open. You can have MS Word running without any open document. In this case the application should be allowed to run.

I suggest obtaining a reference to an active MS Word session and checking the number of documents would be more appropriate.
___
[tt]
Private Function IsAnyWordDocumentActive() As Boolean
On Error Resume Next
Dim Word As Object
Set Word = GetObject(, "Word.Application")
If Not Word Is Nothing Then IsAnyWordDocumentActive = Word.Documents.Count
End Function[/tt]
 
You can use Hypetia's to interact with the user. If you want to be more pre-emptive, you can simply change to this:
Code:
Private Function WhackAnyWordDocs() As Boolean
    On Error Resume Next
    Dim Word As Object
    Set Word = GetObject(, "Word.Application")
    If Not Word Is Nothing Then Word.Quit False
End Function
To pre-emptively close existing documents one by one:
Code:
Do Until Word.documents.Count = 0
    Word.documents(1).Close False
Loop
In both cases, adding true instead of false will save the existing document.
 
I don't think I'd like any application that went around closing my Word documents without my permission ...
 
Nope, me neither. I'm learning the spiritual lesson of honorable service to a corrupt master...
 
As word puts a write lock on each file it opens, surely you can just have your application attempt to open the file for write. If it fails then you know the file it open ?
 
1) You don't know whether it is open in Word though ...
2) OP suggests that they don't know what particular Word documents might be open, just that some might be
 
You're quite right, my mistake. It's Monday morning and I've not had my 3 cups o'rocket fuel yet :)

While I don't have an alternative suggestion, I would like to offer a warning with the above code. MS Outlook can be configured to use Word as it's default editor. In this case Word may be loaded (with an active document) but completely hidden to the user.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top