自动调整 Word 文档中的形状
Auto fit a Shape in a Word document
如何使用 C# 或 vba (Word 2013) 在 Word 文档中自动调整 Shape
。
我试过使用 shape.Textframe.autosize
属性 但它不起作用。在 Excel 它有效,但在 word 中它抛出错误
"The specified value is out of range."
shape.TextFrame.AutoSize = (int)Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;
以上代码没有做任何事情。
shape.TextFrame2.AutoSize = Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;
以上代码抛出异常
"The specified value is out of range."
我需要自动调整 Word 文件中的图形以适合其中的文本。
代码在 Word 中的工作方式与在 Excel 或 PowerPoint 中不同的原因是 Word 的对象模型不支持 TextFrame2
的属性。 (最近在 Word 365 中测试。)它仅支持 TextFrame
。而AutoSize
for TextFrame
只支持True/False,不支持MsoAutoSize
枚举。
TextFrame.AutoSize
的文档:https://docs.microsoft.com/en-us/office/vba/api/excel.textframe.autosize
TextFrame2.AutoSize
的文档:https://docs.microsoft.com/en-us/office/vba/api/excel.textframe2.autosize
请注意,这些都适用于 Excel(截至该日期为 365,这意味着晚于 Office 2013)。对于 Word,没有 TextFrame2
文档。 Word 的对象模型接受它作为 Shape
对象的 属性,但如果代码试图使用任何 TextFrame2
的属性或方法,则会导致错误。例如,不可能使用 ActiveDocument.Shapes(1).TextFrame2.TextRange.Text
.
对于 C#:请记住,True 的等效项是 -1(而不是 1)。 False 始终为 0。
如何使用 C# 或 vba (Word 2013) 在 Word 文档中自动调整 Shape
。
我试过使用 shape.Textframe.autosize
属性 但它不起作用。在 Excel 它有效,但在 word 中它抛出错误
"The specified value is out of range."
shape.TextFrame.AutoSize = (int)Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;
以上代码没有做任何事情。
shape.TextFrame2.AutoSize = Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;
以上代码抛出异常
"The specified value is out of range."
我需要自动调整 Word 文件中的图形以适合其中的文本。
代码在 Word 中的工作方式与在 Excel 或 PowerPoint 中不同的原因是 Word 的对象模型不支持 TextFrame2
的属性。 (最近在 Word 365 中测试。)它仅支持 TextFrame
。而AutoSize
for TextFrame
只支持True/False,不支持MsoAutoSize
枚举。
TextFrame.AutoSize
的文档:https://docs.microsoft.com/en-us/office/vba/api/excel.textframe.autosize
TextFrame2.AutoSize
的文档:https://docs.microsoft.com/en-us/office/vba/api/excel.textframe2.autosize
请注意,这些都适用于 Excel(截至该日期为 365,这意味着晚于 Office 2013)。对于 Word,没有 TextFrame2
文档。 Word 的对象模型接受它作为 Shape
对象的 属性,但如果代码试图使用任何 TextFrame2
的属性或方法,则会导致错误。例如,不可能使用 ActiveDocument.Shapes(1).TextFrame2.TextRange.Text
.
对于 C#:请记住,True 的等效项是 -1(而不是 1)。 False 始终为 0。