Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    test if document is open (Word97/SR2)

    I want to test if a document that was open a few seconds ago on my desktop is still open.

    Not as silly as it sounds - I'm processing a set of documents, one after the other, and allowing the user to nominate a user macro to augment the process. The user macro might contain "activedocument.close", which means if I continued doing my stuff on the activedocument after regaining control from the user macro, I'd be doing it on the wrong document.

    (search for if document is open)

    I'm not worried about the document being already open on another desktop (although the KB article for "Function FileLocked(strFileName As String) As Boolean" is excellent); I'm worried about losing the document due to a process outside my control.

    I'd thought of testing a document property, but sad to relate, there isn't an "Document.Exists", hah hah!

    In the meantime I've used the VBA Help file's method of checking, while wondering whether a Property Guru might have a better method, that doesn't involve a loop.


    <pre>Public Function blnDocIsOpen(ByVal strDocName As String) As Boolean
    ' Procedure : blnDocIsOpen
    ' Description: Determine if the document is available on the desktop.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING full or local filename and path.
    ' Returns: TRUE if file is now open.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    ' Remarks: None.
    blnDocIsOpen = False ' Force default result is "failure"
    strDocName = UCase(strDocName) ' Force case-insensitive search
    Dim doc As Document
    For Each doc In Documents
    If UCase(doc.Name) = strDocName Or UCase(doc.FullName) = strDocName Then
    blnDocIsOpen = True ' Success. Quit operations
    Exit Function
    Else
    End If
    Next doc
    '
    'Sub TESTblnDocIsOpen()
    'MsgBox blnDocIsOpen("eraseme.doc")
    'MsgBox blnDocIsOpen("d:greavesadmineraseme.doc")
    'End Sub
    '
    End Function
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if document is open (Word97/SR2)

    Off the top (no testing, sorry):

    If windows(strFullName).Caption <> "" then
    msgbox strFullName & " is open."
    else
    'it's not
    endif
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if document is open (Word97/SR2)

    Thankls, but's that a big 5941, good buddy. "The requested member of the collection does not exist"



    I had the same problem in codsidering Proprties of the Document(s). In general, if the object doesn't exist, you get an error message (have I told you how much I hate On Error?), and since there is no test .Exists, one has to examine the literal list of the collection.

    I'd love to be proved wrong ... (unusual for me, I know....)

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: test if document is open (Word97/SR2)

    Might as well stick to the loop you posted.
    If you were going to build an .Exists method into a class, that's probably what the code in the Exists method would do anyway.....

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: test if document is open (Word97/SR2)

    Chris,
    How about this (yeah, I know you hate on error! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>):
    <pre>Function IsOpen(strDocName As String) As Boolean
    On Error GoTo err_not_open
    Dim docTemp As Document
    Set docTemp = Documents(strDocName)
    Set docTemp = Nothing
    IsOpen = True
    Exit Function

    err_not_open:
    Err.Clear
    IsOpen = False
    End Function
    </pre>

    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if document is open (Word97/SR2)

    > Chris,
    > How about this (yeah, I know you hate on error! (grin)):
    > Function IsOpen(strDocName As String) As Boolean


    Excellent. Thanks.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if document is open (Word97/SR2)

    > Might as well stick to the loop you posted.


    But see Rory's Function IsOpen(strDocName As String) As Boolean which is better than mine, 'though I say so myself!

    It's yet another example of my background - you have to program every thing yourself - and FORTRAN ("any code that doesn't have a loop is not really a program") - being eclipsed by the inbuilt methods that are available, and of which I try to avail myself.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: test if document is open (Word97/SR2)

    If you don't mind forcing the error, then Rory's method's certainly more direct. I don't have enough of a background as a codie to have a view on forcing an error, but I do like the most direct route between two points!

    AFAIK there are very few built-in .Exists methods in Word VBA - Bookmarks and Tasks are the only classes I know of that have it. If you had to build a generic Exists function that you could use to check the existence of say - anything - then Rory's method would probably be the way to do it, rather than a loop.

    Gary

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Generic 'Exists' function(Word97/SR2)

    Here's a first crack at a generic 'Exists' function a la Rory - this will accept a name or an index. The Select Case method is a bit clunky but don't think there's any way 'round that:

    <pre>Public Function fExists(ObjectType As String, _
    Optional ObjectName As String, _
    Optional ObjectIndex As Long) As Boolean
    On Error GoTo Error_DoesNotExist

    Select Case ObjectType
    Case "Style"
    Dim styleTemp As Style
    Set styleTemp = ActiveDocument.Styles(ObjectName)
    Set styleTemp = Nothing
    fExists = True
    Exit Function
    Case "Document"
    Dim docTemp As Document
    Set docTemp = Documents(ObjectName)
    Set docTemp = Nothing
    fExists = True
    Exit Function
    Case "Table"
    Dim tableTemp As Table
    Set tableTemp = ActiveDocument.Tables(ObjectIndex)
    Set tableTemp = Nothing
    fExists = True
    Exit Function
    End Select

    Error_DoesNotExist:
    Err.Clear
    fExists = False

    End Function
    '=====================
    Public Sub TestIfStyleExists()
    Dim strStyleName As String
    Dim boolResult As Boolean
    strStyleName = "OutlandishStyle"
    boolResult = fExists("Style", strStyleName)
    MsgBox strStyleName & " Exists = " & boolResult
    End Sub
    '=====================
    Public Sub TestIfDocumentExists()
    Dim strDocName As String
    Dim boolResult As Boolean
    strDocName = "C:TEMPDummyDocAssembledLetterContent.doc"
    boolResult = fExists("Document", strDocName)
    MsgBox strDocName & " Exists(IsOpen) = " & boolResult
    End Sub
    '======================
    Public Sub TestIfTableExists()
    Dim lngTableIndex As Long
    Dim boolResult As Boolean
    lngTableIndex = 5
    boolResult = fExists("Table", , lngTableIndex)
    MsgBox "Table #" & lngTableIndex & " Exists = " & boolResult
    End Sub
    </pre>

    Gary

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Generic 'Exists' function(Word97/SR2)

    Maybe it's a simple-minded question (I almost never program in Word VBA) but why don't you dim a single objTemp As Object before the Select Case statement and use that instead of the various Dim somethingTemp as something declarations? It should hold any of the Word objects you're testing for and would eliminate the need for individual Dim statements and Set somethingTemp = Nothing in each case. Then after the End Select, just set objTemp = Nothing.
    Charlotte

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Generic 'Exists' function(Word97/SR2)

    Charlotte,

    Have just tried your suggestion and it does work.
    The way it's currently constructed, though, you'd still need to keep all of the individual Set objTemp = Nothing statements in each Case test, because you want to set it to Nothing before the Exit Function.
    It could be done differently by having a single point of exit for the function and substituting "GoTo Exit_fExists" for the ExitFunction.

    What I'd really like to be able to do would be to pass the object type automatically as one of the arguments for the function, but I can't imagine how to do that.

    Gary

  12. #12
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Generic 'Exists' function(Word97/SR2)

    But why do you have all those Exit Functions in there in the first place? Just put one Exit Function right before Error_DoesNotExist. Since only one case can execute, the code will automatically drop to the next statement after the End Select unless it hits an error. All you need is a single Exit Function in the right place to end the routine gracefully.

    I don't understand what you mean by passing the object type automatically. You could create an object variable of that type and pass it into the function as an argument, but I don't see that it would buy you anything.
    Charlotte

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Generic 'Exists' function(Word97/SR2)

    Hey, I did say it was a first crack, didn't I? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    You're absolutely right, and the following works fine:

    <pre>Public Function fExists(ObjectType As String, _
    Optional ObjectName As String, _
    Optional ObjectIndex As Long) As Boolean
    On Error GoTo Error_DoesNotExist
    Dim objTemp As Object
    Select Case ObjectType
    Case "Style"
    Set objTemp = ActiveDocument.Styles(ObjectName)
    fExists = True
    Case "Document"
    Set objTemp = Documents(ObjectName)
    fExists = True
    Case "Table"
    Set objTemp = ActiveDocument.Tables(ObjectIndex)
    fExists = True
    End Select

    Set objTemp = Nothing
    Exit Function
    Error_DoesNotExist:
    Err.Clear
    fExists = False

    End Function</pre>

    You're also right that creating an object variable for a specific type won't bring any benefit. Passing the object type automatically was just a half-digested thought that there might be an easier way to pass the argument, but I don't think there is. (I come up with lots of ideas, it doesn't mean all of them are good! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    Gary

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if document is open (Word97/SR2)

    I realised last night that the problem isn't one of technology; it's life. Consider:

    You open a document, there it is, "ActiveDocument" responds to its .Name and it's .FullName.

    Then a user macro saves the document under a different name, perhaps adding a DOC extent, perhaps changing the Name portion.

    Question: Is the document still open on the computer?



    Some would say yes, changing the name of the document doesn't alter the fact that it is still there; others would say "No, it's no longer there".

    As long as I use the Documents collection to determine the presence of a document, a change of name will mean the document no longer exists.

    Unless I have access to some basic characteristic (such as what?) I would need to define exactly what I mean by "This Document" in orrder to know if it still exists.



    For those still in doubt, consider this real-life argument:

    There is only one of me (collective sigh of relief!). If you ask/search the Internet you will find ChrisGreaves, CPRGreaves, CGreaves ChrisGreaves etc. All me.

    But if you ask for Bloordale Chris, you'll draw a blank, yet that's still me, and I'm known by that name locally. Also "The Mad Aussie", also "The YWS guy" and so on. No doubt about it, it's still me, all of it, but if you use the "wrong" name, people will say "He doesn't exists; never heard of him".



    Back to my main problem: If I open a file on the computer and hand control over to a user macro, as far as I can tell, I have no way of knowing if (what the user and I consider to be) the same document, is still there when the user relinquishes control.

    An easy way to test this would be to write a user macro that saves a document with a file name generated at random. Since we wrote the macro, we know the document is still there, but how would we prove it? How would we write an algorithm to determine that that really is the same document, for our purposes?

  15. #15
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: test if document is open (Word97/SR2)

    How about setting the time in a Docvariable in the document just before you hand control over to the user and removing it again when you get control back - that way you could iterate over all the currently open documents to see if any of them has your Docvariable with the same timestamp.

    StuartR

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •