2013-02-10 40 views
-2

我想插入一個圖像,然後插入一個新行,然後再插入另一個圖像。該過程必須重複,直到插入所有圖像。 這是我的代碼,目前其插入圖片,但不空行在圖像之間插入新行C#Word

using System.IO; 
using Word = Microsoft.Office.Interop.Word; 
namespace Snapper 
{ 
    class WordDocumentGenerator 
    { 
     public void CreateWordDocument(string fileName) 
     { 
      string originalPath = Directory.GetCurrentDirectory(); 
      string path = originalPath; 
      path += @"\snapshots"; 
      object oMissing = System.Reflection.Missing.Value; 

      //Create a new Word Application 
      Word._Application wordApp = new Word.Application(); 
      wordApp.Visible = false; 
      try 
      { 
       //Create a new blank document 
       Word._Document doc = wordApp.Documents.Add(ref oMissing, ref oMissing, 
                  ref oMissing, ref oMissing); 
       string[] images = Directory.GetFiles(path); 

       //Create a range 
       object myTrue = true; 
       object myFalse = false; 
       object endOfDoc = "\\endofdoc"; 
       object myRange; 


       foreach (var image in images) 
       { 
        myRange = doc.Bookmarks.get_Item(ref endOfDoc).Range; 
        //Add images to the document      
        doc.InlineShapes.AddPicture(image, ref myFalse, ref myTrue, ref myRange); 
        //Add a blank line 
        //doc.Content.Text = "\n"; 
       } 


       path = originalPath; 
       path += @"\documents"; 

       DirectoryInfo docDir = new DirectoryInfo(path); 
       if (!docDir.Exists) 
       { 
        docDir.Create(); 
       } 

       object savePath = path + @"\" + fileName + ".doc"; 

       doc.SaveAs(ref savePath, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing, 
        ref oMissing 
        ); 
       doc.Save(); 
      }    
      finally 
      { 
       wordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 
      }                   
     } 

    } 
} 

我需要做一些幫助。

+1

爲什麼?怎麼了? – 2013-02-10 15:22:52

+0

嘗試doc.Content.Text + = Environment.Newline; – JMK 2013-02-10 15:23:46

+0

@JMK,我嘗試了你的建議,但圖像插入一個在另一個之上,最後只有一個圖像持續在文檔而不是幾個 – kunaguvarun 2013-02-10 16:27:21

回答