2012-09-21 3 views
1

vbscript 사용 Excel 객체를 만들 수있게하고 사용자가 파일을 저장 한 후 파일에서 데이터를 확인할 수있게하려고합니다. WaitForChangedResult를 사용하여 파일이있는 디렉토리를 확인하고 진행하기 전에 변경을 기다렸지 만 저장하지 않고 파일을 닫을 때만 이동합니다. 그 코드는 다음과 같습니다.파일이 저장 될 때까지 실행 일시 중지

Dim xl As Object 
xl = CreateObject("excel.application") 
xl.FileDialog(1).AllowMultiSelect = False 
xl.FileDialog(1).Title = "Navigate to 60-40 loan calculator" 
Dim strFilePathAndName As String 
If xl.FileDialog(1).Show() = -1 Then 
    strFilePathAndName = xl.FileDialog(3).SelectedItems(1) 
Else 
    Exit Sub 
End If 
xl.Visible = True 
xl.Workbooks.Open(strFilePathAndName) 
Dim strXLTab As String 
strXLTab = xl.ActiveSheet.Name 


Dim result As System.IO.WaitForChangedResult 
Dim directory As String 
directory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
Dim watcher As New System.IO.FileSystemWatcher(directory, "Calculator.xls") 
result = watcher.WaitForChanged(System.IO.WatcherChangeTypes.Changed) 
TextBox1.Text = directory 

더 좋은 방법이 있습니까?

답변

0

다음 코드를 사용하여 파일의 변경 사항을 모니터링 할 수 있습니다.

코드를 Wscript.echo 명령 대신 사용하여 파일을 변경/생성/삭제할 때 필요한 조치를 취하십시오. Wscript가

' Monitors a file for modifications. 
Dim intTimer:  intTimer = "2" 
Dim strDrive:  strDrive = "c:" 
Dim strPath:   strPath = "\\temp\\" 
Dim strFile:   strFile = "log.txt" 
Dim strComputer:  strComputer = "." 
Dim objWMIService: Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Dim strQuery:  strQuery = "Select * From __InstanceOperationEvent" & " Within " & intTimer & " Where Targetinstance Isa 'CIM_DataFile'" & " And TargetInstance.Drive='" & strDrive & "'" & " And TargetInstance.Path='" & strPath & "'" 
Dim colEvents:  Set colEvents = objWMIService. ExecNotificationQuery (strQuery) 
WScript.Echo "Monitoring file changes... Press [Ctrl]-[C] to exit" 
Do 
    Set objEvent = colEvents.NextEvent() 
    Set objTargetInst = objEvent.TargetInstance 
    if right(objTargetInst.Name,len(strFile)) = strFile then 
     Select Case objEvent.Path_.Class 
      Case "__InstanceCreationEvent" 
       WScript.Echo "Created: " & objTargetInst.Name 
      Case "__InstanceDeletionEvent" 
       WScript.Echo "Deleted: " & objTargetInst.Name 
      Case "__InstanceModificationEvent" 
       WScript.Echo "Modified: " & objTargetInst.Name 
     End Select 
    end if 
Loop 

실행이 그것을 볼 수 없습니다 또는 콘솔 창에서 볼 수 있도록 Cscript를으로 실행합니다.

관련 문제