在C#中使用Word Interop:将图片添加到图片内容控件后错误的文本环绕样式 [英] Using Word Interop in C# : wrong text wrapping style after add picture into picture content control

查看:240
本文介绍了在C#中使用Word Interop:将图片添加到图片内容控件后错误的文本环绕样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



1 /我有一个Word文档。包装样式的图片内容控件是"方形"





2 /在C#中,我在上面的内容控件中添加了一张图片。这是我的示例代码:



           对象缺失= 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内容控件在contentControlsWithMatchingTag中)

            {

                var cc = contentControl.Range.InlineShapes.AddPicture(" C:\\Temp \\PType.jpg");

            }




$
            document.Save();

            applicationWord.Documents.Close();
$




$
3 /在结果中,包装样式始终设置到第一个选项"与文字一致"






4 /在C#代码中,如果我在图片添加后尝试更改包装样式:



            foreach(contentControl内容控件在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



您对我的问题有任何疑问吗?

提前感谢

解决方案

Hi Loc,


感谢您访问我们的论坛。然后,我们主要关注Office客户端的一般问题。由于您的查询与涉及Word的开发问题更相关,因此我将您的主题移至以下专用的MSDN论坛以获得更好的响应:


https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev


我们建议适当发布的原因是您将获得最合格的受访者群体,而定期阅读论坛的其他合作伙伴可以分享他们的知识或从您与我们的互动中学习。感谢您的理解。


问候,


Yuki Sun


I have a problem with picture content control's text wrapping style. Please follow steps below :

1/ I have a Word document. There's a picture content control with wrapping style is "square"


2/ In C#, I add a picture into content control above. This's my sample code :

            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/ In the result, wrapping style's always set to the first option "In line with text"


4/ In C# code, if I try to change wrapping style after picture's added :

            foreach (ContentControl contentControl in contentControlsWithMatchingTag)
            {
                var cc = contentControl.Range.InlineShapes.AddPicture("C:\\Temp\\PType.jpg");

                applicationWord.Selection.Range.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapSquare;
            }



It always raises the error "The Type method or property is not available because the drawing operation cannot be applied to the current selection." (it's a System.Runtime.InteropServices.COMException)


I'm using Win10, VS 2015 and MS Office 2016

Do you have any clue for my problem ?
Thank in advance

解决方案

Hi Loc,

Thanks for visiting our forum. Then here we mainly focus on general issues about Office client. Since your query is more related to developing issues involve Word, I'll move your thread to the following dedicated MSDN forum for Word for better response:

https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

Regards,

Yuki Sun


这篇关于在C#中使用Word Interop:将图片添加到图片内容控件后错误的文本环绕样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆