2014-05-19 63 views
-1

這裏是我的代碼。錯誤沒有重載方法的'添加'需要'0'參數,使用Word Interop

private void button1_Click(object sender, EventArgs e) 
{ 
    { 
     // first we are creating application of word. 
     Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application(); 
     // now creating new document. 
     WordApp.Documents.Add(); 
     // see word file behind your program 
     WordApp.Visible = true; 
     // get the reference of active document 
     Microsoft.Office.Interop.Word.Document doc = WordApp.ActiveDocument; 
     // set openfiledialog to select multiple image files 
     OpenFileDialog ofd = new OpenFileDialog(); 
     ofd.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF"; 
     ofd.Title = "Select Image To Insert...."; 
     ofd.Multiselect = true; 
     // if user select OK, then process for adding images 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      // iterating process for adding all images which is selected by filedialog 
      foreach (string filename in ofd.FileNames) 
      { 
       // now add the picture in active document reference 
       doc.InlineShapes.AddPicture(filename, Type.Missing, Type.Missing, Type.Missing); 
      } 
     } 
     // file is saved. 
     doc.SaveAs("c:\\hello.doc", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
     // application is now quit. 
     WordApp.Quit(Type.Missing, Type.Missing, Type.Missing); 
    } 

} 

我對着下面提及的錯誤在WordApp.Documents.Add();

Error: No overload method for Add takes 0 arguments

你能請幫助我解決這個問題?

我是新來編碼。

+0

您需要將對象傳遞給'Add' 看看http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents .add.ASPX – TryingToImprove

+0

請在發佈之前先進行一些研究工作:http://msdn.microsoft.com/en-us/library/Microsoft.Office.Interop.Word(v=office.15).aspx –

+0

有趣,但文檔說每個參數都是「可選的」。醜陋的他們如何不能在C#中完成這項工作作爲真正的可選參數。 – crashmstr

回答

2

我相信這是一個版本的問題,你在其他methods.Then傳遞Type.Missing可選參數你應該通過Type.MissingAdd方法的參數也是如此。

WordApp.Documents.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
+0

我認爲他使用C#3.0(或更少),所以沒有可選的參數 –

0
object objTest = new object(); 
objTest = Type.Missing; 
WordApp.Documents.Add(ref objTest, ref objTest, ref objTest, ref objTest); 
相關問題