在C#中使用Word Interop:将图片添加到图片内容控件后出现错误的文本包装样式

在C#中使用Word Interop:将图片添加到图片内容控件后出现错误的文本包装样式,c#,ms-word,interop,office-interop,com-interop,C#,Ms Word,Interop,Office Interop,Com Interop,我对图片内容控件的文本包装样式有问题。请按照以下步骤操作: 我有一个Word文档。有一个包装样式为“方形”的图片内容控件 2/在C#中,我将图片添加到上面的内容控件中。这是我的示例代码: object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = true; object fileName = "C:\

我对图片内容控件的文本包装样式有问题。请按照以下步骤操作:

我有一个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();
3/因此,包装样式始终设置为第一个选项“与文本对齐”

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;
        }
它总是引发错误“类型方法或属性不可用,因为绘图操作无法应用于当前选择。”(它是System.Runtime.InteropServices.COMException)

我正在使用Win10、VS 2015和MS Office 2016


你对我的问题有什么线索吗?

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

出于好奇,您为什么要使用互操作服务?使用互操作我总是发现一些问题,因此建议使用类似openXML或类似的东西内容控制和图片不是一个实体。内容控件包含一个InlineShape对象(图片)。如果使用文本换行设置格式,则会从内容控件中删除图片。您可以在文本框(图形类型)或单单元格表格中插入内容控件,并使用文本换行设置其格式,同时内容控件及其图片与文本保持“对齐”。您同意我提供的信息吗?我应该把它写下来作为答案,还是我们应该以“不可复制”来结束这个问题?嗨,辛迪,你的解决方案救了我。谢谢你,如果答案中有更多关于为什么这样做的细节,那么muchIt会有所帮助。