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

How do I determine if Excel file is open by another 1

Status
Not open for further replies.

jmueller

MIS
Oct 9, 2001
57
US
Can somebody please give me the command to determin if an excel file is open by another person?

Thank You Everyone!

Jack.
 
This is not my original code & I cannot take credit for it. Also, I usually comment where I got my code from but did not do that this time so I apologize in advance for not giving credit where credit is due.

Code:
cFileName = [C:\path\filename.ext])
lnNo = FOPEN(lcFilename,0)
IF lnNo = -1 
    MESSAGEBOX([File in use])
ELSE
    MESSAGEBOX([File not in use])
ENDI
FCLOSE(lnNo)



Jim Osieczonek
Delta Business Group, LLC
 
Thanks alot Jim for responding. Is this code suppose to work with an excel file? In other words, I want to check with VFP 8 if somebody has an excel file open. The reason I ask is because I get a -1 everytime I run your code no matter if the xls file is open or not.

Hopefully, i'm just doing something stupid!

Thanks again.

Jack.
 
Jim,

Sorry, but that code won't work.

First, you need to pass 1 as the second parameter to FOPEN(). If you don't, VFP will try to open the file read-only, which will always succeed, even if another user has opened it in Excel.

But even you do pass 1 as the second param, the attempt will still succeed if the Excel user has flagged the file as sharable.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
I think you need to pass 12 as the 2nd parameter, which stands for read/write unbuffered.

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
This was only tested on Word 2002...

Brian

Code:
lcWordFile=[C:\Downloads\MyWordDoc.doc]

lcTempDoc=ADDBS(JUSTPATH(lcWordFile))+[~$]+;
	RIGHT(JUSTSTEM(lcWordFile),LEN(JUSTSTEM(lcWordFile))-2)+;
	[.]+JUSTEXT(lcWordFile)
	
IF ADIR(laTest,lcTempDoc,[H])=1
 lcFile=FILETOSTR(lcTempDoc)
 lcUserName=SUBSTR(lcFile,1,ATC(chr(0),lcFile)-1)
 MESSAGEBOX(lcUserName + [ is using ] + lcWordFile)
ELSE
 IF ADIR(laTest,lcWordFile)=1
  loWord=CREATEOBJECT([Word.Application])
  loWord.Documents.Open(lcWordFile)
  loWord.visible=.t.
 ELSE
  MESSAGEBOX([File Does Not Exist: ]+lcWordFile)
 ENDIF 
ENDIF
 
I just saw this thread on the forum and remembered that FOPEN will return -1 if the file being queried doesn't exist.

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top