字互操作 将文档属性字段添加到表 - "System.Runtime.InteropServices.COMException: 'This command is not available.'



我正在尝试使用Word互操作将自定义文档属性的字段添加到Word文档中的表中。但是,当我这样做时,我得到了一个错误:

"System.Runtime.InteropServices。COMException: '此命令不可用'">

我已经尝试了多种方法来解决这个问题,代码只适用于将字段添加到文档内的范围,但每当我尝试将字段添加到表内的单元格中时,我都会得到相同的错误。有没有人知道怎么做,或者我做错了什么?

我要做的一些示例代码是:

Word.Section section = wordDoc.Sections[1];
Word.Range range = section.Range;
Word.Table table = range.Tables.Add(range, 1, 3);
//Format table
table.Cell(1, 1).Range.Fields.Add(table.Cell(1,1).Range, Word.WdFieldType.wdFieldDocProperty,
"DocPropertyName", true);

关于我之前的评论,确实不能向单元格添加字段。范围作为一个整体:下面的示例在表格的页脚'foot'中插入一个页码作为' ' x ' ';其中x为当前页数,y为总页数。

Dim rng As Range = footerTable.Cell(1, 3).Range.Duplicate
rng.Collapse(WdCollapseDirection.wdCollapseStart)

Dim fld As Field = foot.Range.Fields.Add(Range:=rng, Type:=WdFieldType.wdFieldPage)
If fld.Result.Text <> Nothing Then rng.MoveEnd(WdUnits.wdCharacter, Count:=fld.Result.Text.Count)

rng.Collapse(WdCollapseDirection.wdCollapseEnd)
rng.InsertAfter(Text:=" of ")
rng.Collapse(WdCollapseDirection.wdCollapseEnd)
foot.Range.Fields.Add(Range:=rng, Type:=WdFieldType.wdFieldNumPages)

p。

最新更新