我对图片内容控件的文本换行样式有疑问。请按照以下步骤操作:
1 /我有一个Word文档。有一个图片内容控件,包装风格是“方形”
2 /在C#中,我在上面的内容控件中添加了一张图片。这是我的示例代码:
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object fileName = "C:\\Temp\\Pic.docx";
var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
var document = new Microsoft.Office.Interop.Word.Document();
document = applicationWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
var contentControlsWithMatchingTag = document.SelectContentControlsByTag("Pic");
foreach (ContentControl contentControl in contentControlsWithMatchingTag)
{
var cc = contentControl.Range.InlineShapes.AddPicture("C:\\Temp\\PType.jpg");
}
document.Save();
applicationWord.Documents.Close();
4 /在C#代码中,如果我在添加图片后尝试更改包装样式:
foreach (ContentControl contentControl in contentControlsWithMatchingTag)
{
var cc = contentControl.Range.InlineShapes.AddPicture("C:\\Temp\\PType.jpg");
applicationWord.Selection.Range.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapSquare;
}
它总是引发错误“Type方法或属性不可用,因为绘图操作无法应用于当前选择。” (这是一个System.Runtime.InteropServices.COMException)
我正在使用Win10,VS 2015和MS Office 2016
你对我的问题有任何线索吗?
答案 0 :(得分:0)
var cc = contentControl.Range.InlineShapes.AddPicture("C:\\Temp\\PType.jpg");
替换为
var pic = document.Shapes.AddPicture("C:\\Temp\\PType.jpg");
并用
进行更改 pic.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;