We're doing an update to an MS Word Add-In that allows users to insert MS Word files into a central MS Word document by using the following function:
//Created rng variable
Microsoft.Office.Interop.Word.Range rng = Globals.ThisAddIn.Application.Selection.Range;
//MS Interop method.
Globals.ThisAddIn.Application.Selection.InsertFile(
filename,
rng,//<--Added range
ref missing, //Confirm Conversions
ref falsevalue, //Link file
ref falsevalue //Attachment
);
The second parameter:
ref missing, //Range - For Word can be a bookmark
Seems to say that we can pass a Range parameter that can be a bookmark (which we can use to delete the added content, if needed), however we haven't been able to find any implementation examples that demonstrate how this is done.
MSDN describes this parameter as follows:
Type: System.Object
Optional Object.
If the specified file is a Word document, this parameter refers to a bookmark. If the file is another type (for example, a Microsoft Excel worksheet), this parameter refers to a named range or a cell range (for example, R1C1:R3C4).
However at this point, since we can't find any examples of how this is achieved, we're at a stopping point.
We also found examples such as these:
http://www.c-sharpcorner.com/article/add-replace-and-remove-bookmarks-in-word-using-c-sharp/
But it doesn't look like their implementations are using Word Interop or Selection.InsertFile().
Intellisense doesn't seem to indicate a Range property on Microsoft.Office.Interop.Word.Bookmark:
Thanks.
Bookmarks (your fist link) are a collection of Bookmark objects. Bookmark has a Range property. Use that.
Type: System.Object
Optional Object.
If the specified file is a Word document, this parameter refers to a bookmark. If the file is another type (for example, a Microsoft Excel worksheet), this parameter refers to a named range or a cell range (for example, R1C1:R3C4).
Meant is a bookmark or range identifier of the inserted file.
@Edit:
In case you search a solution to insert files in a existing bookmark without delete the bookmark, try this:
var bmRange = bookmark.Range;
string bookmarkName = bookmark.Name;
// plus 1 on the end prevents startRange to collapse on insert.
// be shure there is something after the end
var startRange = document.Range(bmRange.Start, bmRange.End + 1);
object missing = System.Reflection.Missing.Value;
object ConfirmConversions = false;
object Link = false;
object Attachment = false;
bmRange.InsertFile("C:\\somefile.docx", ref missing, ref ConfirmConversions, ref Link, ref Attachment);
// after insert the startRange contains the inserted content + 1
var newRange = document.Range(startRange.Start, startRange.End - 1);
document.Bookmarks.Add(bookmarkName, ref newRange);
with a little modification (you only have to get the bmRange and bookmarkName from somewhere else) this should work also without an exisiting bookmark
@Edit2: Remanufactored to Extension Method for Microsoft.Office.Interop.Word.Document:
public static void ReplaceBookmarkContentWithFile(this Microsoft.Office.Interop.Word.Document document, string path, Microsoft.Office.Interop.Word.Range range, string bookmarkName)
{
var startRange = document.Range(range.Start, range.End + 1);
object missing = System.Reflection.Missing.Value;
object ConfirmConversions = false;
object Link = false;
object Attachment = false;
range.InsertFile(path, ref missing, ref ConfirmConversions, ref Link, ref Attachment);
object newRange = document.Range(startRange.Start, startRange.End - 1);
document.Bookmarks.Add(bookmarkName, ref newRange);
}
Call it like:
document.ReplaceBookmarkContentWithFile(path, range, bookmarkName);
works for both ... exisiting Bookmark (if the range is the bookmark.Range) or not ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.