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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this document open? 2

Status
Not open for further replies.

Cloonalt

Programmer
Jan 4, 2003
354
US
I pass a variable to the below to check if a document is open. If it's open, fine; however if it's not open, I get an error. I can trap the error, but is there a better way to check if there's a specific document already open?

If ActiveDocument = Request & ".doc" Then....

Thanks.

Any help appreciated.
 
Can you have it search for "~" at the beginning of doc name?

Chris
 
Code:
ActiveDocument = Request & ".doc" Then
only checks if the active document is Request.doc. If Request.doc is OPEN, but not the active one, then this would return a False.

I am not sure what you may be thinking of as "better".

All open documents are members of the Documents collection. No matter what, you are going to have to check against something, so again, I'm not sure what "better" means. It depends on whether you commonly have multiple documents open. One possibility could be:
Code:
Dim aDoc As Document
For Each aDoc In Documents
   If aDoc.Name = Request & ".doc" Then
      If ActiveDocument.Name = Request & ".doc" Then
         MsgBox "Yes it is open and the active document."
      Else
         MsgBox "Open but NOT active."
      End If
   End If
Next

Gerry
 
Gerry, thanks for your help.

I get an 'ActiveX component can't create object' error 429

on that code.

Is that because there's no Word document open? That's also a possibility.

I need to check first if any Word docs are open and then loop through like your code does.
 
**Just Missing one Word


Dim aDoc As Word.Document **
For Each aDoc In Documents
If aDoc.Name = Request & ".doc" Then
If ActiveDocument.Name = Request & ".doc" Then
MsgBox "Yes it is open and the active document."
Else
MsgBox "Open but NOT active."
End If
End If
Next
 
I always like to use a simple function to test of a file is open or not ..


Code:
Function IsDocOpen(docName As String) As Boolean
    On Error Resume Next
    IsDocOpen = Len(Documents(docName).Name)
End Function

Sub test()
    MsgBox IsDocOpen("Document2.doc")
End Sub

HTH

-----------
Regards,
Zack Barresse
 
Thanks, but it looks like that may not work if there's more than one document open.
 

Personally I prefer to cater for the error and use something like ..
Code:
[blue]Function DocumentIsOpen(DocumentName As String) As Boolean

    Dim TempDoc As Document
    
    On Error Resume Next
    Set TempDoc = Documents(DocumentName)
    On Error GoTo 0
    
    DocumentIsOpen = Not TempDoc Is Nothing
    
    Set TempDoc = Nothing
    
End Function[/blue]
Then you can use
Code:
[blue]If DocumentIsOpen("Request.doc") Then[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Having a broadly useable function would be best. Although the function would be more useable if it also tested on whether the document (if open) is the active one, or not. More often than not, the document we are checking for will be the one we want to perform action on. In which case, knowing if it is active (or not) can be helpful.

Gerry
 
I'm going crazy. Below is returning falsel when it should be returning true. Can anyone see why?

Request = Me.txtSearchDesc

If DocumentIsOpen(Request & ".doc") = True Then
MsgBox "Word document " & Request & Chr(13) & Chr(10) & "is open. Please close.", vbCritical, "Document Exists and is Open"
Exit Sub
End If


Function DocumentIsOpen(DocumentName As String) As Boolean

Dim TempDoc As Document

On Error Resume Next
Set TempDoc = Documents(DocumentName)
On Error GoTo 0

DocumentIsOpen = Not TempDoc Is Nothing

Set TempDoc = Nothing

End Function
 
Have you tried my function as well? If that fails also, check each character of your filename ensuring a match.

-----------
Regards,
Zack Barresse
 
Thanks, your function is returning false also. And I can't see why.

When I do ?Word.ActiveDocument in the Immediate Window I get

'Runtime Error 4248. The command is not available because no document is open'

even though the document is open.

Oh boy.
 
Uh, Request = Me.txtSearchDesc will return the control, NOT the value of the control.

Request has to be a string!

Assuming Request is declared as a String, then Request = Me.txtSearchDesc.Text. If Request is declared as a Variant (which it would be if you did Dim Request), then again, the code would make it the control, NOT the text value.

Gerry
 
In the immediate window Me.txtSearchDesc returns CC049365
and me.txtSearchDesc.Text returns CC04935

I Dim Request as string
and do
Request = Me.txtSearchDesc

I use Request as a string elsewhere in my code.
 
In the immediate window

?Documents(docname).Name

returns Run-time error 4160 'bad file name'
 

Word seems to think you don't have a document open so a couple of questions.

Are you running your code from within Word?
Do you only have a single instance of Word running?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thanks for your reply.

I'm running the code from VB.
I have only one instance of Word running
 

I'm sorry but multiple instances is all I can think of to explain the 4248 error - it's a fairly explicit error and if you can see the document on your screen that's fairly explicit as well. It's possible that the 'bad file name' error could be caused by other things but not the 'no document open' so I don't know what else to suggest at the moment.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
If you're running from VB (as opposed from the VBE via VBA) don't forget to explicitly reference the [Word] application.

-----------
Regards,
Zack Barresse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top