I select a range out of a Bookmark position (working).
Word.ContentControl wcc;
Word.Selection WSelection = wApp.Selection;
object obj;
obj = (object)wApp.ActiveDocument.Bookmarks.get_Item(bookmark).Range;
Then I move before the range and add a paragraph and move to the new paragraph. This is because I want to add the ContentControl BEFORE the Bookmark (for ordering reasons) (working):
Word.Range WRange = WSelection.Range;
object count = 1;
object back = -1;
(obj as Word.Range).Move(Unit: Word.WdUnits.wdCharacter, Count: ref back);
(obj as Word.Range).Paragraphs.Add();
(obj as Word.Range).Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);
Now I add a ContentControl (working):
wcc = WRange.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText, ref obj);
wcc.LockContentControl = true;
wcc.SetPlaceholderText(null, null, " ");
wcc.Tag = fileName;
And now I want to insert another Word-File into that ContentControl (not working!):
wcc.Range.InsertFile(Path.Combine(path, fileName));
The external Word-File contains a Table with 4 rows (just a sample file). The content is now inserted AFTER the ContentControl, not inside?
This is how the result looks:
If I try to set the Text of the ContentControl it is working fine (content "test" is now inside of the ContentControl):
wcc.Range.Text = "test";
As a result of the last test, I think I have selected the correct range, but why is the table outside of the ContentControl?
EDIT:
I recorded a Macro with the steps I want to do by code and foud out, that I cant insert a Table in a line where a ContentControl and some other character is in (in my case a space). So a added another Paragraph to get this solution:
Word.Range WRange = WSelection.Range;
object next = 1;
object back = -1;
(obj as Word.Range).Move(Unit: Word.WdUnits.wdCharacter, Count: ref back);
(obj as Word.Range).Paragraphs.Add();
(obj as Word.Range).Move(Unit: Word.WdUnits.wdParagraph, Count: ref next);
(obj as Word.Range).Paragraphs.Add();
(obj as Word.Range).Move(Unit: Word.WdUnits.wdCharacter, Count: ref back);
wcc = WRange.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText, ref obj);
wcc.LockContentControl = true;
wcc.SetPlaceholderText(null, null, " ");
wcc.Tag = fileName;
wcc.Range.InsertFile(Path.Combine(path, fileName));
the result is, that there is an extra paragraph between the first and second added ContentControl (including the paragraphs at the beginning and at the end of the table inside the ContentControl) in total 3 paragraphs... Does anybody provide a better solution?
I created a little test method based on your code to do a bit of black box testing.
To do this I created two small files:
After running the exact code you showed here it worked without problems. To see if it had something to do with the ranges you used I did a bit of rewriting:
[TestMethod]
public void TestInsertFile()
{
Application word = new Microsoft.Office.Interop.Word.Application();
word.Visible = true;
Document doc = word.Documents.Open(@"c:\temp\testdoc1.docx");
Range rng = word.ActiveDocument.Bookmarks.get_Item("testbookmark").Range;
object count = 1;
object back = -1;
rng.Move(Unit: Word.WdUnits.wdCharacter, Count: ref back);
rng.Paragraphs.Add();
rng.Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);
object obj = rng as object;
Word.ContentControl wcc = word.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref obj);
wcc.LockContentControl = true;
wcc.SetPlaceholderText(null, null, " ");
wcc.Tag = @"c:\temp\testdoc2.docx";
wcc.Range.InsertFile(wcc.Tag);
}
So your issue does not seems to be code related. See if there is other content in your source or target documents that might influence the behavior. Try to run the code in my TestMethod and see if the isolated code does the same at your side.