# Opens Underwriting files, extracts necessary data, closes files
#
[cmdletbinding()]
$FilePath = "C:\Users\JimD\Underwriting"
#Specify the Sheet name
$SheetName1 = "Property Summary"
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
$UWFiles = Get-ChildItem -Filter "Underwriting (*).xlsm" -path $FilePath
foreach ($UW in $UWFiles)
{
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($UW.fullname)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName1)
[pscustomobject][ordered]@{
PropertyName = $WorkSheet.Range("Name").Text
Market = $WorkSheet.Range("D6").Text
Units = $WorkSheet.Range("Unit_Count").Text
Price = $WorkSheet.Range("Purchase_Price").Text
OCC_Rate = $WorkSheet.Range("D9").Text
Year_Built = $WorkSheet.Range("D8").Text
}
$workBook.Save()
$workbook.Close()
}
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)){'released one count'}
Remove-Variable objExcel
# Opens Underwriting files, extracts necessary data, closes files
#
[cmdletbinding()]
$FilePath = "C:\Users\JimD\Underwriting"
$details = ""
$details2 = ""
#Specify the Sheet name
$SheetName1 = "Property Summary"
$SheetName2 = "New Financing"
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
#$UWFiles = Get-ChildItem -Filter "Underwriting (*).xlsm" -Recurse -path $FilePath
$UWFiles = Get-ChildItem -Filter "Underwriting (*).xlsm" -path $FilePath
#write-output $FilePath
foreach ($UW in $UWFiles)
{
write-output $UW.fullname
#Check for worksheet named Property Summary
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($UW.fullname)
$WorkSheets = $WorkBook.Worksheets |where {$_.name -eq "Property Summary"}
if ($worksheets){
$WorkSheet = $WorkBook.sheets.item($SheetName1)
$details = [pscustomobject][ordered]@{
PropertyName = $WorkSheet.Range("Name").Text
Market = $WorkSheet.Range("D6").Text
Units = $WorkSheet.Range("Unit_Count").Text
Price = $WorkSheet.Range("Purchase_Price").Text
OCC_Rate = $WorkSheet.Range("D9").Text
Year_Built = $WorkSheet.Range("D8").Text}
$details | Export-Csv -path "c:\temp\test.csv.txt" -Append
}
Else {
$SheetName2 = "New Financing"
$WorkSheet = $WorkBook.sheets.item($SheetName2)
$details = [pscustomobject][ordered]@{
PropertyName = $WorkSheet.Range("C8").Text
Market = $WorkSheet.Range("C8").Text
Units = $WorkSheet.Range("D9").Text
Price = $WorkSheet.Range("D10").Text
OCC_Rate = $WorkSheet.Range("I20").Text
Year_Built = $WorkSheet.Range("C8").Text}
$details | Export-Csv -path "c:\temp\test.csv.txt" -Append
}
$workBook.Save()
$workbook.Close()
}
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)){'released one count'}
#$objExcel.Quit()
Remove-Variable objExcel
Remove-Variable SheetName1
Remove-Variable SheetName2
Remove-Variable Details
Stop-Process -processname EXCEL
Open in new window
This gets files matching the pattern, then groups them based on whatever "(property)" the file has.