Monday, April 11, 2011

Function to verify whether any excel instance is open or not. If opened terminate the same.

Function IsAnyExcelInstancesOpen 'As Boolean

    IsAnyExcelInstancesOpen = True

    Dim objWMIService, objProcess, ProcessCollection
    Dim strComputer, strMessage
    Dim bolTerminate
    Dim InputUserSelection
    ' Make it true if you want to terminate process without any user;s permission.
    bolTerminate = False
   
    strComputer = "."
   
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   
    Set ProcessCollection = objWMIService.ExecQuery("Select * from Win32_Process")
   
    For Each objProcess in ProcessCollection
    If StrComp(objProcess.Name, "Excel.exe", 1) = 0 Then
        ' If true it will terminate process without any message or intimation
        If boolTerminate Then
            objProcess.Terminate()
        ' It will ask user(Permission) input "Yes" to terminate excel instance opened.     
        Else
           
            strMessage = "An Instance of Excel is running on the Machine. The Framework needs all the Excel instances to be closed for running the Tests." & vbCr & vbCr &_
                "Do you want the Framework to CLOSE all the instances of the Excel?" & vbCr & vbCr &_
                " - Yes:" & vbCr & "Click on Yes to close all the instances of Excel and proceed with the Test Execution." & vbCr & vbCr &_
                " - No :" & vbCr & "Click on No to Stop the test execution and close the Excel files manualy." & vbCr & "--" & vbCr &_
                "Note: All unsaved data in the open excel files will be lost on clicking Yes."
               
            oUserSelection = MsgBox(strMessage, vbYesNo + vbExclamation , "Excel Instances Open")
               
            If oUserSelection = vbYes Then
               
                boolTerminate = True
                objProcess.Terminate()
               
            Else
           
                Exit Function
               
            End If
           
        End If
       
    End If
   
    Next

    IsAnyExcelInstancesOpen = False
   
End Function

To get list of specific file from specific folder

'Funtion to get list of the specific type of files(Based on extension) in specified folder. In this example will filter 'out file having "txt", "qfl" & "vbs" extension. To get the list of file you have keept Array global for atleast that 'file (depend on implementation).
Public Function GetFileList (folderLocation, RequiredfileNames()) 'As Boolean
   
    'Initialize the function and declare the variables
    GetFileList = False
    Dim fso, sfileName, files, fileCount, folder, selectedFile
    fileCount = 0
     On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")     
    Set folder = fso.GetFolder(folderLocation) 
    Set files = folder.Files
   
    'Browse through all the files in the selected folder
    For each selectedFile In files
           sfileName = selectedFile.Name
          
           'Filter the file based on the extension of the file
        If (StrComp(Right(sfileName,3), "txt", 1) Or StrComp(Right(sfileName,3), "qfl", 1) Or StrComp(Right(sfileName,3), "vbs", 1)) Then
            ReDim Preserve RequiredfileNames(fileCount)
            RequiredfileNames(fileCount) = sfileName
            fileCount = fileCount + 1                                      
        End If
    Next 
   
    'Finalize the function and return the value
    If(fileCount > 0) Then
        GetFileList = True
    End If
   
End Function
 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons