I get Invalid parameters when I try to add a Building block in Word 2016.
The range is a cell in a table (the first cell in the last row). The Activedocument is the Template I would like to add the Building block to.
private void CreateNewBuildingblock(string sBuildingblockName)
{
Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
Template templateFile = (Template) currentDocument.get_AttachedTemplate();
currentDocument.Tables[1]
.Cell(currentDocument.Tables[1].Rows.Count, 1)
.Range.Text.Substring(
0,
currentDocument.Tables[1]
.Cell(currentDocument.Tables[1].Rows.Count, 1)
.Range.Text.Length - 2
);
Range rBuildingblockContent = Globals.ThisAddIn.Application.Selection.Range;
_ = templateFile.BuildingBlockEntries.Add(
Name: sBuildingblockName,
Type: WdBuildingBlockTypes.wdTypeCustom1,
Category: "Generals",
Range: rBuildingblockContent,
Description: "AV_Buildingblock",
InsertOptions: WdDocPartInsertOptions.wdInsertParagraph
);
}
Any suggestion / help is appreciated!
I solved it myself. The Range rBuildingblockContent is the content of a cell. A cell i Word contains "EndOfCell-sign" that cannot be part of the Range rBuildingblockContent. The solution is to select all the text in the cell except the "EndOfCell-sign".