2017-07-25 370 views
1

我們正在更新MS Word加載項,允許用戶將MS Word文件插入到中央MS通過以下功能Word文檔:Microsoft.Office.Interop.Word Selection.InsertFile()方法 - >如何在範圍中使用書籤參數

//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 
); 

第二個參數:

ref missing, //Range - For Word can be a bookmark 

似乎是說,我們可以通過,可以是一個書籤(我們 可以用它來刪除一個範圍參數添加內容,如果需要的話),但是我們一直無法找到y實現示例,演示如何完成此操作。

MSDN描述這個參數如下:


範圍

類型:System.Object

可選的對象。

如果指定的文件是Word文檔,則該參數引用書籤。如果該文件是另一種類型(例如Microsoft Excel工作表),則此參數引用命名範圍或單元格區域(例如,R1C1:R3C4)。


既然我們要插入一個新的書籤與文本,我們認爲我們需要:

  1. 創建一個新的書籤,並
  2. 它傳遞到的insertFile參數列表

然而,在這一點上,由於我們無法找到任何實現這個目標的例子,所以我們處於一個停止點。

我們還發現例子,如這些:

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.bookmarks(v=vs.120).aspx

http://www.c-sharpcorner.com/article/add-replace-and-remove-bookmarks-in-word-using-c-sharp/

但它看起來並不像它們的實現使用Word互操作或Selection.InsertFile()。

智能感知似乎並沒有表明對Microsoft.Office.Interop.Word.Bookmark一個Range屬性:

enter image description here

感謝。

回答

0

書籤(您的拳頭鏈接)是Bookmark對象的集合。書籤有一個Range屬性。使用它。

+0

我認爲你包含的鏈接是指Microsoft.Office.Tools.Word而不是Microsoft.Office.Interop.Word。我們正在使用Interop。謝謝。 – jazzBox

+0

對不起,鏈接不對。我現在有鏈接到互操作書籤對象。同樣的答案,使用Bookmark.Range。 –

+0

我加了 Microsoft.Office.Interop.Word.Range rng = Globals.ThisAddIn.Application.Selection.Range; (Microsoft.Office.Interop.Word.Bookmark.Range似乎不可用,如上面的屏幕截圖所示。) 然後使用該參數,如我編輯的帖子中所示。 獲取此錯誤: 「類型不匹配(來自HRESULT的異常:0x80020005(DISP_E_TYPEMISMATCH))」 – jazzBox

-1

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是插入文件的書籤或範圍標識符。

@Edit:

如果你搜索的解決方案,以插入文件在現有的書籤,而不刪除書籤,嘗試這個辦法:用少許修改

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); 

(你只需要從別的地方得到bmRange和bookmarkName)這應該也沒有現有書籤

@編輯2: Remanufactored以擴展方法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); 
} 

這樣稱呼它:

document.ReplaceBookmarkContentWithFile(path, range, bookmarkName); 

作品都... exisiting書籤(如果範圍的書籤。範圍)或不;)

相關問題