如何通过PowerShell修改Word表格列宽?报错求助
PowerShell修改Word表格列宽问题解决
原始脚本
Add-Type -AssemblyName Microsoft.Office.Interop.Word # Initialize Word application $word = New-Object -ComObject Word.Application $word.Visible = $True # Open the document $doc = $word.Documents.Open("C:\temp.docx") # Get the table $table = $doc.Tables.Item(1) # change orientation of doc $doc.PageSetup.Orientation = [Microsoft.Office.Interop.Word.WdOrientation]::wdOrientLandscape # Set behavior of table to autofitcontent $table.AutoFitBehavior = [Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent # Set overall width of table $table.PreferredWidth = InchesToPoints(10.3) #set column width $table.Columns[1].Width = InchesToPoints(0.19) $table.Columns[2].Width = InchesToPoints(2.92) $table.Columns[3].Width = InchesToPoints(0.81) $table.Columns[4].Width = InchesToPoints(1.71) $table.Columns[5].Width = InchesToPoints(3.1) $table.Columns[6].Width = InchesToPoints(1.55) # Save and close the document $doc.Save() $doc.Close() # Quit the Word application $word.Quit()
运行报错
AutoFitBehavior' is not a property. At C:\temp.ps1:14 char:1 + $table.AutoFitBehavior = [Microsoft.Office.Interop.Word.WdAutoFitBeha ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException InchesToPoints : The term 'InchesToPoints' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\temp.ps1:16 char:25 + $table.PreferredWidth = InchesToPoints(10.3) + ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (InchesToPoints:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:20 char:1 + $table.Columns[1].Width = InchesToPoints(0.19) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:21 char:1 + $table.Columns[2].Width = InchesToPoints(2.92) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:22 char:1 + $table.Columns[3].Width = InchesToPoints(0.81) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:23 char:1 + $table.Columns[4].Width = InchesToPoints(1.71) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:24 char:1 + $table.Columns[5].Width = InchesToPoints(3.1) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Cannot access individual columns in this collection because the table has mixed cell widths. At C:\temp.ps1:25 char:1 + $table.Columns[6].Width= InchesToPoints(1.55) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
尝试的其他脚本
Add-Type -AssemblyName Microsoft.Office.Interop.Word $wordAssembly = [Microsoft.Office.Interop.Word.Application].Assembly # Initialize Word application $word = New-Object -ComObject Word.Application $word.Visible = $True # Open the document $doc = $word.Documents.Open("C:\temp.docx") # Get the table $table = $doc.Tables(1) $table.AutoFitBehavior = [Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent $doc.PageSetup.Orientation = [Microsoft.Office.Interop.Word.WdOrientation]::wdOrientLandscape $table.Columns(1).Width = 72*0.19 $table.Columns(2).Width = 72*2.92 $table.Columns(3).Width = 72*0.81 $table.Columns(4).Width = 72*1.71 $table.Columns(5).Width = 72*3.1 $table.Columns(6).Width= 72*1.55 # Save and close the document $doc.Save() $doc.Close() # Quit the Word application $word.Quit() $version = $wordAssembly.FullName $version [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
问题分析与解决方案
1. AutoFitBehavior不是属性的问题
AutoFitBehavior是方法而非属性,不能直接赋值,需要通过调用方式使用:
$table.AutoFitBehavior([Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent)
2. InchesToPoints未识别的问题
InchesToPoints是Word应用对象的专属方法,必须通过$word实例调用,不能作为独立命令使用:
# 正确调用方式 $table.PreferredWidth = $word.InchesToPoints(10.3) $table.Columns[1].Width = $word.InchesToPoints(0.19)
手动转换的话,1英寸=72磅,72*0.19的逻辑是对的,但建议用[double]72*0.19避免整数运算精度问题。
3. 表格有混合单元格宽度无法访问列的问题
如果表格存在合并单元格、不同行的列宽不一致(混合宽度),直接访问Columns集合会触发报错。解决方法:
- 先重置表格布局,清除混合宽度规则:
$table.AllowAutoFit = $true $table.AutoFitBehavior([Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitWindow) - 若表格有合并单元格,可通过单元格对象而非列对象设置宽度:
# 针对第一行单元格设置宽度,后续行无合并的话会自动继承 $table.Rows[1].Cells[1].Width = $word.InchesToPoints(0.19) $table.Rows[1].Cells[2].Width = $word.InchesToPoints(2.92)
修正后的完整脚本
Add-Type -AssemblyName Microsoft.Office.Interop.Word # 初始化Word应用 $word = New-Object -ComObject Word.Application $word.Visible = $True # 打开文档 $doc = $word.Documents.Open("C:\temp.docx") # 获取第一个表格 $table = $doc.Tables.Item(1) # 设置文档为横向 $doc.PageSetup.Orientation = [Microsoft.Office.Interop.Word.WdOrientation]::wdOrientLandscape # 重置表格布局,解决混合宽度问题 $table.AllowAutoFit = $true $table.AutoFitBehavior([Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent) # 设置表格整体宽度 $table.PreferredWidth = $word.InchesToPoints(10.3) # 设置各列宽度 $table.Columns.Item(1).Width = $word.InchesToPoints(0.19) $table.Columns.Item(2).Width = $word.InchesToPoints(2.92) $table.Columns.Item(3).Width = $word.InchesToPoints(0.81) $table.Columns.Item(4).Width = $word.InchesToPoints(1.71) $table.Columns.Item(5).Width = $word.InchesToPoints(3.1) $table.Columns.Item(6).Width = $word.InchesToPoints(1.55) # 保存并关闭文档 $doc.Save() $doc.Close() # 退出Word应用并释放COM对象 $word.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($table) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers()
内容的提问来源于stack exchange,提问作者ScareFries
相关产品推荐
相关产品推荐

