Alhamdulillah, sudah berhasil Mr. Kid.
Memang Mr. Kid seorang Guru sejati. Tidak bosan-bosan menjawab dengan 
penjelasan yang panjang sehingga bisa dimengerti penanya.

Saya hanya bisa berdoa, semoga amal kebaikan Mr. Kid diterima Allah SWT dengan 
pahala yang berlipat ganda dalam mencerdaskan bangsa Indonesia. Amiin Yaa 
Robbal ‘Alamin…

Syukron katsiiir…

From: [email protected] [mailto:[email protected]]
Sent: Thursday, March 30, 2017 5:08 PM
To: BeExcel
Subject: Re: [belajar-excel] Mengetahui apakah file tertentu sudah dibuka HANYA 
di computer kita sendiri


Hai Tio,
1. untuk cek sebuah file ada atau tidak bisa dengan begini :
dim sPath as string, sFile as string, sFileName as string
spath="\\komputer-x\folder\subfolder\dst"    'atau "d:\folder dataku\sub 
foldernya"
sfile="file excel nya.xlsx"
sfilename=spath & "\" & sfile
if len(sfilename)=0 then     'nama file belum didefinisikan (masih teks kosong)
           msgbox "Nama file masih kosong"
elseif len(dir(sfilename))=0 then     'ada teks nama file, tetapi wujud filenya 
tidak ada
           msgbox "File [" & sfilename & "] tidak ditemukan"
endif

2. untuk cek sebuah workbook sedang terbuka atau tidak
dim iFile as integer
dim sPath as string, sFile as string, sFileName as string
spath="\\komputer-x\folder\subfolder\dst"
sfile="file excel nya.xlsx"
sfilename=spath & "\" & sfile
on error resume next
ifile=freefile
open sfilename for input lock read as #ifile
close ifile
if err.number=0 then       'file sedang tidak dibuka oleh siapapun
      msgbox "Tidak ada user yang sedang membuka file [" & sfilename & "]"
else
      msgbox "File sedang dibuka entah dimana dan entah oleh siapa"
endif
err.clear
on error goto 0

3. untuk memeriksa apakah Excel yang dipakai si VBA sedang membuka workbook
dim wbk as workbook
dim sPath as string, sFile as string, sFileName as string
spath="\\komputer-x\folder\subfolder\dst"
sfile="file excel nya.xlsx"
sfilename=spath & "\" & sfile
on error resume next
set wbk=workbooks(sfile)
if not wbk is nothing then     'wbk ada isinya, berarti sedang terbuka di 
aplikasi excel yang dipakai si VBA
       msgbox "Anda sedang membuka file [" & sfilename & "] di aplikasi Excel 
ini"
else
       msgbox "Pada aplikasi Excel ini tidak ditemukan workbook [" & sfilename 
& "]"
endif

4. untuk memeriksa apakah workbook terbuka di instansi aplikasi Excel selain 
yang dipakai si VBA di komputer yang sama
dim wbk as workbook, app as excel.application
dim sPath as string, sFile as string, sFileName as string
spath="\\komputer-x\folder\subfolder\dst"
sfile="file excel nya.xlsx"
sfilename=spath & "\" & sfile
on error resume next
set app=getobject(sfilename).application
if app.hwnd<>thisworkbook.parent.hwnd then    'handle aplikasi Excel yang 
membuka file ternyata beda dengan aplikasi excel yang dipakai si VBA
        msgbox "File [" & sfilename & "] sedang dibuka di aplikasi Excel lain 
pada komputer ini"
else
       app.workbooks(sfile).close false
endif

5. memisahkan nama lengkap file menjadi 2 bagian, yaitu bagian path dan bagian 
file
dim sFileParts() as string
dim sPath as string, sFile as string, sFileName as string
sfilename="\\komputer-x\folder\subfolder\dst\file excel nya.xlsx"     'atau 
"d:\folder data ku\sub foldernya\file excel nya.xlsx"
sfileparts=split(sfilename,"\")
sfile=sfileparts(ubound(sfileparts))
spath=replace(sfilename,"\" & sfile,vbnullstring)

*** urutan proses :
Nomor 1 -> Nomor 2 bagian else diisi nomor3 dan nomor 3 bagian else diisi nomor 
4 lalu nomor 4 bagian else ditambahkan pesan bahwa file dibuka oleh user lain 
entah dimana (dikomputer yang sama ataupun tidak).

*** jika inputannya adalah nama file lengkap, maka baris-baris kode :
dim sPath as string, sFile as string, sFileName as string
spath="\\komputer-x\folder\subfolder\dst"    'atau "d:\folder dataku\sub 
foldernya"
sfile="file excel nya.xlsx"
sfilename=spath & "\" & sfile
    diganti dengan nomor 5
Regards,
Kid


On Thu, Mar 30, 2017 at 11:02 AM, Tio Adi Bayu Adjie 
[email protected]<mailto:[email protected]> [belajar-excel] 
<[email protected]<mailto:[email protected]>> wrote:

Salam BeExceller,
Saya sudah menemukan coding vb untuk mengetahui apakah file yang saya pilih 
sudah dibuka atau tidak. Tapi code vba tsb dimaksudkan file terbuka di computer 
kita dan terbuka juga di network oleh orang lain.
Bisakah kita memodif code vba untuk mengetahui apakah file yang dimaksud sudah 
terbuka HANYA di computer kita sendiri (bukan terbuka di network oleh user 
lain) ?

Dibawah adalah code vba yang mengetahui apakah file dimaksud sudah 
terbuka/tidak oleh kita sendiri dan user lain di network.

Sub TestFileOpened()

    ' Test to see if the file is open.
    If IsFileOpen("\\ssbfs01\ssbstore\Corporate Division\Estimating 
Department-Services\2. Estimator\8. Tio\Nozzle-Type-Information.xlsx") Then
        ' Display a message stating the file in use.
        MsgBox "File already in use!"
        '
        ' Add code here to handle case where file is open by another
        ' user.
        '
    Else
        ' Display a message stating the file is not in use.
        MsgBox "File not in use!"
        ' Open the file in Microsoft Excel.
'        Workbooks.Open "c:\Book2.xls"
        '
        ' Add code here to handle case where file is NOT open by another
        ' user.
        '
    End If

End Sub

' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function


Thanks,
Tio
__,_._,_


Kirim email ke