Powershell - kontrol af script - Word tabeller til Excel
Hej,Jeg er ved at lave et script der trækker data ud fra alle tabeller i en .docx-fil, der anvender en bestemt typografi i celle (1,1) og sender det til et Excel-ark. Men jeg kan ikke få det til at virke.
Er der nogen, der kan hjælpe mig med at få styr på dette? Jeg har kendt til Powershell i et par dage, så jeg skal nok have et eventuelt svar ind med ske.
På forhånd tak!
Selve scriptet:
[code]
function Invoke([object]$m, [string]$method, $parameters)
{
$m.PSBase.GetType().InvokeMember(
$method, [Reflection.BindingFlags]::InvokeMethod, $null, $m, $parameters,$ciUS)
}
$word = new-object -ComObject "word.application"
#$word.Visible = $true
del "C:\PowerShell Kravextractor\excelfil.xls"
$doc = $word.documents.open("C:\PowerShell Kravextractor\ks_test.docx")
$sel = $word.Selection
$tables = $doc.Tables
# debug $paras.Item(7).Style.NameLocal
$excel = New-Object -comobject Excel.Application
#$excel.Visible = $True
$ciUS = [System.Globalization.CultureInfo]'en-US'
$workbook = Invoke $excel.Workbooks Add
$worksheet = $workbook.Worksheets.Item(1)
#$worksheet.Cells.Item(1,1).FormulaLocal = "A value in cell A1."
$worksheet.Cells.Item(1,1).FormulaLocal = "Krav # (ID)"
$worksheet.Cells.Item(1,2).FormulaLocal = "Titel"
$worksheet.Cells.Item(1,3).FormulaLocal = "Kategori"
$worksheet.Cells.Item(1,4).FormulaLocal = "Type"
$worksheet.Cells.Item(1,5).FormulaLocal = "Beskrivelse"
# debug
# $doc.Paragraphs.Item(9).Style.NameLocal
$cnt = 1
foreach ($table in $tables)
{
$style = $table.Cells.Item(1,1).Style
If ($style.NameLocal.Contains("Krav1 Overskrift"))
{
#echo $cnt
$id = $table.Cells.Item(1,1).Text
$titel = $table.Cells.Item(1,2).Text
$kat = $table.Cells.Item(2,2).Text
$type = $table.Cells.Item(2,4).Text
$besk = $table.Cells.Item(3,2).Text
$cnt += 1
$worksheet.Cells.Item($cnt,1).FormulaLocal = $id
$worksheet.Cells.Item($cnt,2).FormulaLocal = $titel
$worksheet.Cells.Item($cnt,3).FormulaLocal = $kat
$worksheet.Cells.Item($cnt,4).FormulaLocal = $type
$worksheet.Cells.Item($cnt,5).FormulaLocal = $besk
echo $id
}
}
#echo $cnt
#$excel.DisplayAlerts = $False
Invoke $workbook SaveAs "C:\PowerShell Kravextractor\excelfil.xls" > $null
Invoke $workbook Close 0 > $null
$word.Quit()
$excel.Quit()
[/code]
Og fejlmeddelelser fra Powershell ISE:
[code]
PS P:\> C:\PowerShell Kravextractor\Kravextractor.ps1
Exception calling "Open" with "1" argument(s): "Filen blev ikke fundet. ("C:\...\ks_test.docx")"
At C:\PowerShell Kravextractor\Kravextractor.ps1:12 char:28
+ $doc = $word.documents.open <<<< ("C:\PowerShell Kravextractor\ks_test.docx")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\PowerShell Kravextractor\Kravextractor.ps1:44 char:31
+ $style = $table.Cells.Item <<<< (1,1).Style
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\PowerShell Kravextractor\Kravextractor.ps1:46 char:34
+ If ($style.NameLocal.Contains <<<< ("Krav1 Overskrift"))
+ CategoryInfo : InvalidOperation: (Contains:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "InvokeMember" with "6" argument(s): "Typeuoverensstemmelse. (Undtagelse fra HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At C:\PowerShell Kravextractor\Kravextractor.ps1:3 char:33
+ $m.PSBase.GetType().InvokeMember <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodTargetInvocation
[/code]
