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!

Why is my file locking?

Status
Not open for further replies.

RhinoCan

Programmer
Oct 30, 2011
4
I am having an intermittent problem with a couple of my VBScripts. Sometimes, when I execute them, a dialog window pops up that tells me the following:
==========
File In Use
foo.doc is locked for editing by [me].
Do you want to:
- Open a Read Only copy
- Create a local copy and merge your changes later
- Receive notification when the original copy is available
[Ok] [Cancel]
===========

I don't understand why I am getting this though. I don't consciously access that file except when I run the VBScripts.
I am not very fluent with VBScript so I may be doing something foolish with it; perhaps I need to specify an option to release the file as I finish with it or something like that? Then again, this problem IS intermittent so that's probably not it, otherwise I'd get it all the time.

I'd like to know what I need to do to avoid this issue in the future. For what it's worth, I have resolved the problem by rebooting the computer but I certainly don't want to do that routinely; I'd rather prevent the problem in the first place.

Here are the VBScripts in question:
===========
[CreateWordMacros.vbs]
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open resume.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\foo.doc")

' Temporary - Determine value of document path. (c:\Documents and Settings\Sparky\My Documents)
' MsgBox "The document originates at this directory: " & strMyDocPath & "."

'Run macro named createResumeFromFile, which has no arguments, and catch its return code
retcde=oWd.Run("createResumeFromFile")

'Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing

'Exit the script with the value of the return code from the macro/function. That will be zero from a successful execution or 1 otherwise.
wscript.quit(retcde)

===========
===========
[ExportWordMacros.vbs]

' Get arguments into variables
If WScript.Arguments.Count < 1 Then
MsgBox "Too few arguments; expecting one."
WScript.Quit
Elseif WScript.Arguments.Count > 1 then
MsgBox "Too many arguments; expecting one."
WScript.Quit
Else
strArg1 = WScript.Arguments.Item(0)
End If

' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path

' Start Word Application, open resume.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\foo.doc")

'Run macro named exportMacros, which has one argument
retcde = oWd.Run("exportMacros",strArg1)

'Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing

Set objShell = Nothing

WScript.quit(retcde)
===========
 
In what order to your run these files? Chances are the 2nd .vbs is trying to open foo.doc before the Word in the first script has a chance to close it. Set the oDoc to nothing and put a delay after your close the file in word - give the computer time to release the file hold.
Code:
'Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
[red]wscript.sleep 500 'millisecs[/red]
oWd.Quit
[red]set oDoc = Nothing[/red]
set oWd = Nothing

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I do run the two scripts back to back in the order shown so I can see the benefit of having a brief pause between the two, as you suggest. However, I don't think that is the real problem.

You see, BOTH scripts encounter that error (sometimes). Something is locking the file and it isn't just a matter of waiting a few seconds. It is sometimes hours since I last ran either script yet I can still get the locking message on BOTH scripts.

Any ideas on what might be going on and why it is intermittent?
 
can you copy the file and name it something else, then set the script to read the copy. It's a work-around to original problem, but a solution while you investigate the hold.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Sure, I could do that. But I don't know how to investigate the hold. I don't know where to look. I don't know anything about the file locking that VBScript and/or Windows are doing.

Can you tell me how to investigate the hold?

What locks a Windows file? When a file is locked, how can I determine what process is locking it? If the lock is being held excessively long, as I suspect, How can I release that lock?

I used to work with row and page locking in the DB2 relational database and understand how that works but DB2 has ways to terminate locks. I have no idea what, if any, comparable facilities are held by Windows/VBScript.
 
You have to understand that your issue is a Word lock.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay, it's a Word lock. But why does Word seem to be holding it? What ends a lock in Word? What can I do to force a lock to be released? Are there any utilities that can help me determine why the lock is there and to release it?

 
If the hold is coming from Word (as this discussion would suggest) then you'll need to find the process that is running Word and foo.doc. Maybe it's a scheduled task or maybe someone is accessing it. If you are unable to find it, a drastic measure would be to kill all Word processes at the beginning of your script and see what breaks or who complains.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top