Thank you for the n00b tips. I clearly just joined and posted my question without reading the guidelines. I appreciate the direction.
I’ve updated my code:
#####################################################################################
### This function opens a Windows Dialog Box for the selection of the source file ###
#####################################################################################
Write-Output "################################################"
Write-Output " "
Write-Output "Select the source file."
Write-Output "The starting directory is C:\."
Write-Output " "
Write-Output "################################################"
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "docx (*.docx)| *.docx"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "C:\"
$Word = New-Object -Com Word.Application
$Document = $Word.Documents.Open($inputfile)
#######################
### End of function ###
#############################################################################
### This function lists a menu for the user to choose the number of tables###
### in the word document that need to be imported ###########################
#############################################################################
# Display the menu and get the user's choice
$choice = Read-Host "Enter the number of tables in the word document that need to be imported"
#######################
### End of function ###
$TablesToExport = $Document.Tables | Select-Object -First $choice
$TablesToExport | Export-csv Medwell.csv
$Document.Close()
$Word.Quit()
# Stop Winword Process
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Document)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Yes the CSV output file looks bizzare:
#TYPE System.__ComObject#{00020951-0000-0000-c000-000000000046}
"Range","Application","Creator","Parent","Columns","Rows","Borders","Shading","Uniform","AutoFormatType","Tables","NestingLevel","AllowPageBreaks","AllowAutoFit","PreferredWidth","PreferredWidthType","TopPadding","BottomPadding","LeftPadding","RightPadding","Spacing","TableDirection","ID","Style","ApplyStyleHeadingRows","ApplyStyleLastRow","ApplyStyleFirstColumn","ApplyStyleLastColumn","ApplyStyleRowBands","ApplyStyleColumnBands","Title","Descr"
"System.__ComObject","Microsoft.Office.Interop.Word.ApplicationClass","1297307460","Microsoft.Office.Interop.Word.DocumentClass","System.__ComObject","System.__ComObject","System.__ComObject","System.__ComObject","True","1","System.__ComObject","1","True","True","9999999","2","0","0","5.4","5.4","0","1",,"System.__ComObject","True","False","True","False","True","False",,
"System.__ComObject","Microsoft.Office.Interop.Word.ApplicationClass","1297307460","Microsoft.Office.Interop.Word.DocumentClass","System.__ComObject","System.__ComObject","System.__ComObject","System.__ComObject","True","1","System.__ComObject","1","True","True","9999999","2","0","0","5.4","5.4","0","1",,"System.__ComObject","True","False","True","False","True","False",,
"System.__ComObject","Microsoft.Office.Interop.Word.ApplicationClass","1297307460","Microsoft.Office.Interop.Word.DocumentClass","System.__ComObject","System.__ComObject","System.__ComObject","System.__ComObject","True","1","System.__ComObject","1","True","True","9999999","2","0","0","5.4","5.4","0","1",,"System.__ComObject","True","False","True","False","True","False",,