Reputation: 1
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 ?
Upvotes: 0
Views: 2923
Reputation: 11
var cc = contentControl.Range.InlineShapes.AddPicture("C:\\Temp\\PType.jpg");
replace with
var pic = document.Shapes.AddPicture("C:\\Temp\\PType.jpg");
and change it with
pic.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
Upvotes: 1