2017-10-16 3 views
1

ここでは、OfficeアドインからWordにカーソルを移動してドキュメントファイルを挿入しようとしていますが、適切な解決策が見つかりませんでした。 insertLocationStartEndまたはReplaceすることができカーソルポイントの現在の反対のドキュメントに挿入する文書ファイル(insertFileFromBase64を使用)

bodyObject.insertFileFromBase64(base64File, insertLocation); 

: APIは唯一の3つのオプションがあります。

答えて

1

Word.InsertLocationのオプションは次のとおりです。

  • Start:前に、既存のコンテンツを挿入されたコンテンツを付加します。
  • Endの後にの内容を追加します。
  • Replace:既存のコンテンツを挿入されたコンテンツに置き換えます。

bodyObject.insertFileFromBase64を使用している場合は、ドキュメントの本文全体にスコープが設定されています。したがって、このメソッドを呼び出すと、カーソル位置が気になりません。

あなたが本当にここにほしいと思うのはrangeObject.insertFileFromBase64です。これは、ボディ全体ではなく範囲にスコープされます。現在の選択範囲から範囲を取得することができます(またはカーソル位置が選択されていない場合)。

Word.run(function (context) { 

    // Queue a command to get the current selection and then 
    // create a proxy range object with the results. 
    var range = context.document.getSelection(); 

    // Queue a commmand to insert base64 encoded .docx at the beginning of the range. 
    // You'll need to implement getBase64() to make this work. 
    range.insertFileFromBase64(getBase64(), Word.InsertLocation.start); 

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion. 
    return context.sync().then(function() { 
     console.log('Added base64 encoded text to the beginning of the range.'); 
    }); 
}) 
.catch(function (error) { 
    console.log('Error: ' + JSON.stringify(error)); 
    if (error instanceof OfficeExtension.Error) { 
     console.log('Debug info: ' + JSON.stringify(error.debugInfo)); 
    } 
}); 
+1

WOW、その動作は絶対良好です。おかげであなたは私を救った。 –

関連する問題