2014-01-29 49 views
0

我正在創建一個Office 2013應用程序,使用HTML & Javascript。我想在應用程序中包含一個功能,即當用戶單擊某個按鈕時,相應的圖像將被插入到文檔中。 這個方法我試過..當按鈕被點擊時插入圖片html javascript

HTML:

<button id="img-insert-btn">Insert Image</button> 

的jQuery:

$('#img-insert-btn').click(insertImage); 

的Javascript:

function insertImage() { 
Office.context.document.setSelectedDataAsync("../ImageFolder/Image.png"); 
} 

我寫了上面的方法,會插入文件路徑進入文檔,而不是插入圖像。請幫忙。附: jQuery也可以隱含。

回答

0

的Javascript:

var imgHTML = "<img " + 
"src='http://i.msdn.microsoft.com/fp123580.AppHome2(en-us,MSDN.10).png'" 
+ " alt ='apps for Office image' img/>"; 

function setHTMLImage(imgHTML) 
    { 
     Office.context.document.setSelectedDataAsync(
      imgHTML, { coercionType: "html" }, 
      function (asyncResult) { 
       if (asyncResult.status == "failed") { 
        write('Error: ' + asyncResult.error.message); 
       } 
      }); 
    } 

答發現here通過@guli

1

做到這一點像:

$(document).ready(function(){ 
    $('button').click(function(){ 
     $('body').append('<img src="http://www.gratuit-en-ligne.com/telecharger-gratuit-en-ligne/telecharger-image-wallpaper-gratuit/image-wallpaper-animaux/img/images/image-wallpaper-animaux-autruche.jpg">'); 
    }); 
}) 

Fiddle demo.

+0

非常感謝!我會嘗試這一點,並標記這個正確的,如果它的工作。 – Preetesh

+0

沒什麼兄弟!它不工作...您插入的圖像適用於網頁... Office應用程序與網頁不同...我想在Word文檔中插入圖像,用戶正在其中工作..不在HTML的正文中文件! – Preetesh

+0

看看這個:http://blogs.msdn.com/b/officeapps/archive/2012/10/26/inserting-images-with-apps-for-office.aspx – guli