Consulting

Results 1 to 5 of 5

Thread: Finding Multiple Instances of Word

  1. #1
    VBAX Newbie
    Joined
    Sep 2008
    Location
    Bromley, Kent
    Posts
    4
    Location

    Question Finding Multiple Instances of Word

    A system I've taken over (Access 2007 front end, converting back end from Jet to SQL Server) has various processes calling Word - some processes run visibly, others run hidden. Usually they close OK!....

    But sometimes a user will inadvertently try to save a specific document having created it twice - with the first copy still open under a separate instance of Word.

    Therefore I need to be able to loop through all instances of Word (hidden and visible) to check each instance for the existence of a document BEFORE trying to save the "current" one. Then I can close the previous version and proceed.

    But there doesn't seem to be a container for "Applications" - any idea how I can find any instance other than the one that's been open longest? (Yes, I'd love to do everything in ONE instance of Word - but with some 'needing' to be visible and others 'needing' to be hidden, there's no chance of that - the client, as ever, always being right!)

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    Laury... in absence of other respondents, I'll offer up some XL VBA code that may be of some use to you. The following uses the file copy error to determine if the doc is open already. I'm suggesting that you add some code that ensures the user doesn't open 2 of the same docs at the same time to begin with (ie. short circuit the save error). This works for me. HTH. Dave
    [vba]
    Public Function NoFileEr(Flpath As String)
    'Copy to temp file from real file
    Dim fs As Object, temp3 As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    temp3 = Left(Flpath, Len(Flpath) - 4) & "T.doc"
    fs.CopyFile Flpath, temp3
    Set fs = Nothing
    End Function


    Public Function BackToR(Retpath As String)
    'If file not open, copy temp file back to real file & kill temp file
    'If file open, close WORD application, copy temp file back to real file...
    '... & kill temp file
    Dim fs As Object, Objwordapp As Object, d As Variant, temp2 As String
    temp2 = Left(Retpath, Len(Retpath) - 4) & "T.doc"
    Set fs = CreateObject("Scripting.FileSystemObject")
    On Error GoTo Errcode
    fs.CopyFile temp2, Retpath
    Set fs = Nothing
    Kill temp2
    Exit Function
    Errcode:
    On Error GoTo 0
    MsgBox "WORD Application closed"
    Set Objwordapp = GetObject(, "word.application")
    With Objwordapp
    .Application.Quit
    End With
    Set Objwordapp = Nothing
    fs.CopyFile temp2, Retpath
    Set fs = Nothing
    Kill temp2
    End Function

    [/vba]

    To use this, before the user opens the doc, run the following with the real doc path (ie. in this case "D:\test1.doc")
    [vba]
    NoFileEr ("D:\test1.doc")
    BackToR ("D:\test1.doc")
    'user can now open file without having 2 seperate aps
    [/vba]

  3. #3

    Two Instances of Word

    Good Evening.

    Not having the code makes it a little more difficult to truly debug the project, but my advise is to declare two instances of Word.

    Using the GetObject Function, set one instance of Word so that it is assigned to the currently opened version of Word. Use this as the "Visible" version. I would name it something like appWordVisible. By running the GetObject Function first, you ensure you do not get the "Hidden" instance of Word.

    Next, use the CreateObject Function to create a new instance of Word. This would be your "Hidden" instance. Name it appWordHidden.


    In the code, specify which instance of Word is going to open the Document you are working with. By using GetObject for all Documents which would be visible, you are ensuring that single instance always has the Documents in question for saving twice.

    Scott

  4. #4
    VBAX Newbie
    Joined
    Sep 2008
    Location
    Bromley, Kent
    Posts
    4
    Location

    Thumbs up Thanks Dave & Scott

    Two very different approaches - I'll try both and get back to you!

    As an aside, it's good to find a forum where posts get read and answered! I think I'll like this place - hopefully as a contributor as well as a seeker!

    Laury

    (Mine will be a budweiser - but of the original Czech variety !!)

  5. #5
    VBAX Newbie
    Joined
    Sep 2008
    Location
    Bromley, Kent
    Posts
    4
    Location
    Dave - I've a horrible feeling that I'm missing something. I've been trying (and tweaking) your coding, but finally I've reached this conclusion - I can't "kill" an open file, so the previous (unwanted) version is effectively "safe". Also, the 'application.quit' instruction in BackToR(retpath) closes the latest instance - which of course has the 'wanted' version of the document, i.e. its latest incarnation.

    It seems I was a bit vague in my initial question. The problem is that the older version of the same file will already be open and in a separate instance of Word (if it were closed, the simple Save within the existing code would kill the older version anyway.)

    Looks like tomorrow I'll be testing Scott's idea (rats - that means rather more re-coding!!)

Posting Permissions

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