在C#中使用Word Interop:将图片添加到图片内容控件后,文本换行样式错误

问题描述 投票:0回答:1

我对图片内容控件的文字环绕样式有疑问。请按照以下步骤操作:

1 /我有一个Word文档。有一个图片内容控件,其包装样式为“正方形”enter image description here

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();

3 /结果,换行样式始终设置为第一个选项“与文本对齐”enter image description here

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

您对我的问题有任何线索吗?

c# ms-word interop office-interop com-interop
1个回答
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;

© www.soinside.com 2019 - 2024. All rights reserved.