Author Topic: Checking if there is a drawing (file) opened or not  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

iliekater

  • Guest
Checking if there is a drawing (file) opened or not
« on: December 08, 2007, 06:52:17 AM »
Is it possible to find with VBA code if AutoCAD has a file opened or wheither there is no file opened at all ?

Humbertogo

  • Guest
Re: Checking if there is a drawing (file) opened or not
« Reply #1 on: December 09, 2007, 08:35:33 AM »
Code: [Select]

'Sample Code: Microsoft Visual Basic version of IsFileAlreadyOpen
'----------------------------------------------------------------


      ' Declaration for APIs used by our function...
      Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal
      lpPathName As String, ByVal iReadWrite As Long) As Long
      Private Declare Function GetLastError Lib "kernel32" () As Long
      Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal
      hFile As Long) As Long



      Function IsFileAlreadyOpen(Filename As String) As Boolean
         Dim hFile As Long
         Dim lastErr As Long


         ' Initialize file handle and error variable.
         hFile = -1
         lastErr = 0


         ' Open for for read and exclusive sharing.
         hFile = lopen(Filename, &H10)


         ' If we couldn't open the file, get the last error.
         If hFile = -1 Then
            lastErr = Err.LastDllError
         ' Make sure we close the file on success.
         Else
            lclose (hFile)
         End If


         ' Check for sharing violation error.
         If (hFile = -1) And (lastErr = 32) Then
            IsFileAlreadyOpen = True
         Else
            IsFileAlreadyOpen = False
         End If


      End Function


iliekater

  • Guest
Re: Checking if there is a drawing (file) opened or not
« Reply #2 on: December 12, 2007, 01:56:11 AM »
OK , I'll give it a try !

Ricky_76

  • Guest
Re: Checking if there is a drawing (file) opened or not
« Reply #3 on: December 13, 2007, 01:00:50 PM »
HI,

no way to try it now to give you the perfect code
but i think it's something like this:

msgbox application.documents.count

Hope it works!

/r

ps
I hope I've understand yuor question...

Humbertogo

  • Guest
Re: Checking if there is a drawing (file) opened or not
« Reply #4 on: December 14, 2007, 03:20:34 AM »
check this

Code: [Select]

      ' Declaration for APIs used by our function...
      Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
      Private Declare Function GetLastError Lib "kernel32" () As Long
      Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long



Sub test()
IsFileAlreadyOpen ("P:\Autocad\Library\Test.dwg")
End Sub

      Function IsFileAlreadyOpen(Filename As String) As Boolean
         Dim hFile As Long
         Dim lastErr As Long


         ' Initialize file handle and error variable.
         hFile = -1
         lastErr = 0


         ' Open for for read and exclusive sharing.
         hFile = lopen(Filename, &H10)


         ' If we couldn't open the file, get the last error.
         If hFile = -1 Then
            lastErr = Err.LastDllError
         ' Make sure we close the file on success.
         Else
            lclose (hFile)
         End If


         ' Check for sharing violation error.
         If (hFile = -1) And (lastErr = 32) Then
            IsFileAlreadyOpen = True
            MsgBox Filename & " Is Already Open"
         Else
            IsFileAlreadyOpen = False
            ThisDrawing.Application.Documents.Open Filename
         End If


      End Function



iliekater

  • Guest
Re: Checking if there is a drawing (file) opened or not
« Reply #5 on: March 02, 2008, 05:11:27 AM »
Thanks alot Ricky_76 , the
Code: [Select]
application.documents.countcode was more than enough for me !