Results 1 to 12 of 12
  1. #1
    jsbotts is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Aug 2011
    Location
    Hobbs, NM
    Posts
    14

    Post From Access unable to close Word with VBA

    Using Microsoft Office 2007, and Windows XP. In Access 2007 a Word document is created, text is added, and Word closed with VBA. Then unable to open the document because it is 'locked by user'.



    ***********Code*********************
    Dim wo As Word.Application
    Dim path As String

    Set wo = CreateObject("Word.Application")
    wo.Documents.Add
    wo.ActiveDocument.SaveAs FileName:=path

    'Text added to the document

    wo.ActiveDocument.Save
    wo.ActiveDocument.Close False
    wo.Quit
    Set wo = Nothing
    **********End of Code******************

    The code is successful in creating the document and when looking in the directory, the file is there but with ~owner's file. When attempting to open the file with VBA, the message states the file is locked by the user who created the file.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I tested your code with Access 2007 and works great. No problem opening created file. Sorry, cannot replicate your issue.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    the ~Owner's file is a temporary file. Does the user who created the file have delete permission on the directory where the file is located? If not, that may be why the temp file is still there and Access thinks the file is locked.

    John

  4. #4
    jsbotts is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Aug 2011
    Location
    Hobbs, NM
    Posts
    14
    Good point John G. I do not have any way of knowing, but am having our network guys look at it, and will ask.

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    One thing I forgot to ask - how much time passes between creating the file, and trying to open it? I do the same sort of thing, and I have seen that it takes a few seconds for the MS Word process to actually shut down, and until it does that file is "locked". If you are trying to open the file immediately after creating it, that may well be the source of the problem.

    Just as an aside, if the code to create the file fails, the MS Word process will not terminate; you have to go into Windows Task Manager and terminate it manually. You can avoid that problem by putting an error hander into your code, to close the file and stop MS Word in the event of an error.

  6. #6
    jsbotts is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Aug 2011
    Location
    Hobbs, NM
    Posts
    14
    John G:
    The file is opened within 30 to 60 seconds after it is created. The code to create the file does not fail, as the file is created and completed. If the problem is a delay in closing WORD, is there anyway to delay the opening by the user?

  7. #7
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Yes, that can be done - but if your delay is 30 - 60 seconds, then that should be plenty of time for Word to close. One thing you can do to help solve this problem is: as soon as you get the file locked error, start Windows Task manager (Ctrl-Alt-Del, and click the task manager Button), then see if there is a Winword.exe process running (but be sure not to have any other MS Word documents open). If there is, that very likely is the problem, and you have to find out why it is not closing properly.

    Two questions:
    1) If the MS Word document is not going to be updated, did you try opening it as read-only?
    2) Does it have to be MS word, or would a plain text file do?

    John

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Interesting. I tested your code to create the Word document. It does not have problem with closing the file, the locking file is removed. I had the file created at C:/ root level. Perhaps you should try that just to verify if the issue is with the destination folder.

    I did another test by commenting out the .Close and .Quit lines. This causes the locking file to remain and the WINWORD.exe process is still running in TaskManager. I stopped the process but the locking file still there. I manually opened the file and get the DocumentRecovery advisory. I closed the file and the locking file remained but I was able to delete it. Next, with those lines still commented, I added line wo.Visible = True. This works also. This is all with Access and Word 2007.

    What is your code to open the file?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    jsbotts is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Aug 2011
    Location
    Hobbs, NM
    Posts
    14
    June 7:

    wo.Documents.Open(path).Activate

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Well that code does open a Word process and the locking file exists but app is not visible.

    Instead of wo.Quit use wo.Visible = True
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    jsbotts is offline Novice
    Windows 7 64bit Access 2000
    Join Date
    Aug 2011
    Location
    Hobbs, NM
    Posts
    14
    I am not sure. wo.Quit should stop the Word application from processing, right? Wouldn't wo.Visible = true show the document as open when trying to close the file?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Yes, the wo.Quit works for me but apparently is an issue for you. And correction to my suggestion: instead of both .Close and .Quit (I commented both of those lines), use .Visible.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access to Word - Multiple Word Templates?
    By alpinegroove in forum Programming
    Replies: 11
    Last Post: 06-12-2012, 04:42 PM
  2. Unable to Read from COM ports in Access 2003
    By userThree in forum Programming
    Replies: 3
    Last Post: 07-31-2011, 08:59 PM
  3. Access 97: Unable to find project explorer?
    By captgnvr in forum Programming
    Replies: 0
    Last Post: 07-19-2010, 09:20 AM
  4. Unable to close Query without saving
    By jhrBanker in forum Access
    Replies: 0
    Last Post: 06-08-2009, 05:09 PM
  5. Replies: 1
    Last Post: 12-21-2005, 12:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums