Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Checking whether a file is open in Excel
Message
From
20/03/2006 00:50:58
 
 
To
19/03/2006 22:58:28
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01105716
Message ID:
01105723
Views:
28
>I often require to know whether a particular excel file is open, so that an error message is generated before code is executed. Could someone tell me the code to know whether a particular excel file is open?

Steven,

FileOpened(...) will tell

It tries to open a file exclusively for read access. If it fails, then something like excel, word has the file open
* file
#define FILE_SHARE_READ                 0x00000001  
#define FILE_SHARE_WRITE                0x00000002  
#define FILE_SHARE_DELETE               0x00000004  

#define	FILE_ATTRIBUTE_READONLY				0x00000001
#define	FILE_ATTRIBUTE_HIDDEN				0x00000002
#define	FILE_ATTRIBUTE_SYSTEM				0x00000004
#define	FILE_ATTRIBUTE_DIRECTORY			0x00000010
#define	FILE_ATTRIBUTE_ARCHIVE				0x00000020
#define	FILE_ATTRIBUTE_ENCRYPTED			0x00000040
#define	FILE_ATTRIBUTE_NORMAL				0x00000080
#define	FILE_ATTRIBUTE_TEMPORARY			0x00000100
#define	FILE_ATTRIBUTE_SPARSE_FILE			0x00000200
#define	FILE_ATTRIBUTE_REPARSE_POINT		0x00000400
#define	FILE_ATTRIBUTE_COMPRESSED			0x00000800
#define	FILE_ATTRIBUTE_OFFLINE				0x00001000
#define	FILE_ATTRIBUTE_NOT_CONTENT_INDEXED	0x00002000

#define	GENERIC_READ	(0x80000000)
#define	GENERIC_WRITE	(0x40000000)
#define	GENERIC_EXECUTE	(0x20000000)
#define	GENERIC_ALL		(0x10000000)

#define	CREATE_NEW			1
#define	CREATE_ALWAYS		2
#define	OPEN_EXISTING		3
#define	OPEN_ALWAYS			4
#define	TRUNCATE_EXISTING	5

#define INVALID_HANDLE_VALUE		(-1)
#define INVALID_FILE_SIZE			(-1)
#define INVALID_SET_FILE_POINTER	(-1)
#define INVALID_FILE_ATTRIBUTES		(-1)

#Define MAX_PATH 260

#IFNDEF	TRUE
	#define	TRUE	.T.
	#define	FALSE	.F.

	#define	MAX_INTEGER	(0x7fffffff)
#ENDIF

#ifndef VOID
	#define	UINT		long
	#define	DWORD		Long
	#define	WORD		Short
	#define Size_T		DWORD
	#define BOOL		long
	#define POINTER		Long
	#define LPVOID		POINTER
	#define LPCVOID		POINTER
	#define VOID
	#define	CHAR		String
	#define WCHAR		string
	#define	UCHAR		integer
	#define T_HANDLE	Integer
	#define	T_HWND		long
	#define	HINSTANCE	Long
	#define	HRESULT 	Long
	#define	HBITMAP		Long
	#define	HFONT		Long
	#define	HMENU		Long
	#define HICON		POINTER
	#define	PICONINFO	String @
	#define	HGDIOBJ		POINTER
	#define	UINT_PTR	POINTER
	#define	DWORD_PTR	POINTER
	#define COLORREF	long
	#define ULONG		long
	#define T_STATUS	long
	
	#define	HKEY	Long
	#define LPCTSTR	String @
	#define LPCSTR	string @
	#define LPWSTR 	String @
	#define LPTSTR	String @
	#define	REGSAM	integer
	#define	PHKEY	HKEY @
	#define	LPDWORD	DWORD @
	#define LPBYTE	String @
	#define	PFILETIME string @
	#define	LPBOOL	BOOL @
	#define LPRECT	string @
	#define LPHANDLE T_HANDLE @
	
	#define	LPSECURITY_ATTRIBUTES	string @
	#define LPWIN32_FIND_DATA	String @
	
	#define LPSYSTEMTIME string@
	#define LPTIME_ZONE_INFORMATION string @

	#define LPWIN32_FIND_DATA	String @

	#define	HCRYPTPROV	Long
	
#endif

*--------------------------------------------------------------------------
declare T_HANDLE CreateFile in Kernel32.dll ;
		LPCTSTR FileName, ;
		DWORD dwDesiredAccess, ;
		DWORD dwShareMode, ;
		LPSECURITY_ATTRIBUTES lpSecurityAttributes, ;
		DWORD dwCreationDisposition, ;
		DWORD dwFlagsAndAttributes, ;
		T_HANDLE hTemplateFile
		
declare Integer GetFileAttributes in win32api string @
declare integer CloseHandle in win32api long Handle
*--------------------------------------------------------------------------
function FileOpened( FileName )

	local Handle
	
	Handle = CreateFile( ;
						@m.FileName, ;
						GENERIC_READ, ;
						0, ;				&& excl access
						0, ;
						OPEN_EXISTING, ;
						FILE_ATTRIBUTE_NORMAL, ;
						0 ;
				)
	
	do case
	case (m.Handle = INVALID_HANDLE_VALUE )
		return FileExists(m.FileName)
		
	otherwise
		=CloseHandle(m.Handle)
		return FALSE
	
	endcase
	
	assert FALSE
endfunc
*--------------------------------------------------------------------------
Function	FileExists(FileName)
	return (GetFileAttributes(@m.FileName) <> INVALID_FILE_ATTRIBUTES)
endfunc
*--------------------------------------------------------------------------
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform