Advertisement
Guest User

Untitled

a guest
Nov 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # Create an Excel instance and hide it.
  3. Write-Host "Starting Excel..."
  4. $xl = New-Object -Comobject Excel.Application
  5. $xl.Visible = $false
  6.  
  7. # Do the same for MS Word.
  8. #$wd = New-Object -Comobject Word.Application
  9. #$wd.Visible = $false
  10.  
  11. # Load all Excel files into an array.
  12. Write-Host "Finding Files... " -NoNewline
  13. $inputFiles = Get-ChildItem "D:\Foundry\In" -Filter *.xls*
  14. $count = $inputFiles.Count
  15. Write-Host "$count files found."
  16. Write-Host ""
  17. Write-Host "Processing Files..."
  18. Write-Host ""
  19. Write-Host "Status  Filename"
  20. Write-Host "------  --------"
  21.  
  22.  
  23.  
  24.  
  25. # Enumerate through each item.
  26. # $inFile is used to reference each item in the table
  27. Foreach ($inFile in $inputFiles)
  28. {
  29.     # Get the file path
  30.     $inFilePath = $inFile.FullName
  31.     $inFileName = $inFile.Name
  32.     Write-Host "[ ok ]  $inFileName"
  33.  
  34.     # Open the file and open the first worksheet.
  35.     $wb = $xl.workbooks.open($inFilePath)
  36.     $ws = $wb.worksheets.item(1)
  37.  
  38.     # We'll get some basic info first.
  39.     # Each sheet has multiple tables of varying length,
  40.     # But there is still some information at predictable cell locations.
  41.     # We use the "Range" to identify single cells mainly for consistency and convenience
  42.     # There are other ways to do it, though.
  43.  
  44.     $ClientFullName = $ws.Range("B4").Text
  45.     $VRCFullName = $ws.Range("D7").Text
  46.  
  47.     # Now, let me show you how it's done ;)
  48.     # We search for the headers of each table.
  49.     # Once we find one, we note it as the first.
  50.     # THen, we continue finding it over and over again
  51.     # We do this until we find the first one again, or find nothing at all.
  52.     # This gives us all cells in the searched range with the text we searched for.
  53.     $SearchTerm = "Question Sequence"
  54.     $Range = $ws.UsedRange()
  55.     $Match = $Range.Find($SearchTerm)
  56.     $First = $Match
  57.     Write-Host "        Reports at: " -NoNewline
  58.     Do
  59.     {
  60.         $MatchCell = $Match.AddressLocal($false, $false)
  61.         Write-Host "$MatchCell " -NoNewline
  62.         $Match = $Range.FindNext($Match)
  63.     } while ($Match -ne $NULL -and $Match.AddressLocal() -ne $First.AddressLocal())
  64.     Write-Host ""
  65.     Write-Host ""
  66. }
  67.  
  68. # Quit Excel, then instruct the system to release the COM Object.
  69. # Doing this is necessary, otherwise the process will never exit, even when we call the Quit() method.
  70. $xl.Quit()
  71. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
  72. Remove-Variable xl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement