I am trying to group the image and text in one shape using Microsoft.Office.Interop.Word in C# .NET Framework 4.7 application. At the moment of grouping I have strange error "Grouping is desactivated for selected shapes"
Code:
public static void TestWordImageGrouping(string documentPath)
{
_Document doc = null;
Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.Application();
try
{
#region Document setup
app.NormalTemplate.Saved = true;
object template = Missing.Value;
object newTemplate = Missing.Value;
object documentType = Missing.Value;
object visible = Missing.Value;
doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
//New version
doc.PageSetup.PaperSize = WdPaperSize.wdPaperA3;
doc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
doc.PageSetup.PageWidth = 595.276F * 2;
doc.PageSetup.PageHeight = 841.8898F;
doc.PageSetup.TopMargin = 20;
doc.PageSetup.LeftMargin = 0;
doc.PageSetup.RightMargin = 0;
doc.PageSetup.BottomMargin = 0;
#endregion
try
{
RectangleF imagePosition = new RectangleF(40, 30, 400, 320);
RectangleF labelPosition = new RectangleF(40, 400, 400, 50);
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Content","fail.jpg");
Microsoft.Office.Interop.Word.Shape imageShape = doc.Shapes.AddPicture(imagePath, Left: imagePosition.X,
Top: imagePosition.Y, Width: imagePosition.Width, Height: imagePosition.Height);
imageShape.Name = "Image1";
Microsoft.Office.Interop.Word.Shape imageLabel = doc.Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
labelPosition.X,
labelPosition.Y,
labelPosition.Width,
labelPosition.Height);
imageLabel.TextFrame.TextRange.Text = "It Expectance vs Real life";
imageLabel.Name = "Label1";
imageLabel.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
doc.SaveAs(FileName: documentPath);
object shapesIndex = new object[] { imageShape.Name, imageLabel.Name };
var range = doc.Shapes.Range(shapesIndex);
doc.Save();
range.Group();
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
}
app.Visible = true;
app.Activate();
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
try
{
doc.Saved = true;
app.Quit();
}
catch (Exception exept)
{
System.Diagnostics.Trace.Write(exept.ToString());
}
}
}
Same time, i succeed to group three designed in code lines. Can you, please, share the solution if you successfully resolved problem like this with C# .Net Framework Word interop
Thank you.